Skip to content

Instantly share code, notes, and snippets.

@lukKowalski
Last active November 25, 2020 16:06
Show Gist options
  • Save lukKowalski/afe598a4834ad06ea37542965ce57538 to your computer and use it in GitHub Desktop.
Save lukKowalski/afe598a4834ad06ea37542965ce57538 to your computer and use it in GitHub Desktop.
const int lamp_0_0 = 5;
const int lamp_0_1 = 6;
const int lamp_0_2 = 7;
const int lamp_0_3 = 8;
const int lamp_0_4 = 9;
const int lamp_0_5 = 10;
const int lamp_1_0 = 11;
const int lamp_1_1 = 12;
const int sensor0 = 2;
const int sensor1 = 3;
const int sensor2 = 4;
const int lampTurnOnDelay = 100; //time in ms
const int lampTurnOffDelay = 1000;
struct lamp {
bool isTurnedOn = false;
bool shouldTurnOn = false;
unsigned long turnOnAt;
int pin;
};
lamp createLamp (int pin) {
lamp l;
l.pin = pin;
pinMode(pin, OUTPUT);
return l;
}
lamp allLamps[8] = {
createLamp(lamp_0_0),
createLamp(lamp_0_1),
createLamp(lamp_0_2),
createLamp(lamp_0_3),
createLamp(lamp_0_4),
createLamp(lamp_0_5),
createLamp(lamp_1_0),
createLamp(lamp_1_1)
};
int sensorCounters[3] = {0, 0, 0};
void delayedTurnOnSingleLamp (int i, int idx) {
if (!allLamps[i].shouldTurnOn) {
allLamps[i].turnOnAt = millis() + idx * lampTurnOnDelay;
allLamps[i].shouldTurnOn = true;
} else if (allLamps[i].isTurnedOn) {
allLamps[i].turnOnAt = millis() + idx * lampTurnOnDelay;
}
}
// 0 - normal, 1 - reversed
void turnOnLampsSet (int from, int to, int mode = 0) {
for(int i=from; i<=to; i++) {
delayedTurnOnSingleLamp(i, mode == 0 ? (i - from) : (to - i));
}
}
void checkLamps () {
unsigned long now = millis();
for (int i = 0; i < 8; i++) {
if (now >= allLamps[i].turnOnAt) {
if (!allLamps[i].isTurnedOn && allLamps[i].shouldTurnOn) {
digitalWrite(allLamps[i].pin, HIGH);
allLamps[i].isTurnedOn = true;
}
}
if (now >= (allLamps[i].turnOnAt + lampTurnOffDelay)) {
if (allLamps[i].isTurnedOn) {
digitalWrite(allLamps[i].pin, LOW);
allLamps[i].isTurnedOn = false;
allLamps[i].shouldTurnOn = false;
}
}
}
}
void debugLamps () {
for (int i = 0; i < 8; i++) {
Serial.print("Debugging lamp: ");
Serial.println(allLamps[i].pin);
Serial.print("turnOnAt: ");
Serial.println(allLamps[i].turnOnAt);
Serial.print("isTurnedOn: ");
Serial.println(allLamps[i].isTurnedOn ? "1" : "0");
Serial.print("shouldTurnOn: ");
Serial.println(allLamps[i].shouldTurnOn ? "1" : "0");
}
}
void setup() {
pinMode(sensor0, INPUT);
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
Serial.begin(9600);
}
int sensorResetCounter = 0; //reset at 100
void loop() {
if (digitalRead(sensor0) == HIGH) {
sensorCounters[0]++;
if (sensorCounters[0] >= 3) {
Serial.println("running sensor0");
turnOnLampsSet(0, 5);
sensorCounters[0] = 0;
}
}
if (digitalRead(sensor1) == HIGH) {
sensorCounters[1]++;
if (sensorCounters[1] >= 3) {
Serial.println("running sensor1");
turnOnLampsSet(0, 5, 1);
turnOnLampsSet(6, 7);
sensorCounters[1] = 0;
}
}
if (digitalRead(sensor2) == HIGH) {
sensorCounters[2]++;
if (sensorCounters[2] >= 3) {
Serial.println("running sensor2");
sensorCounters[2] = 0;
turnOnLampsSet(6, 7, 1);
}
}
sensorResetCounter++;
if (sensorResetCounter >= 100) {
sensorCounters[0] = 0;
sensorCounters[1] = 0;
sensorCounters[2] = 0;
sensorResetCounter = 0;
}
checkLamps();
delay(30);
//debugLamps();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment