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. :)
digitalWrite (leftMotor, HIGH)
and then lateranalogWrite(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