need suggestion for modifying code......for 5 sensor array and two motors.....

i want to modify the following code into a grid following track code using 5 sensor array and two motors…please reply me…the modified progam

_5_senor.ino (4.98 KB)

What does the existing code do?

Is there any reason why I should use my time opening your attachment?

Why not post the code directly in the forum?

Where did you get the code?

What parts of it do you need to modify?

What have you tried and how did that work?

Are you starting with a project that is beyond your present capabilities? Should you be starting with something easier?

...R

#define leftCenterSensor   3
#define leftNearSensor     4
#define leftFarSensor      5
#define rightCenterSensor  2
#define rightNearSensor    1
#define rightFarSensor     0

int leftCenterReading;
int leftNearReading;
int leftFarReading;
int rightCenterReading;
int rightNearReading;
int rightFarReading;

#define leftMotor1  10
#define leftMotor2  9
#define rightMotor1 6
#define rightMotor2 5

void setup(){
  
  pinMode(leftCenterSensor, INPUT);
  pinMode(leftNearSensor, INPUT);
  pinMode(leftFarSensor, INPUT);
  pinMode(rightCenterSensor, INPUT);
  pinMode(rightNearSensor, INPUT);
  pinMode(rightFarSensor, INPUT);
    
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);

}

void loop(){
  readSensors();
  if(leftNearReading>750 && rightNearReading>750 && (leftCenterReading<750 || rightCenterReading<750)){
    straight();
  }
  else{
    stayOnLine();
  }
}
void readSensors(){
  
  leftCenterReading  = analogRead(leftCenterSensor);
  leftNearReading    = analogRead(leftNearSensor);
  leftFarReading     = analogRead(leftFarSensor);
  rightCenterReading = analogRead(rightCenterSensor);
  rightNearReading   = analogRead(rightNearSensor);
  rightFarReading    = analogRead(rightFarSensor);  
}

