loading

I need arduino code help.

I have a code for my robot and when i try to compile it I get an error.

error expected unqualified-id before 'if '

here is the code

// Connections to the Tumber PCB
//
// Arduino : Tumbler
// pin 9 ---- pin 6
// pin 10 --- pin 7
// pin 3 ---- pin 11
// pin 11 --- pin 10
// ground --- pin 2 (ground)
//
//



int inputVariable = HIGH; // sets input to high
int RightFwd = 3; // PWM Output to go Foward on Right Wheel
int RightRev = 11; // PWM Output to go Reverse on Right Wheel
int LeftFwd = 9; // PWM Output to go Forward on Left Wheel
int LeftRev = 10; // PWM Output to go Reverse on Left Wheel

int delaytime = 2000; // delay between steps
int speedval = 145; // speed forward and reverse, at 50% (of 255 max)



void setup()
{
pinMode(inputVariable, INPUT); // Sets the digital pin as input
inputVariable = digitalRead(2); // Right bump switch on pin 2
digitalWrite (inputVariable, HIGH); // Sets the pin to high
pinMode(RightFwd, OUTPUT);
digitalWrite(RightFwd, LOW); // Make sure Output is initally at LOW
pinMode(RightRev, OUTPUT);
digitalWrite(RightRev, LOW); // Make sure Output is initally at LOW
pinMode(LeftFwd, OUTPUT);
digitalWrite(LeftFwd, LOW); // Make sure Output is initally at LOW
pinMode(LeftRev, OUTPUT);
digitalWrite(LeftRev, LOW); // Make sure Output is initally at LOW


delay(delaytime);

}

void loop()
{



analogWrite(LeftRev, 0);
analogWrite(LeftFwd, speedval); // turns on the left wheel in forward direction at the speed value
analogWrite(RightRev, 0);
analogWrite(RightFwd, speedval); // turns on the right wheel in forward direction at the speed value

delay(delaytime);

analogWrite(LeftFwd, 0); // turns off the left wheel
analogWrite(RightFwd, 0); // turns off the right wheel

delay(delaytime);
}
if(inputVariable == LOW); }
analogWrite(LeftFwd, 0); // turns off the left wheel
analogWrite(RightFwd, 0); // turns off the right wheel
analogWrite(LeftRev, 0); // turns off left wheel
analogWrite(RightRev, 0); // turns off right wheel
analogWrite(LeftRev, speedval); // turns on left wheel rev
analogWrite(RightRev, speedval); // turns on right wheel rev
delay(800); // 800 milliseconds
analogWrite(LeftRev, 0); // turns off left wheel
analogWrite(RightRev, 0); // turns off right wheel
analogWrite(LeftFwd, speedval); // turns on left wheel
analogWrite(RightFwd, speedval); // turns on right wheel
}

I bolded the line that the error comes up on

sort by: active | newest | oldest
Willard2.06 years ago
I'm not an expert on Arduino, but I think that anything outside 'void loop()' will not run continuously. If you want to check if inputvariable ==LOW each loop of the code, your code should read as follows

//my changes are in bold

void loop()
{
  analogWrite(LeftRev, 0);
  analogWrite(LeftFwd, speedval); // turns on the left wheel in forward      direction at the speed value
  analogWrite(RightRev, 0);
  analogWrite(RightFwd, speedval); // turns on the right wheel in forward direction at the speed value

  delay(delaytime);

  analogWrite(LeftFwd, 0); // turns off the left wheel
  analogWrite(RightFwd, 0); // turns off the right wheel

  delay(delaytime);
              // removed this  }
if(inputVariable == LOW)  // you may need a ; here, I'm not sure
{
  analogWrite(LeftFwd, 0); // turns off the left wheel
  analogWrite(RightFwd, 0); // turns off the right wheel
  analogWrite(LeftRev, 0); // turns off left wheel
  analogWrite(RightRev, 0); // turns off right wheel
  analogWrite(LeftRev, speedval); // turns on left wheel rev
  analogWrite(RightRev, speedval); // turns on right wheel rev
  delay(800); // 800 milliseconds
  analogWrite(LeftRev, 0); // turns off left wheel
  analogWrite(RightRev, 0); // turns off right wheel
  analogWrite(LeftFwd, speedval); // turns on left wheel
  analogWrite(RightFwd, speedval); // turns on right wheel
  }
}

