cool2005tzh 发表于 2013-1-16 18:28:35

第一次做程序,,,两三个月之前做的摇头和扫描子程序,求指正

本来是做小车的 好久没动了。。。要知道我们学校十月份开始考试考到现在了 怨念啊!!!!

这个程序用来扫描最近距离的,检测到最近距离了之后迅速摇头转向最近的地方。。。。但是有时候会卡。。。不知道为什么。

其实我知道这样做不怎么好,等暑假了我把它修改一下,应该先把扫描到的东西存数组的 但那时候就没想到。。。前段时间想到了懒得加。。。求大神帮优化~~~~

cool2005tzh 发表于 2013-1-16 18:29:58

本帖最后由 cool2005tzh 于 2013-1-16 18:43 编辑

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

cool2005tzh 发表于 2013-1-16 18:31:05

#include <arduino.h>

int trig = 2;
int echo = 3;
int servo=4;
Servo frontservo;
float nearest=99999;
int angle=90;
float lonth=9999;

void scan(int pos);

//--------------------------------------------------------------
//---------------protect servo--------------------------------
//----------------------------------------------------------------
void ahead()
{
int npos=frontservo.read();
if (npos>90)
{
    for(int pos=npos;pos>90;pos-=1)
    {
      frontservo.write(pos);
      delay(5);
    }
}

if (npos<90)
{
    for(int pos=npos;pos<90;pos+=1)
    {
      frontservo.write(pos);
      delay(5);
    }
}

}
void research()
{
//--------------------------------------------------------------
//---------------protect tuoji--------------------------------
//----------------------------------------------------------------
int npos=frontservo.read();
for(int pos=npos;pos>0;pos-=1)
{
    frontservo.write(pos);
    delay(5);
}
//--------------------------------------------------------------
//-------------------research 0--180--------------------------------
//----------------------------------------------------------------
for(int pos=0;pos<180;pos+=5)
{
    frontservo.write(pos);
    scan(pos);
}
//--------------------------------------------------------------
//---------------research 180--0--------------------------------
//----------------------------------------------------------------
for(int pos=180;pos>0;pos-=2)
{
    frontservo.write(pos);
    scan(pos);
}
//--------------------------------------------------------------
//---------------pot to the nearest eagle--------------------------------
//----------------------------------------------------------------
for(int pos=0;pos<angle;pos+=2)
{
   frontservo.write(pos);
   delay(5);
}
//Serial.print("it is the nearest");
//delay(1000);
}

void scan(int pos)
{
    digitalWrite(trig,1);//sent signal
    delay(10);
    digitalWrite(trig,0);
    float time=pulseIn(echo,HIGH);//resive signal
    float lonth=((time)*340)/20000;
    nearest=min(lonth,nearest);//nearest or not
    if(nearest==lonth & lonth<15)
    angle=pos;
   
    /*Serial.print("time:");
    Serial.println(time);
    Serial.print("lonth:");
    Serial.print(lonth);
    Serial.print("       neareat:");
    Serial.println(nearest);
    Serial.print("angle:");
    Serial.print(pos);
    Serial.print("       nearest angle:");
    Serial.println(angle);*/
}

cool2005tzh 发表于 2013-1-16 18:33:24

这个是一点不起作用的主程序和小车运动的程序。。。。纠结 就是只会摇头


#include <Servo.h>
#include "action.h"
#include "research.h"

void setup()
{
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
//Serial.begin(9600);
frontservo.attach(servo);
}

void loop()
{
ahead();
research();
while(1)
{
    direction();
}
}

void direction()
{
scan(angle);
if(lonth>15)
{   
    goahead();
}
else
{
    research();
    if (angle>100)
    turnr();
    if (angle<80)
    turnl();
}

}




前进后退什么的


#include <arduino.h>
int lmotor1=5;
int lmotor2=6;
int rmotor1=7;
int rmotor2=8;

void goahead()
{
digitalWrite(lmotor1,HIGH);
digitalWrite(lmotor2,LOW);
digitalWrite(rmotor1,HIGH);
digitalWrite(rmotor2,LOW);
}

void back()
{
digitalWrite(lmotor1,LOW);
digitalWrite(lmotor2,HIGH);
digitalWrite(rmotor1,LOW);
digitalWrite(rmotor2,HIGH);
}

void turnl()
{
digitalWrite(lmotor1,LOW);
digitalWrite(lmotor2,HIGH);
digitalWrite(rmotor1,HIGH);
digitalWrite(rmotor2,LOW);
}

void turnr()
{
digitalWrite(lmotor1,HIGH);
digitalWrite(lmotor2,LOW);
digitalWrite(rmotor1,LOW);
digitalWrite(rmotor2,HIGH);
}

cool2005tzh 发表于 2013-1-16 18:36:39

我现在又买了6dof还有蓝牙模块。。。到时候做出来玩玩~~~~;P
页: [1]
查看完整版本: 第一次做程序,,,两三个月之前做的摇头和扫描子程序,求指正