DF robot motorshield code


hey

i've bought arduino motor shield (l293) dfrobot.com. planning on making obstacle
avoiding robot use of 3 ping)))-sensors.

however, new this, , can't seem figure out how code supply shield works...

code source:
http://www.dfrobot.com/wiki/index.php?title=arduino_motor_shield_(l293)_(sku:_dri0001)

the code itself:

//this motor shield use pin 6,5,7,4 control motor
// connect motors m1+,m1-,m2+,m2-
// upload code arduino/roboduino
// through serial monitor, type 'a','s', 'w','d','x' control motor
// www.dfrobot.com
// last modified on 24/12/2009
 
int en1 = 6; 
int en2 = 5;  //roboduino motor shield uses pin 9
int in1 = 7;
int in2 = 4; //latest version use pin 4 instead of pin 8
 
 
 
void motor1(int pwm, boolean reverse)
        {
          analogwrite(en1,pwm); //set pwm control, 0 stop, , 255 maximum speed
         if(reverse)
         {
          digitalwrite(in1,high);   
         }
        else
        {
          digitalwrite(in1,low);   
         }
        } 
         
void motor2(int pwm, boolean reverse)
        {
          analogwrite(en2,pwm);
         if(reverse)
         {
          digitalwrite(in2,high);   
         }
        else
        {
          digitalwrite(in2,low);   
         }
        } 
       
void setup()
{
    int i;
   // for(i=6;i<=9;i++) //for roboduino motor shield
   // pinmode(i, output);  //set pin 6,7,8,9 output mode
 
    for(i=4;i<=7;i++)  //for arduino motor shield
    pinmode(i, output);  //set pin 4,5,6,7 output mode
 
    serial.begin(9600);   
}
 
 
void loop()
{
  int x,delay_en;
  char val;
  while(1)
  {
    val = serial.read();
    if(val!=-1)
       {
          switch(val)
           {
             case 'w'://move ahead
                        motor1(100,true);  //you can change speed, such motor(50,true)
                        motor2(100,true);
                       
                         break;
             case 'x'://move back
                        motor1(100,false);
                        motor2(100,false);
                         break;
             case 'a'://turn left
                        motor1(100,false);
                        motor2(100,true);
                         break;       
             case 'd'://turn right
                        motor1(100,true);
                        motor2(100,false);
                        break;   
               case 's'://stop
                        motor1(0,false);
                        motor2(0,false);
                         break;
                                   
           }     
         
       }
           
  }                           
}

i thinking creating functions different actions

forward(); turnleft(); turnright() etc., , call on these functions depending on state of sensors.

for example:

if

front sensor < 10cm, stop - check left sensor, check right sensor, if right sensor>left sensor, turn left , on.

is okay approach?

thanks in advance!


quote
i thinking creating functions different actions
not add crappy code, hope. there no excuse running infinite loop inside infinite loop().

there no excuse not checking there serial data before trying read serial data.

quote
is okay approach?
on scale of 1 10, ok 3. idea 7.


Arduino Forum > Using Arduino > Programming Questions > DF robot motorshield code


arduino

Comments