Go Down

Topic: Button to turn blinking light on or off (Read 1 time) previous topic - next topic


Hi, im pretty new to arduino, and programming in general, and i just cant figure out this code.  I am tryinng to make a code that takes imput from two buttons (pins 8(left) and 9(right), an has one led (pin 13). when the program starts the lights is turned off. if you click the left button (push and release) it starts the led blinking 5 times a second, and stops blinking when you click(push and release) the right button. so click the left one starts the blinking and clicking the right one stops it.

this is the best i could come up with, im afraid its not much
Code: [Select]
int led = 13;
const int buttonPinLeft = 9;
const int buttonPinRight = 8;
int state;

void setup()
  pinMode(buttonPinLeft, INPUT);
  pinMode(buttonPinRight, INPUT);
  pinMode(led, OUTPUT);
  state = 0;
void loop()
  if(state == 1)
    for(int i=0; i<5; i++)
      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

any help would be greatly appreciated!


Two hints. :) Try to figure out the rest ;)
if (digitalRead(buttonPinLeft) ==  HIGH) state = 1;
if (digitalRead(buttonPinRight) == HIGH) state = 0;


before you start coding with buttons, study carefully the coding for debounce (noting that for some really perverse reason, that tutorial shows the switch connected "upside down" to the usual and recommended convention which has the button switching to ground and the internal pull-up of the Arduino enabled).

Go Up