void stayOnLine(){
  if(leftNearReading>750 && leftCenterReading<750 && rightCenterReading<750 && rightNearReading<750){
    turnRight();
  }
  if(leftNearReading<750 && leftCenterReading<750 && rightCenterReading<750 && rightNearReading>750){
    turnLeft();
  }
  if(leftNearReading>750 && leftCenterReading<750 && rightCenterReading>750 && rightNearReading>750){
    turnLeft();
  }
  if(leftNearReading>750 && leftCenterReading>750 && rightCenterReading<750 && rightNearReading>750){
    turnRight();
  }
  if(leftNearReading<750 && leftCenterReading<750 && rightCenterReading<750 && rightNearReading<750){
    endmotion();
  }
}
  void turnLeft(){
    
     while(analogRead(rightCenterSensor)<750||analogRead(leftCenterSensor)<750){
     digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, HIGH);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(2);
     digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  }
    
  while(analogRead(rightCenterSensor)>750){
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, HIGH);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(2);
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  }
  }
    void turnRight(){

    
  while(analogRead(rightCenterSensor)<750){
     digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, HIGH);
    delay(2);
     digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  }
   while(analogRead(rightCenterSensor)>750){
     digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, HIGH);
    delay(2);
     digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  }
   while(analogRead(leftCenterSensor)>750){
     digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, HIGH);
    delay(2);
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  }
    }
  
  void straight(){
  if( analogRead(leftCenterSensor)>750){
    digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(1);
    digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(5);
    return;
  }
  if(analogRead(rightCenterSensor)>750){
    digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(1);
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(5);
    return;
  }
  
    digitalWrite(leftMotor1, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
    delay(4);
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
    delay(1);
  
}
  
  void endmotion(){
    digitalWrite(leftMotor1, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
  }

Already has 2 motors and 6 sensors. Which one is not to be used?

i am having 5 sensor array.so( left far,left near,middle,right near & right far) and two motor one left & right..so accordingly suggest me.....

So, get rid of leftCenter, call rightCenter Center, go from there...

please modify the code as u suggest ......i am not getting that.....

I think CR means like this. Just remove one, then you have 5, and rename the one which is now in the centre:

#define leftCenterSensor 3 <<<<<<<<<<<<<<<<<<<< remove this one
#define leftNearSensor 4
#define leftFarSensor 5
#define rightCenterSensor 2 <<<<<<<<<<<< rename this
#define rightNearSensor 1
#define rightFarSensor 0

Then do the same through the whole code…

i modified the code suggested by u but code is showing error in arduino …while verifying…please modify the code according to u…
the modified code is as follows :-

#define leftNearSensor 4
#define leftFarSensor 5
#define CenterSensor 2
#define rightNearSensor 1
#define rightFarSensor 0

int leftNearReading;
int leftFarReading;
int CenterReading;
int rightNearReading;
int rightFarReading;

#define leftMotor 10
#define rightMotor 5

void setup(){

pinMode(leftNearSensor, INPUT);
pinMode(leftFarSensor, INPUT);
pinMode(CenterSensor, INPUT);
pinMode(rightNearSensor, INPUT);
pinMode(rightFarSensor, INPUT);

pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);

}

void loop(){
readSensors();
if(leftNearReading>750 && rightNearReading>750 && ( CenterReading<750)){
straight();
}
else{
stayOnLine();
}
}
void readSensors(){

leftNearReading = analogRead(leftNearSensor);
leftFarReading = analogRead(leftFarSensor);
CenterReading = analogRead(CenterSensor);
rightNearReading = analogRead(rightNearSensor);
rightFarReading = analogRead(rightFarSensor);
}

void stayOnLine(){
if(leftNearReading>750 && CenterReading<750 && rightNearReading<750){
turnRight();
}
if(leftNearReading<750 && CenterReading<750 && rightNearReading>750){
turnLeft();
}
if(leftNearReading>750 && CenterReading>750 && rightNearReading>750){
turnLeft();
}
if(leftNearReading>750 && CenterReading<750 && rightNearReading>750){
turnRight();
}
if(leftNearReading<750 && CenterReading<750 && rightNearReading<750){
endmotion();
}
}
void turnLeft(){

while(analogRead(CenterSensor)<750{
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, HIGH);
delay(2);
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
delay(1);
}

while(analogRead(CenterSensor)>750){
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, HIGH);
delay(2);
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
delay(1);
}
}
void turnRight(){

while(analogRead(CenterSensor)<750){
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(2);
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
delay(1);
}
while(analogRead(CenterSensor)>750){
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(2);
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
delay(1);
}

void straight(){

if(analogRead(CenterSensor)>750){
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
delay(1);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
delay(5);
return;
}

digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
delay(4);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
delay(1);

}

void endmotion(){
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
}

There is a sticky at the top of the forum that you REALLY need to go read. It discusses how to ask a question here (what details to provide) AND how to post code. You are 0 for 2.

And, this is not a damned chat line. Spell ALL words out.

but code is showing error in arduino .....while verifying.

The trick is to provide us with all the information you're seeing.

Did you even try to solve the compile error*?

I compiled and get error:

sketch_feb11a.ino: In function 'void turnLeft()':
sketch_feb11a:66: error: expected `)' before '{' token

That tells you it expects a ) and if you look at the line it highlights in yellow you will see there are 2 of these: ( but only 1 of these: )

So you need to figure out where to put the missing one… The message basically tells you where it should go.

  • Alas, there are other errors once you fix that one, so you’ll need to work through each one.

EDIT… by the way your original code right at the top compiles clean (but you knew that already… this is your code not something you lifted from somewhere?) , so it seems you introduced these errors with your edits to implement the changes…

i am not getting that .......please tell me how to overcome that errors,,,,

Bhushan6:
i am not getting that …please tell me how to overcome that errors,

You mean you don’t get the same error? Or you don’t get how I’m explaining?

Ok here’s a freebie for that the first error, assumimg you get the same ones I do. Look at my attached pic and you’ll see that this line:

while(analogRead(CenterSensor)<750{

…needs a ) before the } which is exactly what the error says. So change it to this:

while(analogRead(CenterSensor)<750){

That error seems to have come from when you edited this original line:

while(analogRead(rightCenterSensor)<750||analogRead(leftCenterSensor)<750){

… and deleted the 2nd ) in the process.

Now when the compiler is happy with that it’s going to give you a new problem, which wasn’t there before you made the changes. When you get that new error, look at the lines above the new highlighted line which will be

void straight(){

… and see if you can see the difference between the old code and the new.

Fix that and it might compile…

error1.PNG

thank you very much …for suggesting…me i fix the errors all but please check once that code is correct or not… and tell me in which direction bot will go first …straight,left or right…

#define leftNearSensor     4
#define leftFarSensor      5
#define CenterSensor       2
#define rightNearSensor    1
#define rightFarSensor     0

int leftNearReading;
int leftFarReading;
int CenterReading;
int rightNearReading;
int rightFarReading;

#define leftMotor  10
#define rightMotor 5

void setup(){
  
  pinMode(leftNearSensor, INPUT);
  pinMode(leftFarSensor, INPUT);
  pinMode(CenterSensor, INPUT);
  pinMode(rightNearSensor, INPUT);
  pinMode(rightFarSensor, INPUT);
    
  pinMode(leftMotor, OUTPUT);
  pinMode(rightMotor, OUTPUT);

}

void loop(){
  readSensors();
  if(leftNearReading>750 && rightNearReading>750 && ( CenterReading<750)){
  }
  else{
    stayOnLine();
  }
}
void readSensors(){
  
  leftNearReading    = analogRead(leftNearSensor);
  leftFarReading     = analogRead(leftFarSensor);
  CenterReading      = analogRead(CenterSensor);
  rightNearReading   = analogRead(rightNearSensor);
  rightFarReading    = analogRead(rightFarSensor);  
}

void stayOnLine(){
  if(leftNearReading>750 && CenterReading<750 && rightNearReading<750){
    turnRight();
  }
  if(leftNearReading<750 && CenterReading<750 && rightNearReading>750){
    turnLeft();
  }
  if(leftNearReading>750 && CenterReading>750 && rightNearReading>750){
    turnLeft();
  }
  if(leftNearReading>750 && CenterReading<750 && rightNearReading>750){
    turnRight();
  }
  if(leftNearReading<750 && CenterReading<750 && rightNearReading<750){
  }
}
  void turnLeft(){
    
     while(analogRead(CenterSensor)<750){
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, HIGH);
    delay(2);
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, LOW);
    delay(1);
  }
    
  while(analogRead(CenterSensor)>750){
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, HIGH);
    delay(2);
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, LOW);
    delay(1);
  }
  }
    void turnRight(){

    
  while(analogRead(CenterSensor)<750){
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, LOW);
    delay(2);
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, LOW);
    delay(1);
  }
   while(analogRead(CenterSensor)>750){
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, LOW);
    delay(2);
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, LOW);
    delay(1);
  }
    
  {
  
  if(analogRead(CenterSensor)>750){
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, HIGH);
    delay(1);
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, HIGH);
    delay(5);
    return;
  }
  
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, HIGH);
    delay(4);
    digitalWrite(leftMotor, HIGH);
    digitalWrite(rightMotor, HIGH);
    delay(1);
  
}
  
  {
    digitalWrite(leftMotor, LOW);
    digitalWrite(rightMotor, LOW);
  }
    }

please check once that code is correct or not... and tell me in which direction bot will go first ...straight,left or right....

Nope, I think it's up to you now to build and implement, test the bot, see where it goes and de-bug your own logic.

We'll help with specific querstions: like I expect "this" and it does "that"....

I'd take another look at the sketch in reply #14, and figure out how you're going to debug it, blind. You will probably want to add some debug prints and run it tethered.