Well here is my code :
#include <Servo.h> // Include servo library
Servo servoLeft; // Declare left and right servos
Servo servoRight;
int button=2;
int led=11;
int d3=3;
int d4=4;
int d5=5;
int d6=6;
int d7=7;
int s1=0;
int s2=0;
int s3=0;
int s4=0;
int s5=0;
void setup() // Built-in initialization block
{
Serial.begin(9600);
pinMode( button, INPUT_PULLUP);
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);
servoLeft.writeMicroseconds(1480); // Left wheel counterclockwise
servoRight.writeMicroseconds(1472);
servoLeft.attach(12); // Attach left signal to P12
servoRight.attach(13); // Attach right signal to P13
//disableServos(); // Stay still indefinitely
}
void loop() // Main loop auto-repeats
{
int sensor1 = digitalRead(d7); //Rightmost
int sensor2 = digitalRead(d6);
int sensor3 = digitalRead(d5); //middle
int sensor4 = digitalRead(d4);
int sensor5 = digitalRead(d3); //Lefttmost
//Serial.println(sensor5);
//Serial.println(sensor4);
//Serial.println(sensor3);
//Serial.println(sensor2);
//Serial.println(sensor1);
//forward();delay(2000);
if(sensor2==LOW){turnRight();} //--->line follower segment begins
if(sensor4==LOW) {turnLeft();}
if(sensor2==LOW && sensor3==LOW ){turnRight();}
if(sensor4==LOW && sensor3==LOW ){turnLeft();}
if(sensor3==LOW) {forward();} //----> line follower segment ends
//delay(1000);
// if(sensor1==LOW){ ++s1;}
// if(sensor2==LOW){ ++s2;}
//if(sensor3==LOW){ ++s3;}
//if(sensor4==LOW){ ++s4;}
// if(sensor5==LOW){ ++s5;}
//forward(2000);turnLeft(1000);forward(4000);turnRight(1000);disableServos();
//disableServos();
//if(s5==0){turnRight();}
//if(s3=1){forward();}
formatPrint("s5=",s5);
formatPrint("s4=",s4);
formatPrint("s3=",s3);
formatPrint("s2=",s2);
formatPrint("s1=",s1);
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() // forward function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1300); }
// Right wheel clockwise
//delay(time); // Maneuver for time ms
void turnLeft() // Left turn function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
//delay(time); // Maneuver for time ms
}
void turnRight() // Right turn function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
//delay(time); // Maneuver for time ms
}
void backward() // backward function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
//delay(time); // Maneuver for time ms
}
void formatPrint( char *leftStr, int MyVar1){
Serial.println (leftStr);
Serial.println (MyVar1);
}
void stay() // Right turn function
{
servoLeft.writeMicroseconds(1480); // Left wheel counterclockwise
servoRight.writeMicroseconds(1472); // Right wheel counterclockwise
//delay(time); // Maneuver for time ms
}
void disableServos() // Halt servo signals
{
servoLeft.detach(); // Stop sending servo signals
servoRight.detach();
}
So my question is that Can ii make a function for the line follower outside the loop and call it ?..It tried to do that but it won’t work properly…I think mainly because of the digitalRead part which is being done in the loop… So is there anyway around it ?
p.s…-Please ignore the commented lines it’s just for my reference.