My program is moving the wheels even though I haven't given any commands for it

#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 sensor1; //Rightmost
int sensor2;
int sensor3; //middle
int sensor4;
int sensor5;

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);

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(2000);
//disableServos();
// if(sensor3 == LOW)
//{ forward(100); }
//if(sensor5 == LOW)
//{turnLeft(1000);}

//forward(2000);turnLeft(1000);forward(4000);turnRight(1000);disableServos();

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();
}

Well this is my code and even when i don’t call any movement functions in the loop() and setup() it still moves. I have boe shield bot with 5 CNY 70 sensors .

How is the input pin wired ?

Have you a pulldown resistor in place to keep the pin LOW until the button is pressed or is it floating at an unknown, possibly HIGH, voltage ?

Consider using INPUT_PULLUP in pinMode to activate the built in pullup resistor and change the wiring and program logic so that when pressing the button to take the input to GND, ie LOW, it is detected and acted upon

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

I can't see any reference to motors so I'm having to guess that the "servos" are continuous rotation servos used as motors not normal servos. Correct?

When you attach() a servo it is initialised to the equivalent of write(90). I imagine that value is causing the movement you're seeing.

Steve

int sensor1;          //Rightmost
int sensor2;
int sensor3;          //middle
int sensor4;
int sensor5;

You’ll want to lose those.

Please remember to use code tags when posting code

Sorry for the inconvenience of me not using code tags i am very new to this forum so i will keep that in mind from now.
→ Yes I used input_pullup but it didn’t help much.
→ Yes i believe that initialized value is causing it too… how can i sort it out ?
→ Below is my full code…Is there anyway I can put the section off line follower in a function outside the loop? I tried to do by creating a function outside but I think its the issue of scope due to digitalread which is in loop()… is there anyway i can do that ?

#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.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(1500);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1454);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
}
void disableServos()                         // Halt servo signals
{                                            
  servoLeft.detach();                      // Stop sending servo signals
  servoRight.detach(); 
}

You need to work out what value causes the servos to stop turning. It is likely to be somewhere close to write(90) or writeMicroseconds(1500) which are roughly the same thing. For that I usually use a potentiometer and the Knob example with a serial write() added to write the current value to the monitor.

Then you can put a write() with that value immediately BEFORE the attach(). That will override the default 90 value.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.