/*
LED bar graph for K.I.T.T. (Knight Rider)
20231116 by Luis Mateo
https://wokwi.com/arduino/projects/30982948935901570
This example code is in the public domain.
References:
https://playground.arduino.cc/Code/BitMath/
*/
// Constants
const int lampCount = 10;
int lampPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
// Lamp lighting table
// Caveat: #elements = sizeof(myarray)/sizeof(int);
const int barLength = 12;
int barStatus[] = {
0b0000000000,
0b0000000001,
0b0000000011,
0b0000000111,
0b0000001110,
0b0000011100,
0b0000111000,
0b0001110000,
0b0011100000,
0b0111000000,
0b1100000000,
0b1000000000
};
bool goingup
; //true means go up
int count;
void setup() {
goingup = true;
for (int i = 0; i < lampCount; i++) {
pinMode(lampPin[i], OUTPUT);
}
}
void loop() {
Show(count);
if (goingup) count++; else count--; // next step
if (goingup)
if (barLength == count) {
goingup = false;
count--; // avoid matrix limits
}
else
if (0 == count)
goingup = true;
delay(1000);
}
void Show(int cnt) {
for (int p = 0; p < lampCount; p++) {
digitalWrite(lampPin[p], bitRead(barStatus[cnt], p));
}
}