LED + BUTTON = DRINK

buttonPress1.jpg

I followed the book and used the sketch to keep on the led after a button press. Then, I added a random integer to serve as a counter that, when reached, would trigger the led. New drinking game?? I haven't, however, figured out how to change the random integer within the loop and keep the led on when the new random integer is reached. Here's the sketch: // turn led on when counter reaches random number // and keep on until pressed again

const int LED = 13; // the pin for the led const int BUTTON = 7; // the pin for the button int val = 0; // variable that stores state of input pin int old_val = 0; // stores previous value of "val" int state = 0; // 0 = LED off and 1 = LED on int randNumber = random(1,11); // choose a number between 1 and 10

void setup() { pinMode(LED, OUTPUT); // tell Arduino LED is an output pinMode(BUTTON, INPUT); // and BUTTON is an input }

void loop() { val = digitalRead(BUTTON); // read input value and store it

// check if the button was pressed again if ((val == HIGH) && (old_val == LOW)){ state = 1 + state; delay(10); }

old_val = val; // refresh old_val if (state == randNumber) { digitalWrite(LED, HIGH); // turn LED on } else { digitalWrite(LED, LOW); } }