Some time ago, I published a project using 7 keys and 7 LEDs which is commonly shown at various places. Numbers which are devisable by 3 as 6, 9, and 12 will not work. So 10 pins for the buttons plus 10 pins for the LEDs should be possible and actually reaches the very limit of ARDUINO UNO pins when you combine the 14 digital and 6 analog pins, including the pins used for serial communication, loosing any debug facilities.
Sidenote: I usually begin sketches with Serial.println(__FILE__) which enables me to detect which sketch actually is running, so I have to use Serial.end() in order to make use of pins 0 and 1.
If I wanted to keep pins 0 and 1 for Serial and not using a MEGA or DUE, there are some options: you might use a NANO which offers analog pins A6 and A7 and check them using analogRead and test for zero. You will need external pullup resistors, otherwise you might get bouncing errors. Or you could use an ARDUINO UNO or ARDUINO NANO where the mcu is a ATmega328PB (watch the "B") instead of the normal ATmega328P where A6 and A7 can be used as digital pins. There are some UNO clones equipped with these extra pins.
So, misusing pins 0 and 1 makes addressing very easy: the even pin numbers go to the buttons and the odd pin numbers go to the LEDs.
I used this type of combined buttons and LEDs which are available in five different colors:
but you can also use separate keys and LEDs.
Instead of using plexiglass you can use an old 7" vinyl record which also spares you to saw the circle.
The schematic is very easy: the cathodes of the LEDS goes to GND, the anodes to one of the pins with an odd number, the buttons go to GND and a pin with an even number.
To mount everything together, you can use an old vinyl record if you manage to find one, otherwise get a piece of cardboard or whatever iies around.
/*
The exhibit shown in many phaenoments
especially the Mathematikum in Giessen
(Germany) realized using ARDUINO UNO
and 10 buttons and 10 LEDs.
*/
const int N = 10;
boolean b[N]; // the states of the LEDs
boolean firstRun = true;
void setup() {
Serial.begin(9600);
Serial.print("\n" __FILE__ " "
__TIME__ "\nrnd = ");
int rnd = analogRead(A1) +
analogRead(A6);
Serial.println(rnd);
Serial.flush();
Serial.end();
randomSeed(rnd);
newGame();
}
void loop() {
// which button was pressed (if any)
int k = getButton();
// none?
if (k < 0) return;
// toggle the three LEDs:
toggle(b[k]);
if (k > 0) toggle(b[k - 1]);
else toggle(b[N - 1]);
if (k < N - 1) toggle(b[k + 1]);
else toggle(b[0]);
// suppress bouncing:
delay(300);
// waait until button released
while (getButton() >= 0);
showState();
delay(100);
for (int i = 0; i < N; i++)
if (!b[i]) return;
// if all on show success:
newGame();
}
void initPins() {
// prepare all the pins:
for (int i = 0; i < N; i++) {
// even: buttons
pinMode(i * 2, INPUT_PULLUP);
// odd: LEDs
pinMode(i * 2 + 1, OUTPUT);
}
}
void showState() {
for (int i = 0; i < N; i++)
setLed(i, b[i]);
}
void newGame() {
initPins();
if (!firstRun) {
delay(1000);
// show success:
int t = 100;
int j = 0;
for (int i = 0; i < 100; i++) {
setLed(j, HIGH);
delay(t);
setLed(j, LOW);
j++;
if (j > N) {
j = 0;
t = t - 2;
}
}
}
firstRun = false;
delay(1000);
int d;
// generate new random pattern
do {
d = 0;
for (int i = 0; i < N; i++) {
int z = random(2); // 0 or 1
b[i] = z;
d = d + z;
}
// avoid all on and all off:
} while (d <= 0 || d >= N);
showState();
}
int getButton() {
// check all buttons:
for (int i = 0; i < N; i++)
if (digitalRead(i * 2) == LOW)
return i;
return -1;
}
void setLed(byte nr, boolean state) {
digitalWrite(nr * 2 + 1, state);
}
void toggle(boolean & x) {
x = !x; // invert one bit:
}Instructions how to play might follow.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)



Comments