Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int button = 12;
- const int pin1 = 3;//middle led
- const int pin2 = 4;//middle layer outer two
- const int pin3 = 6;//bottom layer
- const int pin4 = 5;//top layer
- int randi;
- int readValue = 0;
- boolean lastbutton = false;
- boolean currentbutton = false;
- void setup()
- {
- randomSeed(A0);//allows me to give a range instead of being limited to one number
- pinMode (button, INPUT);
- pinMode (pin1, OUTPUT);
- pinMode (pin2, OUTPUT);
- pinMode (pin3, OUTPUT);
- pinMode (pin4, OUTPUT);
- Serial.begin (9600);
- }
- boolean debounce (boolean lastbutton)
- {
- boolean currentbutton = digitalRead (button);
- if (lastbutton != currentbutton)
- {
- delay (5);
- currentbutton = digitalRead (button);
- }
- return currentbutton;
- }
- void loop()
- {
- currentbutton = debounce (lastbutton);//this is used so that the LED will stay on without having to hold down the button
- if (currentbutton == true && lastbutton == false)
- {
- randi = random (1,7);//this gives me a random number from 1-7 (1 is included but 7 is not): could also do random (6)+1
- Serial.println (randi);
- digitalWrite (pin1, HIGH);
- digitalWrite (pin2, HIGH);
- digitalWrite (pin3, HIGH);
- digitalWrite (pin4, HIGH);
- delay (50);
- digitalWrite (pin1, LOW);
- digitalWrite (pin2, LOW);
- digitalWrite (pin3, LOW);
- digitalWrite (pin4, LOW);
- delay (10);
- digitalWrite (pin1, HIGH);
- digitalWrite (pin2, HIGH);
- digitalWrite (pin3, HIGH);
- digitalWrite (pin4, HIGH);
- delay (50);
- digitalWrite (pin1, LOW);
- digitalWrite (pin2, LOW);
- digitalWrite (pin3, LOW);
- digitalWrite (pin4, LOW);
- }
- switch (randi)//this gives me cases that correlate to the number generated by the rani
- {
- case 1://if number is 1 only the middle LED will turn on
- digitalWrite (pin1, HIGH);
- break;
- case 2://if number is 2 the middle outside LED's will turn on
- digitalWrite (pin2, HIGH);
- break;
- case 3://if number is 3 the middle row LED's will turn on
- digitalWrite (pin1, HIGH);
- digitalWrite (pin2, HIGH);
- break;
- case 4: //if number is 4 the top and bottom layers of LED's will turn on
- digitalWrite (pin3, HIGH);
- digitalWrite (pin4, HIGH);
- break;
- case 5://if number is 5 the top and bottom as well as the middle LED will turn on
- digitalWrite (pin3, HIGH);
- digitalWrite (pin4, HIGH);
- digitalWrite (pin1, HIGH);
- break;
- case 6://if number is 6 every LED will be on EXCEPT for the middle LED
- digitalWrite (pin3, HIGH);
- digitalWrite (pin4, HIGH);
- digitalWrite (pin2, HIGH);
- break;
- default://if there is no number then no LED will be turned on
- digitalWrite (pin1, LOW);
- digitalWrite (pin2, LOW);
- digitalWrite (pin3, LOW);
- digitalWrite (pin4, LOW);
- break;
- }
- lastbutton = currentbutton;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement