// Runaway Railcar - Reckoning Machine
// BMR 11-17-22
// Updated: BMR 12-30-22
const int temp = 3;
const int pass = 4;
const int cargo = 5;
const int steam = 6;
const int sos = 7;
const int lock = 8;
const int spd = A0;
const int oil = A1;
int ledMatrix[25] = { 22, 23, 24, 25, 26,
27, 28, 29, 30, 31,
32, 33, 34, 35, 36,
37, 38, 39, 40, 41,
42, 43, 44, 45, 46 };
int ledStep = 0;
int stepTime = 250;
unsigned long time;
unsigned long lastTime = 0;
// Function for setting all the pinModes; call at setup()
void setPins() {
for (int thisPin = 3; thisPin < 9; thisPin++) {
pinMode(thisPin, INPUT_PULLUP);
}
pinMode(spd, INPUT_PULLUP);
pinMode(oil, INPUT_PULLUP);
for (int thisPin = 22; thisPin < 47; thisPin++) {
pinMode(thisPin, OUTPUT);
}
pinMode(lock, OUTPUT);
}
// Wait function for use instead of delay()
void wait(int period) {
while (millis() < time + period) {
// Timer delay
}
}
// Check inputs; unlock if solved
void inspectorDetector() {
digitalRead(temp) == 1 && digitalRead(pass) == 1 &&
digitalRead(cargo) == 1 && digitalRead(steam) == 1 &&
digitalRead(sos) == 1 &&
analogRead(spd) > 850 && analogRead(spd) < 900 &&
analogRead(oil) > 125 && analogRead(oil) < 150 ?
digitalWrite(lock, LOW) : digitalWrite(lock, HIGH);
}
// Turn on a row of LEDs
void scanBar(int row) {
for (int i = 0; i < 25; i++) {
digitalWrite(ledMatrix[i], LOW);
}
for (int i = row; i < row + 5; i++) {
digitalWrite(ledMatrix[i], HIGH);
}
}
// Fire up the Matrix
void theMatrix() {
if (time > lastTime + stepTime) {
switch (ledStep) {
case 0:
scanBar(0);
ledStep++;
break;
case 1:
scanBar(5);
ledStep++;
break;
case 2:
scanBar(10);
ledStep++;
break;
case 3:
scanBar(15);
ledStep++;
break;
case 4:
scanBar(20);
ledStep = 0;
break;
}
lastTime = time;
}
}
// Debugging monitor
void debugger() {
// Serial.print("temp:");
// Serial.print("\t");
// Serial.print(digitalRead(temp));
// Serial.print("\t");
// Serial.print("Pass:");
// Serial.print("\t");
// Serial.print(digitalRead(pass));
// Serial.print("\t");
// Serial.print("cargo:");
// Serial.print("\t");
// Serial.print(digitalRead(cargo));
// Serial.print("\t");
// Serial.print("Steam:");
// Serial.print("\t");
// Serial.print(digitalRead(steam));
// Serial.print("\t");
// Serial.print("SOS:");
// Serial.print("\t");
// Serial.print(digitalRead(sos));
// Serial.print("\t");
Serial.print("Speed:");
Serial.print("\t");
Serial.print(analogRead(spd));
Serial.print("\t");
Serial.print("Oil:");
Serial.print("\t");
Serial.print(analogRead(oil));
Serial.print("\t");
Serial.print("Lock:");
Serial.print("\t");
Serial.print(digitalRead(lock));
Serial.print("\t");
Serial.print("Step:");
Serial.print("\t");
Serial.print(ledStep);
Serial.println();
wait(100);
}
void setup() {
Serial.begin(9600);
setPins();
digitalWrite(lock, HIGH);
}
// Ha I infiltrated your document!! Hahahaha
void loop() {
time = millis();
inspectorDetector();
theMatrix();
debugger();
}