Arduino Uno on Arduino IDE:
Hi, I'm trying to increase the variable 'sspeed' by 0.01 when button1 is pressed, and decrease it by 0.01 when button2 is pressed.
Currently it won't work. I know its not the connections to the arduino, as I've tried serial printing 'b1' which returns 0 or 1 depending on weather its low or high. So I'm guessing I've done something wrong in code.
My code is below:
float sspeed = 0.00;
void setup()
{
Serial.begin(9600);
//(the buttons are 2 pin)
pinMode(2, INPUT_PULLUP); //button1
pinMode(3, INPUT_PULLUP); //button2
}
void loop()
{
int b1 = digitalRead(2);
int b2 = digitalRead(3);
Serial.println(sspeed);
if (b1 = LOW) sspeed = sspeed + 0.01;
if (b2 = LOW) sspeed = sspeed - 0.01;
}
Hope you can help, thanks.
b1 = low
is an assignment. This will always set b1 to low and also evaluate to low which happens to be 0 which happens to be interpeted as false. b1 == low
is probably the comparison you want. Once you fix this you will notice that this code will "autorepeat" to fast. The next thing that you will learn is button bounce. You may want to read what to do about this on the Arduino pages: http://playground.arduino.cc/code/bounce