Arduino code sensor and moving wheel with encoder help with code

I'm still new at this but I been trying to figure this out for awhile now and can't seem to get it to work how I want it to. If  anyone can help or give advice on how I can go about doing this would be great. Thanks

#include <Servo.h>
      const int BUTTON = A5;              // push button for starting
      const int RIGHT_SERVO = 10;
      const int LEFT_SERVO = 11;
      Servo leftServo;
      Servo rightServo;
      int valButton;
      int turnOnOff=0;             // the val of the pull up button is set to zero to start with
     
      volatile int Lindex = 0;
     
      void setup()
      {
        attachInterrupt(0,ltEncoderInterrupt,RISING);
        Serial.begin(9600);
        leftServo.attach(LEFT_SERVO);
        rightServo.attach(RIGHT_SERVO);
        leftServo.write(1500);
        rightServo.write(1500);
      }
      void loop()
      { Serial.println(turnOnOff);
        turnOnOff=readButton();      // calls the function readButton to see if the button has been pressed(1)
        int sensorValue = analogRead(A0);
        if(turnOnOff==1)
        {
          if (sensorValue <300) //This robot moves on a white background
          {
          delay(500);       
          leftServo.write(1700);
          rightServo.write(1300);
                  
          }
          else if (sensorValue >300)
          {                     // It stop when it detect a black electrical tape.
          leftServo.write(1500);
          rightServo.write(1500); 
          Lindex = 0;
          delay(500);
            }
                               // can't seem to get it to work it keep moving backward
          {          
          }
          leftServo.write(1300);
          rightServo.write(1700);
         
          while (Lindex < 50) // this is how many tick i want it to move back with the encoder
          {          
          }
          turnOnOff = 0;
          Lindex = 0;
        }
       
        else
        {
          leftServo.write(1500);
          rightServo.write(1500);         
        }
       }
     
      /************* Subroutine for reading the button switch********************/
      int readButton()
      {
        unsigned long contactTime;               //local variable; contactTime declared
        valButton=analogRead(BUTTON);            // Read the pushbutton on an analog pin
        if(valButton>512)                        // Since 5V will produce a value of 1023, < 512 means button not pushed
         return turnOnOff;                       // return the value of turnOnOff without changing it
        contactTime=millis();                    // set contactTime = to the millis() clock value
        while(valButton<=512)
        {                   // while the pushbutton is pushed
          valButton=analogRead(BUTTON);           // read the button value again and keep reading until valButton is less than 512
        }
        if(millis()-contactTime<20)               // If the button is held for less than 20 ms
         return turnOnOff;                       // return the turnOnOff value unchanged
         return(1-turnOnOff);                      // if the button is held longer than 20 ms then change the turnOnOff
                                                 // to the opposite value to what it was
      }
      /////////////////////////////////////////////////////////////////////////////
     
      /******************ISR for wheel count**************************************/
      void ltEncoderInterrupt()
      {       
        Lindex++;
      }
      ///////////////////////////////////////////////////////////////////////////////

Downunder35m8 months ago

How can we help if you don't provide info on the actual problem?
You are not expecting that someone here builds your setup and uses a crystal ball to make sure all is correct? ;)