Question: Write an arduino code for a line follower with 5, CNY 70 sensors in the front. So here is what I was able to write
Write an arduino code for a line follower with 5, CNY 70 sensors in the front.
So here is what I was able to write up :
#include
void setup() // Built-in initialization block { Serial.begin(9600); pinMode(button, INPUT); pinMode(led, OUTPUT); pinMode(d7, INPUT); // Set right whisker pin to input pinMode(d6, INPUT); pinMode(d5, INPUT); // Set right whisker pin to input pinMode(d4, INPUT); pinMode(d3, INPUT); //delay(1000); // Delay to finish tone
servoLeft.attach(12); // Attach left signal to P12 servoRight.attach(13); // Attach right signal to P13 //forward(5000); // Go forward for 2 seconds //turnLeft(1500); // Turn left for 0.6 seconds //turnRight(600); // Turn right for 0.6 seconds //forward(2000); // go backward for 2 seconds disableServos(); // Stay still indefinitely } void loop() // Main loop auto-repeats { int sensor1 = digitalRead(d7); //Leftmost int sensor2 = digitalRead(d6); int sensor3 = digitalRead(d5); //middle int sensor4 = digitalRead(d4); int sensor5 = digitalRead(d3); //Rightmost
Serial.println(sensor5); //Serial.println(s5v); Serial.println(sensor4); //Serial.println(s4v); Serial.println(sensor3); //Serial.println(s3v); Serial.println(sensor2); //Serial.println(s2v); Serial.println(sensor1); //Serial.println(s1v);
Serialpforward(2000); delay(1000);
int buttonState = digitalRead(button);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(led, HIGH); } else { // turn LED off: digitalWrite(led, LOW); } delay(100); }
void forward(int time) // forward function { servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise servoRight.writeMicroseconds(1300); // Right wheel clockwise delay(time); // Maneuver for time ms }
void turnLeft(int time) // Left turn function { servoLeft.writeMicroseconds(1300); // Left wheel clockwise servoRight.writeMicroseconds(1300); // Right wheel clockwise delay(time); // Maneuver for time ms }
void turnRight(int time) // Right turn function { servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise servoRight.writeMicroseconds(1700); // Right wheel counterclockwise delay(time); // Maneuver for time ms }
void backward(int time) // backward function { servoLeft.writeMicroseconds(1300); // Left wheel clockwise servoRight.writeMicroseconds(1700); // Right wheel counterclockwise delay(time); // Maneuver for time ms }
void disableServos() // Halt servo signals { servoLeft.detach(); // Stop sending servo signals servoRight.detach(); }
Problems- movement function don't work when called in loop function and the led won't turn on even if i press the button
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