TOCO (author)  Willard2.06 years ago
A pin came unsoldered I fixed it. Now when I upload the code both motors go in reverse. I tried a test code of it going forword and then back then forword and so on. The bump switch isnt working either but it is all soldered right. THere is a resistor in with the switch. I will check if the resistor is too large.
TOCO (author)  Willard2.06 years ago
the error went away. When I upload the code only the left wheel spins. I followed the instructable below. https://www.instructables.com/id/Robot-Platform-including-h-bridges-from-10-RC-Ca/ I added the bumpswitch so if it bumped into something it would back up and turn right. The leds in the right wheel light up but the wheel doesnt spin. Here is the code. I checked the wiring but I will double check it. // Arduino : Tumbler // pin 9 ---- pin 6 // pin 10 --- pin 7 // pin 3 ---- pin 11 // pin 11 --- pin 10 // ground --- pin 2 (ground) // // int inputVariable = HIGH; // sets input to high int RightFwd = 3; // PWM Output to go Foward on Right Wheel int RightRev = 11; // PWM Output to go Reverse on Right Wheel int LeftFwd = 9; // PWM Output to go Forward on Left Wheel int LeftRev = 10; // PWM Output to go Reverse on Left Wheel int delaytime = 2000; // delay between steps int speedval = 145; // speed forward and reverse, at 50% (of 255 max) void setup() { pinMode(inputVariable, INPUT); // Sets the digital pin as input inputVariable = digitalRead(2); // Right bump switch on pin 2 digitalWrite (inputVariable, HIGH); // Sets the pin to high pinMode(RightFwd, OUTPUT); digitalWrite(RightFwd, LOW); // Make sure Output is initally at LOW pinMode(RightRev, OUTPUT); digitalWrite(RightRev, LOW); // Make sure Output is initally at LOW pinMode(LeftFwd, OUTPUT); digitalWrite(LeftFwd, LOW); // Make sure Output is initally at LOW pinMode(LeftRev, OUTPUT); digitalWrite(LeftRev, LOW); // Make sure Output is initally at LOW delay(delaytime); } void loop() { analogWrite(LeftRev, 0); analogWrite(LeftFwd, speedval); // turns on the left wheel in forward direction at the speed value analogWrite(RightRev, 0); analogWrite(RightFwd, speedval); // turns on the right wheel in forward direction at the speed value if (inputVariable == LOW); { analogWrite(LeftFwd, 0); // turns off the left wheel analogWrite(RightFwd, 0); // turns off the right wheel analogWrite(LeftRev, 0); // turns off left wheel analogWrite(RightRev, 0); // turns off right wheel analogWrite(LeftRev, speedval); // turns on left wheel rev analogWrite(RightRev, speedval); // turns on right wheel rev delay(800); // 800 milliseconds analogWrite(LeftRev, 0); // turns off left wheel analogWrite(RightRev, 0); // turns off right wheel analogWrite(LeftFwd, speedval); // turns on left wheel analogWrite(RightFwd, speedval); // turns on right wheel } }
ANd please don't keep posting new questions which are only additions to the ones you've already asked.
That's probably my fault for suggesting the author post a followup question. I should have made clear that he could/should do so by posting a reply to his original question. Blame me, not Toco..
TOCO (author) 6 years ago
I tried a different switch and nothing happens.
I reckon Willard has it. The brace is the wrong way round.
NachoMahma6 years ago
.  I don't know squat about Arduino programming, but it looks to me like the brace after the highlighted if statement needs to be reversed. Ie, change "}" to "{".
kcls6 years ago
What was wrong with the answers to your last question?