Created
March 21, 2023 04:00
-
-
Save lassombra/1203fe45333b5826a118f66be4b58f06 to your computer and use it in GitHub Desktop.
Requires the circuit layout from day 4.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <TimerOne.h> | |
#include <util/atomic.h> | |
#define LED1 10 // RED | |
#define LED2 11 // BLUE | |
#define LED3 12 // GREEN | |
#define SWITCH_1 2 | |
#define SWITCH_2 3 | |
#define SWITCH_3 4 | |
#define SLOW_INCREMENT 3 | |
bool eventRaised = false; | |
short skipped = 0; | |
uint8_t state = LED1; | |
bool isTriggered(); | |
void setLEDs(uint8_t led1, uint8_t led2, uint8_t led3); | |
void count() | |
{ | |
eventRaised = true; | |
} | |
void setup() | |
{ | |
pinMode(LED1, OUTPUT); | |
pinMode(LED2, OUTPUT); | |
pinMode(LED3, OUTPUT); | |
pinMode(SWITCH_1, INPUT); | |
pinMode(SWITCH_2, INPUT); | |
pinMode(SWITCH_3, INPUT); | |
pinMode(LED_BUILTIN, OUTPUT); | |
Timer1.initialize(500000); // .5 seconds | |
Timer1.attachInterrupt(count); | |
} | |
void writeSequence(bool enable) | |
{ | |
if (enable) | |
{ | |
setLEDs( | |
state == LED1 ? HIGH : LOW, | |
state == LED2 ? HIGH : LOW, | |
state == LED3 ? HIGH : LOW); | |
} | |
else | |
{ | |
setLEDs(HIGH, HIGH, HIGH); | |
} | |
} | |
void moveSequence(bool fast, bool reverse) | |
{ | |
if (fast || skipped == SLOW_INCREMENT) | |
{ | |
switch (state) | |
{ | |
case LED1: | |
state = reverse ? LED3 : LED2; | |
break; | |
case LED2: | |
state = reverse ? LED1 : LED3; | |
break; | |
case LED3: | |
state = reverse ? LED2 : LED1; | |
} | |
skipped = 0; | |
} | |
else | |
{ | |
skipped += 1; | |
} | |
} | |
void switchedDelay() | |
{ | |
bool triggered = isTriggered(); | |
if (triggered) | |
{ | |
moveSequence(digitalRead(SWITCH_1) == HIGH, digitalRead(SWITCH_3) == HIGH); | |
} | |
writeSequence(digitalRead(SWITCH_2) == HIGH); | |
} | |
void setLEDs(uint8_t led1, uint8_t led2, uint8_t led3) | |
{ | |
digitalWrite(LED1, led1); | |
digitalWrite(LED2, led2); | |
digitalWrite(LED3, led3); | |
} | |
bool isTriggered() | |
{ | |
bool triggered = false; | |
ATOMIC_BLOCK(ATOMIC_FORCEON) | |
{ | |
triggered = eventRaised; | |
eventRaised = false; | |
} | |
return triggered; | |
} | |
void loop() | |
{ | |
switchedDelay(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment