/*
// https://forum.arduino.cc/t/binary-counter-led-project-issue/1438696
You stated you would be counting to fifteen:
[quote="belerand317, post:1, topic:1438696"]
4 LEDs (this part is done and working correctly), and then to write a program that will count from 0-15
[/quote]
But your code counts to eight (bits, I think) which would be equal to `2 ^ 8 = 256`... and the full code seems a bit too much for the task.
Try coding in steps... using your idea: Count from zero to fifteen. Represent that count in Binary. Show the count using LEDs. Make each count take one second.
Step 1. Write a sketch (program) that uses `loop()` to count from zero to fifteen.
*/
```
byte counter, maxCount = 15; // variables for counting and maximum
void setup() {
Serial.begin(115200); // start serial monitor
}
void loop() {
countup();
}
void countup() {
if (counter > maxCount) // verify counter is below maximum...
while (1) {}; // stop the program here
Serial.println(counter); // all is well, print the value
counter++; // increase counter
}
```
/*
Step 2. Control timing for each count (for example; at one second intervals...
*/
```
byte counter, maxCount = 15; // variables for counting and maximum
bool shownext; // flag to indicate when to show the count
unsigned long timer, timeout = 1000; // timer variables
void setup() {
Serial.begin(115200); // start serial monitor
}
void loop() {
timecount(); // call function to tell when to show the count
if (shownext) { // if timecount() flag indicates it is time...
shownext = false; // ... reset flag and...
countup(); // ... call the function to show the count
}
}
void timecount() {
if (millis() - timer >= timeout) { // if timeout is overrun...
timer = millis(); // ... store millis() "now" time
shownext = true; // ... call the counting function
}
}
void countup() {
if (counter > maxCount) // verify counter is below maximum...
while (1) {}; // stop the program here
Serial.println(counter); // all is well, print the value
counter++; // increase counter
}
```
/*
Step 3. Count from 0 to 15 and translate base - 10 to base - 2
*/
```
byte counter, maxCount = 15; // variables for counting and maximum
bool shownext; // flag to indicate when to show the count
unsigned long timer, timeout = 100; // timer variables
void setup() {
Serial.begin(115200); // start serial monitor
}
void loop() {
timecount(); // call function to tell when to show the count
if (shownext) { // if timecount() flag indicates it is time...
shownext = false; // ... reset flag and...
countup(); // ... call the function to show the count
}
}
void timecount() {
if (millis() - timer >= timeout) { // if timeout is overrun...
timer = millis(); // .. store millis() "now" time
shownext = true; // ... call the countup function
}
}
void countup() {
if (counter > maxCount) // verify counter is below maximum...
while (1) {}; // ... counter is above max, stop the program
if (counter < 10)
Serial.print(0);
Serial.print(counter); // all is well, print the value
Serial.print(" = ");
dectobin(counter); // call the decimal to binary function
counter++; // increase counter
}
void dectobin(byte decimal) { // receive the counter value
byte binary[4], bits; // four bits
while (decimal) { // greater than zero means more bits
Serial.print(decimal % 2);
// binary[bits++] = decimal % 2; // collect bits LSB to MSB
decimal /= 2; // divide by base 2
}
bits = 0; // reset bit number
for (byte i = 0; i < 4; i++) { // count four bits
Serial.print(binary[3 - i]); // invert bit order MSB to LSB
}
Serial.println(); // next line
}
```