DF robot motorshield code

Hey

I've bought the Arduino Motor Shield (L293) from dfrobot.com. I am planning on making an obstacle
avoiding robot with the use of three PING)))-sensors.

However, i am really new to this, and i can't seem to figure out how the code they supply with the shield works...

Code source:

The code itself:

//This motor shield use Pin 6,5,7,4 to control the motor
// Simply connect your motors to M1+,M1-,M2+,M2-
// Upload the code to Arduino/Roboduino
// Through serial monitor, type 'a','s', 'w','d','x' to control the 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 for stop, and 255 for 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 to output mode

for(i=4;i<=7;i++) //For Arduino Motor Shield
pinMode(i, OUTPUT); //set pin 4,5,6,7 to 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 the speed, such as 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 am thinking about creating functions for different actions

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

for example:

if

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

Is this an okay approach?

thanks in advance!

I am thinking about creating functions for different actions

Not to add to that crappy code, I hope. There is NO excuse for running an infinite loop inside an infinite loop().

There is no excuse for not checking that there is serial data before trying to read the serial data.

Is this an okay approach?

On a scale of 1 to 10, OK would be a 3. Your idea is about a 7.

PaulS:
Not to add to that crappy code, I hope. There is NO excuse for running an infinite loop inside an infinite loop().

There is no excuse for not checking that there is serial data before trying to read the serial data.
On a scale of 1 to 10, OK would be a 3. Your idea is about a 7.

Thanks for your reply!

I am not planning on using that code, no. I merely want to understand how it works, so i can adapt it to my own code. I want to write the sketch from scratch but, frankly, i have no idea where to start...

quote author=Delta_G link=msg=2193320 date=1429391731]
Which part do you not understand?

If you are new to programming, I would suggest hitting google up for "C++ tutorials" before you get started. You will be glad you did.
[/quote]

I'm not that new to programming, i have done a course in Java at my university for example, but it is a couple of years ago now.. i'll try to point out the things i don't understand:

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

Why EN and IN?

void Motor1(int pwm, boolean reverse)
{
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
if(reverse)
{
digitalWrite(IN1,HIGH);
}
else
{
digitalWrite(IN1,LOW);
}
}

analogWrite sets the speed, and digitalWrite sets direction, correct?
However, i do not understand the what the if statement does in this case.. why reverse? and why boolean?
And naturally i can't seem to grasp the next part.. I understand press W for forward, S for stop and so on, but not the content of the cases...

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 the speed, such as 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;

}

}

}
}

Mr. Delta_G, thanks alot..

I fooled around a bit after i got your reply, and think i figured out how its all connected.

I mentioned i wanted to create functions earlier, called upon depending on the values of the sensors.

will it suffice with simple functions like

void forward(){

Motor1(50,true);
Motor2(50,true);
}

and

void reverse(){

Motor1(100,false);
Motor2(100,false);
}

and so on, or is it too primitive?

I really appreciate your help. Thanks alot!!
Enjoy the rest of your weekend

will it suffice with simple functions like ... and so on, or is it too primitive?

Functions should encapsulate a single action. Yours do. They are just fine.