Skip to content

Instantly share code, notes, and snippets.

@paulanthonywilson
Created December 29, 2022 13:30
Show Gist options
  • Save paulanthonywilson/30f9e21f629113a1b79a85975884cec6 to your computer and use it in GitHub Desktop.
Save paulanthonywilson/30f9e21f629113a1b79a85975884cec6 to your computer and use it in GitHub Desktop.
#define LIGHT_PIN 13
#define BUTTON_PIN 8
unsigned int lightState = LOW;
unsigned long toggleAt = 0;
unsigned long lastSwitch = 0;
unsigned long northRonWaitTimes[] = {750, 10000 - 750};
unsigned long startPointWaitTimes[] = {750, 750, 750, 20000 - 750 * 3};
unsigned long sumburghHeadWaitTimes[] = {750, 750, 750, 750, 750, 30000 - 750 * 4};
char names[][20] = {"North Ronaldsay", "Start Point", "Sumburgh Head"};
unsigned long *lightHouse[] = {northRonWaitTimes, startPointWaitTimes, sumburghHeadWaitTimes};
unsigned int lightHouseCount = 3;
unsigned int currentLightHouse = 0;
unsigned int waits[] = {2, 4, 6};
unsigned int waitTimeIx = 0;
int previousButtonState = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.write("somethingorother\n");
pinMode(LIGHT_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
checkButton();
if(millis() > toggleAt) {
toggle();
toggleAt = toggleAt + lightHouse[currentLightHouse][waitTimeIx];
waitTimeIx = (waitTimeIx + 1) % waits[currentLightHouse];
}
digitalWrite(LIGHT_PIN, lightState);
}
void toggle() {
Serial.println(names[currentLightHouse]);
if(lightState == HIGH){
lightState = LOW;
} else {
lightState = HIGH;
}
}
void checkButton() {
int buttonState = digitalRead(BUTTON_PIN);
if(HIGH == buttonState) {
digitalWrite(LIGHT_PIN, HIGH);
}
long now = millis();
if (LOW == buttonState && buttonState != previousButtonState && now - lastSwitch > 3000){
currentLightHouse = (currentLightHouse + 1) % lightHouseCount;
toggleAt = now + 1000;
waitTimeIx = 0;
lastSwitch = now;
lightState = LOW;
Serial.write("Switching to ");
Serial.println(names[currentLightHouse]);
}
previousButtonState = buttonState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment