Pages: [1]   Go Down
Author Topic: Button to turn blinking light on or off  (Read 490 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
      delay(200);
      digitalWrite(led, LOW);
    delay(200);
    }
  }
  if
}
any help would be greatly appreciated!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

NSW Australia
Offline Offline
Faraday Member
**
Karma: 80
Posts: 3234
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Pages: [1]   Go Up
Jump to: