Skip to content
Snippets Groups Projects
Commit 3c0e703a authored by Malte Schmitz's avatar Malte Schmitz
Browse files

Add first simple game logic

parent 14237409
No related branches found
No related tags found
No related merge requests found
......@@ -45,43 +45,79 @@ void loop() {
scheduler.execute();
}
uint32_t color = 0xFFFFFF;
int position = 0;
int last_internal_button_state = HIGH;
int last_external_button_state = HIGH;
unsigned long last_internal_button_pressed = 0;
unsigned long last_external_button_pressed = 0;
#define STATE_PAUSE 0
#define STATE_FORWARD 1
#define STATE_BACKWARD 2
int state = STATE_PAUSE;
void show() {
for (int i = 0; i < NEOPIXEL_LEN; i++) {
pixels.setPixelColor(i, 0);
}
for (int i = position; i < position + 10; i++) {
pixels.setPixelColor(i, color);
if (state > STATE_PAUSE) {
pixels.setPixelColor(position, 0xFFFFFF);
}
pixels.show();
}
void handleButton() {
if (digitalRead(INTERNAL_BUTTON_PIN_B) == LOW) {
color = 0x0000FF;
} else if (digitalRead(EXTERNAL_BUTTON_PIN_NO) == LOW) {
color = 0xFF0000;
} else if (digitalRead(EXTERNAL_BUTTON_PIN_B) == LOW) {
color = 0x00FF00;
int current_internal_button_state = digitalRead(INTERNAL_BUTTON_PIN_B);
int current_external_button_state = digitalRead(EXTERNAL_BUTTON_PIN_B);
if (last_internal_button_state == HIGH && current_internal_button_state == LOW) {
last_internal_button_pressed = millis();
}
show();
if (last_external_button_state == HIGH && current_external_button_state == LOW) {
last_external_button_pressed = millis();
}
last_internal_button_state = current_internal_button_state;
last_external_button_state = current_external_button_state;
}
boolean forward = true;
void handleStep() {
if (forward) {
position = position + 1;
if (position > NEOPIXEL_LEN - 11) {
position = position - 2;
forward = false;
}
} else {
position = position - 1;
if (position < 0) {
position = 1;
forward = true;
}
unsigned long now = millis();
switch (state) {
case STATE_PAUSE:
if (now - last_internal_button_pressed < 100) {
position = 0;
state = STATE_FORWARD;
}
if (now - last_external_button_pressed < 100) {
position = NEOPIXEL_LEN - 1;
state = STATE_BACKWARD;
}
break;
case STATE_FORWARD:
position = position + 1;
if (position > NEOPIXEL_LEN - 1) {
if (now - last_external_button_pressed < 100) {
position = position - 2;
state = STATE_BACKWARD;
} else {
state = STATE_PAUSE;
}
}
break;
case STATE_BACKWARD:
position = position - 1;
if (position < 0) {
if (now - last_internal_button_pressed < 100) {
position = 1;
state = STATE_FORWARD;
} else {
state = STATE_PAUSE;
}
}
break;
}
show();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment