Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Arduino LED game
- * by: Michael Schuppenies
- * http://www.ezplugins.de/
- *
- * Have fun, drink responsibly, etc.
- */
- const int latchPin = 8;
- const int clockPin = 12;
- const int dataPin = 11;
- const int buttonPin = 2;
- int button_state = 0;
- int button_state_last = 0;
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- byte leds = 0;
- unsigned long int timer = 0;
- int timer_update = 200;
- int current_anim_step = 0;
- int anim_length;
- int delay_mode[4];
- int mode = 3; // 0 = easy, mid = 1, hard = 2, wtf = 3
- int r;
- int state = 0; // 0 = menu, 1 = start, 2 = running, 3 = end
- int state_last = -1;
- const int STATE_MENU = 0;
- const int STATE_START = 1;
- const int STATE_RUNNING = 2;
- const int STATE_END = 3;
- int game_led_win;
- int game_led;
- int game_leds = 8;
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Setup");
- pinMode(latchPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(buttonPin, INPUT);
- delay_mode[0] = 300; // easy
- delay_mode[1] = 150; // mid
- delay_mode[2] = 100; // hard
- delay_mode[3] = 50; // wtf
- r = 0;
- }
- void loop()
- {
- button_update();
- if (state_last != state) {
- current_anim_step = 0;
- r = 0;
- switch (state) {
- case 1: game_start(); break;
- case 2: game_run(); break;
- case 3: game_end(); break;
- default: game_menu(); break;
- }
- state_last = state;
- }
- if ((unsigned long) (millis() - timer) >= timer_update) {
- timer_update = 400;
- switch (state) {
- case 1: game_start_update(r); break;
- case 2: timer_update = delay_mode[mode]; game_run_update(r); break;
- case 3: game_end_update(r); break;
- default: game_menu_update(r); break;
- }
- r++;
- timer = millis();
- updateShiftRegister();
- }
- }
- void animate(int anim_array[]) {
- if (current_anim_step < anim_length) {
- int tmp_led = anim_array[current_anim_step];
- if (tmp_led == -1) {
- all_off();
- }
- else {
- update_led(anim_array[current_anim_step]);
- }
- current_anim_step++;
- }
- else {
- current_anim_step = 0;
- all_off();
- }
- }
- void game_menu() {
- }
- void game_menu_update(int rr) {
- int anim[] = { 0, 1, 2, 7, 6, 5, 3, 4 };
- anim_length = sizeof(anim) / sizeof(anim[0]);
- animate(anim);
- }
- void game_start() {
- game_led_win = 0;
- game_led = 7;
- }
- void game_start_update(int rr) {
- int anim[] = { 7, -1, 7, -1, 7, -1, 7, -1 };
- anim_length = sizeof(anim) / sizeof(anim[0]);
- animate(anim);
- if (rr == 9) {
- state = STATE_RUNNING;
- }
- }
- void game_run() {}
- void game_run_update(int rr) {
- if (game_led == 0) {
- game_led = game_leds - 1;
- }
- else {
- game_led--;
- }
- all_off();
- update_led(game_led);
- //log(String(game_led));
- }
- void game_end() {}
- void game_end_update(int rr) {
- if (game_led == game_led_win) {
- int anim[] = { game_led };
- anim_length = 1;
- animate(anim);
- }
- }
- void update_led(byte led) {
- leds = bitSet(leds, led);
- }
- void all_off() {
- leds = 0;
- updateShiftRegister();
- }
- void updateShiftRegister() {
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, LSBFIRST, leds);
- digitalWrite(latchPin, HIGH);
- }
- void button_press() {
- if (state == STATE_MENU) {
- state = STATE_START;
- }
- else if (state == STATE_RUNNING) {
- state = STATE_END;
- }
- else if (state == STATE_END) {
- state = STATE_MENU;
- all_off();
- }
- }
- void button_release() {
- }
- void button_update() {
- int button_read = digitalRead(buttonPin);
- // If the switch changed, due to noise or pressing:
- if (button_read != button_state_last) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (button_read != button_state) {
- button_state = button_read;
- if (button_state == HIGH) {
- button_press();
- }
- else {
- button_release();
- }
- }
- }
- button_state_last = button_read;
- }
- void log(String str) {
- Serial.println(String(millis()) + ": " + str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement