Hello everyone,
I recently started building a Line Follower for an university project using an Arduino Nano V3 (ATmega328) but I've ran into a problem. We had to implement a countdown (5 seconds) before the car starts moving (that's the use of the for loop) but if I try to write Motor(-120,-120) in my function that controls the steering my Arduino resets and counts down again from 5. The build in white led ('pin' 13) on my arduino blinks as well, as if I interrupted power.
Are there any mistakes in my code? Below a summary of what I'm trying to achieve
EDIT: Thanks to PaulS, I realized my code was in an entirely wrong format/structure, so I reprogrammed the entire thing:
int Red = 7; // Red Led
int Green = 8; // Green Led
int Blue = 9; // Blue Led
int RForward = 5;
int LForward = 6;
int LBackward = 10;
int RBackward = 11;
int SensorL; // Value of A1 (left sensor) will be assigned to this variable
int SensorLM ; // Value of A2 (midleft sensor) will be assigned to this variable
int SensorM; // Value of A3 (mid sensor) will be assigned to this variable
int SensorRM; // Value of A4 (midright sensor) will be assigned to this variable
int SensorR; // Value of A5 (right sensor) will be assigned to this variable
int Base; // Basespeed used in the engine function; Motor(L,R)
int Pot; // Potentiometer (Raw data from A0)
int Pot135; // Mapped value from potentiometer
int LF; int RF;
void setup() {
pinMode(Red,OUTPUT); pinMode(Green,OUTPUT); pinMode(Blue,OUTPUT);
pinMode(RForward,OUTPUT); pinMode(LForward,OUTPUT); pinMode(RBackward,OUTPUT); pinMode(LBackward,OUTPUT);
for(int Led = 0; Led < 5; Led++){
delay(500); digitalWrite(Red,HIGH); delay(500); digitalWrite(Red,LOW);
if(Led > 3){digitalWrite(Green,HIGH);}
}
}
void Motor(int L,int R){
if((L >= 0) && (R >= 0)){
LF = (L + Base);
RF = (R + Base);
}
else{
LF = L;
RF = R;
}
if(L >= 0){ // Instructions for the left wheel
analogWrite(LForward,LF);
analogWrite(LBackward,0);
}
else{
analogWrite(LBackward,abs(L));
analogWrite(LForward,0);
}
if(R >= 0){ // Instructions for the right wheel
analogWrite(RForward,RF);
analogWrite(RBackward,0);
}
else{
analogWrite(RBackward,abs(R));
analogWrite(RForward,0);
}
if(L != R){ // If one of the wheels turns different compared to another
digitalWrite(Blue,HIGH); // the blue led will light up instead of the usual green one
digitalWrite(Green,LOW);
}
else{
digitalWrite(Blue,LOW);
digitalWrite(Green,HIGH);
}
}
void loop() {
Pot = analogRead(A0);
Pot135 = map(Pot,0,1023,60,135); // Basevalue is 60, because the wheels block if driver input (PWM) is below 60
if(Pot > 0){
Base = Pot135;
}
else{
Base = 0;
}
SensorL = analogRead(A1); // Values read from the infrared sensors (Left, Midleft, Mid, Midright, Right)
SensorLM = analogRead(A2);
SensorM = analogRead(A3);
SensorRM = analogRead(A4);
SensorR = analogRead(A5);
/* This is where the 'if' statements will be written that steer the car, something like this:
if(SensorM > 680){
Motor(0,0);}
-and so on. If the received value is above 680, the mid-sensor is on the black line it
is supposed to follow, so no corrections should be made. The rest of the steering controls will be similar,
they will all use the Motor() function to steer the vehicle around. I'm still thinking of implementing the potentiometer
value in the Motor() function, as the steering needs to happen faster when the car's going faster as well.
*/
}
I threw my code in as an attachment as well.
What I'm trying to do:
Make a car that follows a black line, by using infrared sensors to notice if it's nearing the white paper or on the black line. I'm using an array of 5 sensors and a potentiometer to control the speed at which the car moves.
What goes wrong:
So far, most of the problems have been solved. Although one wheel tends to rotate faster than the other depending which one receives it's write earlier. If the left wheel is written to first, it'll spin faster. I already tried replacing the motor driver, pwm pin,...
Overall, all that remains now is fully optimalizing my code, any ideas? Feel free to share!
EDIT: Thanks to CrossRoads, I now know that my board resets when the driver board requires peak power, This is the cause of the Motor(-120,-120) problem as well. Karma+!
The Arduino board that I'm using:
The driverboard for controlling the motors:
http://www.lctech-inc.com/Hardware/Detail.aspx?id=155c73e2-e972-473f-913d-ec443dbef0be
Thanks in advance people, I could really use the help of more experienced people than me as I'm new to programming.
Attachments: Code, schematic, pictures
Second_Line.ino (3.05 KB)