const byte ledPin[] = {2, 3, 4, 5, 6, 7}; // array of LED pins
const byte arraySize = sizeof(ledPin) / sizeof(ledPin[0]);
void setup() {
Serial.begin(115200); // start serial communications
for (byte i = 0; i < arraySize; i++) // count through pins
pinMode(ledPin[i], OUTPUT); // set LED pins
Serial.println(); // handshake serial communications
}
void loop() {
count(); // automatic counter from 0 to 59
// keyboard(); // enter value 0 to 59 on a keyboard
}
void count() {
for (int i = 0; i < 60; i++) { // count 60 seconds
int2bin(i); // convert to binary
delay(1000); // pause for one second
}
}
void keyboard() { // keyboard entered value
int number;
if (Serial.available()) { // wait for serial data
number = Serial.parseInt(); // get an integer
Serial.read(); // remove CRLF
if (number >= 0 && number < 60) // allow values 0 to 59
int2bin(number);
else
clearLEDs(); // outside 0 to 59 clear LEDs
}
}
void int2bin (int value) { // receive integer, show
byte mask = 1; // start with mask of "000001"
for (byte i = 0; i < 6; i++) { // count through six bits
if (value & mask) { // "AND" value with mask
digitalWrite(ledPin[i], HIGH); // if 1, turn corresponding LED on
}
else {
digitalWrite(ledPin[i], LOW); // if 0, turn corresponding LED off
}
mask = mask << 1; // shift mask left one bit
}
}
void clearLEDs() {
for (byte i = 0; i < 6; i++) { // count through pins
digitalWrite(ledPin[i], LOW); // turn LED off
}
}