PDA

View Full Version : Line Follower with one sensor - Help to improve the code



curka
01-29-2014, 09:15 PM
Hi All!

I and my brother started to build a line follower with one Line Finder sensor. This is our first Arduino project, please help us to improve the code. Is it okay? It’s very important for my brother.

http://forum.arduino.cc/Smileys/arduinoWide/smiley-red.png

Parameters:
ArduinoATMega2560
Pololu tb6612fng
2 DC motos 5V
1 Line Finder Sensor

http://kepfeltoltes.hu/thumb/140130/K_sz_kapcsol_si_rajz_www.kepfeltoltes.hu_.jpg (http://kepfeltoltes.hu/view/140130/K_sz_kapcsol_si_rajz_www.kepfeltoltes.hu_.jpg)
A képet a Képfeltöltés.hu tárolja. http://www.kepfeltoltes.hu

I uploaded the Schematic and the Code.

Code:



#define M1 44 //Pin4 : Motor1 direction of rotation (Left Motor)
#define EN1 5 //Pin5 : Motor1 speed (PWM)
#define EN2 6 //Pin6 : Motor2 speed (PWM)
#define M2 46 //Pin7 : Motor2 direction of rotation (Right Motor)

int LineFinder = 52; // sensor input is connected to digital
int Motor = 80; // Initial motor speed
int limit = 820; // Line detection limit

void setup() //settings
{
pinMode(EN1, OUTPUT); //Motor1 speed lab: output
pinMode(EN2, OUTPUT); //Motor2 speed lab: output
pinMode(M1, OUTPUT); // Motor1 direction of rotation lab: output
pinMode(M2, OUTPUT); // Motor2 direction of rotation lab: output
}

void loop() //Main program - infinite loop
{
LineFinder = digitalRead (52); // voltage measurement of sensor (0 - 1023 value between)

// If the sensor is above the pale floor: forward
if (LineFinder>Limit)
{
MotorLeft(Motor-2); //Left motor: forward
MotorRight(Motor); //Right motor: forward
}

// If the sensor is above the line: Turn left
else if (LineFinder>Limit)
{
MotorLeft(0); //Left motor: stop
MotorRight(Motor); //Right motor: forward
}

// Forward (the sensor is above the line: crossroads)
else if (LineFinder>Limit)
{
MotorLeft(Motor-2); //Left motor: forward
MotorRight(Motor); //Right motor: forward
}
delay(50); // Wait 50ms
}

void MotorLeft(int speed)
{
if (speed>0) //Forward
{
digitalWrite(M1,HIGH); //M1 direction (forward)
analogWrite(EN1,speed*255/100); //M1 speed(PWM)
}
else //Reverse
{
digitalWrite(M1,LOW); //M1 direction (reverse)
analogWrite(EN1,abs(speed)*255/100); //M1 speed(PWM)
}
}

void MotorRight(int speed)
{
if (speed>0) //Forward
{
digitalWrite(M2,HIGH); //M2 diretion (forward)
analogWrite(EN2,speed*255/100); //M2 speed(PWM)
}
else //Reverse
{
digitalWrite(M2,LOW); //M2 direction (reverse)
analogWrite(EN2,abs(speed)*255/100); //M2 speed(PWM)
}
}

Deimos
01-30-2014, 09:16 PM
First of all, you should use code tags when posting code so that we can read it.

void loop() //Main program - infinite loop
{
LineFinder = digitalRead (52); // voltage measurement of sensor (0 - 1023 value between)

// If the sensor is above the pale floor: forward
if (LineFinder>Limit)
{
MotorLeft(Motor-2); //Left motor: forward
MotorRight(Motor); //Right motor: forward
}
...
is considerably more legible than:


void loop() //Main program - infinite loop
{
LineFinder = digitalRead (52); // voltage measurement of sensor (0 - 1023 value between)

// If the sensor is above the pale floor: forward
if (LineFinder>Limit)
{
MotorLeft(Motor-2); //Left motor: forward
MotorRight(Motor); //Right motor: forward
}
...

Secondly, it's a little difficult to help you out without knowing specifically what you need help with. For me, my code is "okay" if it works, and I can't tell you if your robot works or not :). Are you worried about clear comments? Optimization? Following guidelines? Just writing good quality code? Or something else?

A little more details can go a long way in getting some help ;).

KevinO
01-30-2014, 10:23 PM
Edited above post for easier reading. :) Also moved thread into appropriate section.

jwatte
01-30-2014, 10:32 PM
The comment says "0 to 1023" yet the code uses digitalRead(), which only returns HIGH or LOW.