Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.
int leftInput=A0;
int rightInput=A1;
int leftMotor=11;
int rightMotor=10;
int leftValue = 0;
int rightValue = 0;
void setup() {
  pinMode (leftMotor, OUTPUT);
  pinMode (rightMotor, OUTPUT);

}

void loop() {
  leftValue = analogRead (leftInput);
  rightValue= analogRead (rightInput);

  // Part 1
  if ( leftValue < 400 && rightValue < 400)
  {
    digitalWrite (leftMotor, HIGH);
    digitalWrite (rightMotor, HIGH);
  }
  else if ( leftValue < 400 && rightValue > 800 )
  {
    digitalWrite (leftMotor, HIGH);
    digitalWrite (rightMotor, LOW);
  }
  else if ( leftValue > 800 && rightValue < 400)
  {
    digitalWrite (leftMotor, LOW);
    digitalWrite (rightMotor, HIGH);
  }

  // Part 2
  if ( leftValue > 400 && rightValue > 400 && leftValue < 500 && rightValue < 500) 
  {
    analogWrite(leftMotor, 255);
    analogWrite(rightMotor, 255);
  }

  if ( leftValue > 400 && rightValue < 400 && leftValue < 500) 
  {
    analogWrite(leftMotor, 191);
    analogWrite(rightMotor, 191);
  }

  if ( leftValue < 400 && rightValue > 400 && rightValue < 500) 
  {
    analogWrite(leftMotor, 127);
    analogWrite(rightMotor, 127);
  }  

}

In the above Arduino code, Part 2 (Speed of the motors) may have all the 3 conditions false, but I need to make my robot run with the previously know conditions of the Part 2. ie. if

  analogWrite(leftMotor, 127);
  analogWrite(rightMotor, 127);

was the previous decided condition, then even the if the part 1 changed multiple times, the above code should work. Please provide me the solution. Thanks. :)

share

migration rejected from electronics.stackexchange.com Aug 10 at 13:12

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as unclear what you're asking by Nick Gammon, fuenfundachtzig, LoganBlades, Annonomus Penguin Aug 10 at 13:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Why are you doing digitalWrite (leftMotor, HIGH) and then later analogWrite(leftMotor, 255) ? I think you should decide if you want to do PWM writes or not, and not mix them up like that. Your question is unclear, please explain in more detail. –  Nick Gammon Jul 23 at 3:25

Browse other questions tagged or ask your own question.