I am working on a line follower robot as part of my Microelectronics project, an
ID: 662807 • Letter: I
Question
I am working on a line follower robot as part of my Microelectronics project, and am confused over what sort of code to use to program the "arduino uno" microcontroller I'm using. we tried this code, and there was no error in program(sketch)
but im not sure it is right code or not.
does anyone help me guys? please
you guys think it is working?
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int DIRECTION_PINA=12 ;
int VELOCITY_PINA=3;
int DIRECTION_PINB=13 ;
int VELOCITY_PINB=11;
void setup() {
Serial.begin(9600);
pinMode(DIRECTION_PINA, OUTPUT);
pinMode(VELOCITY_PINA, OUTPUT);
pinMode(DIRECTION_PINB, OUTPUT);
pinMode(VELOCITY_PINB, OUTPUT);
}
void loop() {
sensor();
driver();
}
void driver()
int lowVal = 60;
int highVal = 300;
int s0 = analogRead(A0);
int s1 = analogRead(A1);
int s2 = analogRead(A2);
int s3 = analogRead(A3);
////////////////////////////////////////////////
if ((s1 >= highVal) && (s2 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 255);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 255);
}
else if ((s2 >= highVal) && (s3 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 200);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 240);
}
else if ((s0 >= highVal) && (s1 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 240);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 200);
}
else if(s0 >= highVal)
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 240);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 0);
}
else if(s3 >= highVal)
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 0);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 240);
}
}
void sensor()
{
int s0 = analogRead(A0);
int s1 = analogRead(A1);
int s2 = analogRead(A2);
int s3 = analogRead(A3);
Serial.print("s0 ");
Serial.print(s0);
Serial.print(" s1 ");
Serial.print(s1);
Serial.print(" s2 ");
Serial.print(s2);
Serial.print(" s3 ");
Serial.print(s3);
Serial.print(' ');
Serial.println();
}
Explanation / Answer
yeah this code seems to be good.....
//here direction velocity declared
int DIRECTION_PINA=12 ;
int VELOCITY_PINA=3;
int DIRECTION_PINB=13 ;
int VELOCITY_PINB=11;
void setup() {
Serial.begin(9600);
pinMode(DIRECTION_PINA, OUTPUT);
pinMode(VELOCITY_PINA, OUTPUT);
pinMode(DIRECTION_PINB, OUTPUT);
pinMode(VELOCITY_PINB, OUTPUT);
}
void loop() {
sensor();
driver();
}
void driver() //as per instructions you are changing velocity using if-else condition
int lowVal = 60;
int highVal = 300;
int s0 = analogRead(A0);
int s1 = analogRead(A1);
int s2 = analogRead(A2);
int s3 = analogRead(A3);
////////////////////////////////////////////////
if ((s1 >= highVal) && (s2 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 255);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 255);
}
else if ((s2 >= highVal) && (s3 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 200);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 240);
}
else if ((s0 >= highVal) && (s1 >= highVal))
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 240);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 200);
}
else if(s0 >= highVal)
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 240);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 0);
}
else if(s3 >= highVal)
{
digitalWrite(DIRECTION_PINA, HIGH);
analogWrite(VELOCITY_PINA, 0);
digitalWrite(DIRECTION_PINB, HIGH);
analogWrite(VELOCITY_PINB, 240);
}
}
void sensor()
{
int s0 = analogRead(A0);
int s1 = analogRead(A1);
int s2 = analogRead(A2);
int s3 = analogRead(A3);
Serial.print("s0 ");
Serial.print(s0);
Serial.print(" s1 ");
Serial.print(s1);
Serial.print(" s2 ");
Serial.print(s2);
Serial.print(" s3 ");
Serial.print(s3);
Serial.print(' ');
Serial.println(); //this thing I am not sure
}
yes it works!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.