# Cronometer in binary

**URL:** <https://forum.arduino.cc/t/cronometer-in-binary/1325108>\
**Category:** General Electronics\
**Created:** [November 23, 2024, 7:34pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108 "2024-11-23T19:34:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![revdruid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/revdruid/32/1102562_2.png) [@revdruid](https://forum.arduino.cc/u/revdruid)\
**Post date:** [November 23, 2024, 7:34pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/1 "2024-11-23T19:34:57Z")

</div>

Hi, thanks for having me here. 🙂  
I want to create a cronometer with 6 leds, the count is 0 to 59 seconds.  
For now i just want to activate the leds of a specific decimal number, for example 4.

The 2 leds on door 0 and 1 start HIGH and dont go LOW. Anyone know why?

the math was based on this site:  
This code mechanic was based on this: [Online C Compiler - online editor](https://www.onlinegdb.com/online_c_compiler)

The code:  
// C++ code  
//

#include \<stdio.h\>  
#include \<stdlib.h\>

#define Pin0 0  
#define Pin1 1  
#define Pin2 2  
#define Pin3 3  
#define Pin4 4  
#define Pin5 5  
//const int Pin0 = 0, Pin1 = 1, Pin2 = 2, Pin3 = 3, Pin4 = 4, Pin5 = 5;  
//bool iniciar = true;  
char opcao;  
void setup() {  
pinMode(Pin0, OUTPUT);  
pinMode(Pin1, OUTPUT);  
pinMode(Pin2, OUTPUT);  
pinMode(Pin3, OUTPUT);  
pinMode(Pin4, OUTPUT);  
pinMode(Pin5, OUTPUT);  
Serial.begin(9600);  
}  
void loop() {  
int n = 4;  
int resto;  
int i;  
String para\_string;  
for (i = 7; i \>= 0; i--) {  
if (i\>5)  
{  
resto = n \>\> i;  
}  
else{  
resto = n \>\> i;  
if (resto & 1) {  
digitalWrite(i, HIGH); //HIGH para ligar o led  
} else {  
digitalWrite(i, LOW);  
}  
}  
}  
}

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/c/f/c/cfc5d44854cd06ddcc879bc435ae22818fa3995a.png)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 23, 2024, 7:36pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/2 "2024-11-23T19:36:06Z")

</div>

> [@revdruid](#):
>
> The code:

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use **code tags** (the \< CODE/ \> icon above the compose window)&nbsp;to make it easier to read and copy for examination   
  
[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum)

Please post your sketch, using code tags when you do  
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows  
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

---

<div class="post-metadata">

**Author:** ![mancera1979](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mancera1979/32/732001_2.png) [@mancera1979](https://forum.arduino.cc/u/mancera1979)\
**Post date:** [November 23, 2024, 7:48pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/3 "2024-11-23T19:48:37Z")

</div>

> [@revdruid](#):
>
> #define Pin0 0  
> #define Pin1 1

These pins are used by Serial and conflict with other uses. Try with 6 and 7

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 23, 2024, 11:15pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/4 "2024-11-23T23:15:58Z")

</div>

What did you want the LEDs to do or look like?

> [@revdruid](#):
>
> 6 leds, the count is 0 to 59 seconds.

okay, 000000 to 111011 or 110111 (depending on LSB or MSB)

> [@revdruid](#):
>
> activate the leds of a specific decimal number, for example 4.

LSB or MSB first?

> [@revdruid](#):
>
> door 0 and 1

What door? What doors?

> [@revdruid](#):
>
> start HIGH and dont go LOW. Anyone know why?

Your code instructs them to do this.

```arduino
// #include <stdio.h> // not needed
// #include <stdlib.h> // not needed

// #define Pin0 0 // not valid for LED
// #define Pin1 1 // not valid for LED
#define Pin2 2
#define Pin3 3
#define Pin4 4
#define Pin5 5
//const int Pin0 = 0, Pin1 = 1, Pin2 = 2, Pin3 = 3, Pin4 = 4, Pin5 = 5; // not used
//bool iniciar = true; // not used
char opcao; // not used

void setup() {
  pinMode(Pin0, OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);
  pinMode(Pin5, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  int n = 4; // not used... n becomes resto
  int resto; // not used
  int i;

  // String para_string; // not used
  for (i = 7; i >= 0; i--) {
    if (i > 5) // if 7 or 6...
    {
      resto = n >> i; // resto is n is 7 >> 7 = 0|0000111 and 6 >> 6 = 0|000011
    }
    else {
      resto = n >> i; // repeats of above 5 = 0|00101, 4 = 0100, 3 = 0|011, 2 = 0|1, 1 = 0|1 
      if (resto & 1) { // nothing remains to the left to & with
        digitalWrite(i, HIGH); //HIGH para ligar o led
      } else {
        digitalWrite(i, LOW); // all LEDs set LOW
      }
    }
  }
}

```

If you want to light an LED by pressing a number, here is a short sketch that will light six LEDs (0 through 5) attached to pins 2 through 7... (\< 0 and \> 6 erase LEDs)

```arduino
const byte ledPin[] = {2, 3, 4, 5, 6, 7}; // array of LED pins
const byte arraySize = sizeof(ledPin) / sizeof(ledPin[0]); // elements in array

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 to OUTPUT
}

void loop() {
  int number; // keyboard value
  if (Serial.available()) { // wait for serial data
    number = Serial.parseInt(); // get a value
    Serial.read(); // remove CRLF
    if (number >= 0 && number <= arraySize - 1) { // from 0 to 6, inclusive
      Serial.print(ledPin[number]); // print LED pin
      digitalWrite(ledPin[number], HIGH); // set LED ON
    } else { // below 0 and above arraySize
      for (int i = 0; i < arraySize; i++) { // count through pins
        digitalWrite(ledPin[i], LOW); // set pins
      }
    }
  }
}

```

> **diagram.json for wokwi.com**
>
> ```arduino
> {
> "version": 1,
> "author": "Anonymous maker",
> "editor": "wokwi",
> "parts": [
> { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
> {
> "type": "wokwi-led-bar-graph",
> "id": "bargraph1",
> "top": -91.4,
> "left": 53,
> "rotate": 270,
> "attrs": { "color": "lime" }
> }
> ],
> "connections": [
> ["nano:2", "bargraph1:A10", "green", ["v0"] ],
> ["nano:3", "bargraph1:A9", "green", ["v0"] ],
> ["nano:4", "bargraph1:A8", "green", ["v0"] ],
> ["nano:5", "bargraph1:A7", "green", ["v0"] ],
> ["nano:6", "bargraph1:A6", "green", ["v0"] ],
> ["nano:7", "bargraph1:A5", "green", ["v0"] ],
> ["nano:GND.2", "bargraph1:C10", "black", ["v0"] ],
> ["bargraph1:C10", "bargraph1:C9", "green", ["v0"] ],
> ["bargraph1:C9", "bargraph1:C8", "green", ["v0"] ],
> ["bargraph1:C8", "bargraph1:C7", "green", ["v0"] ],
> ["bargraph1:C7", "bargraph1:C6", "green", ["v0"] ],
> ["bargraph1:C6", "bargraph1:C5", "green", ["v0"] ]
> ],
> "dependencies": {}
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![revdruid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/revdruid/32/1102562_2.png) [@revdruid](https://forum.arduino.cc/u/revdruid)\
**Post date:** [November 24, 2024, 6:34pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/5 "2024-11-24T18:34:01Z")

</div>

Yes, i will try 6 and 7.

---

<div class="post-metadata">

**Author:** ![revdruid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/revdruid/32/1102562_2.png) [@revdruid](https://forum.arduino.cc/u/revdruid)\
**Post date:** [November 24, 2024, 6:39pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/6 "2024-11-24T18:39:12Z")

</div>

i want a cronometer in binary tha goes from 000000 to 111011

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 24, 2024, 6:58pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/7 "2024-11-24T18:58:21Z")

</div>

Okay, you want a decimal to binary conversion (not a "number to LED pin" as my code is doing). That explains the shifting in your code. Your new code should:

1. test the LSB
2. note if 1 or 0
3. check for end of bits
4. shift again
5. goto 1

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 25, 2024, 6:12pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/8 "2024-11-25T18:12:47Z")

</div>

Have you had a chance to make the integer-to-binary code?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 25, 2024, 8:03pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/10 "2024-11-25T20:03:39Z")

</div>

You can "shift" (divide) either the mask (a "1" bit) or shift the number entered.

Here is another pseudo-code...

- start a counter for the six bits.
- "AND" the number with the mask
- if the result is 1, turn the counter LED on
- if the result is not 1, turn the counter LED off
- shift the mask or shift the number

If the LEDs are not turning on, make a small script to turn them all on. Be sure to write to the PIN, not the sequence of LED...

```arduino
const byte ledPin[] = {2, 3, 4, 5, 6, 7}; // array of LED pins
const byte arraySize = sizeof(ledPin) / sizeof(ledPin[0]);

void setup() {
  for (byte i = 0; i < arraySize; i++) // count through pins
    pinMode(ledPin[i], OUTPUT); // set LED pins
}

void loop() {
  for (byte i = 0; i < 6; i++) { // count through pins
    digitalWrite(ledPin[i], HIGH); // turn LED ON
  }
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 25, 2024, 8:39pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/11 "2024-11-25T20:39:21Z")

</div>

Or _blink_._ino_ them one by one:

```arduino
void loop() {
  for (byte i = 0; i < 6; i++) { // count through pins
    digitalWrite(ledPin[i], HIGH); // turn LED ON
    delay(333); // for awhile

    digitalWrite(ledPin[i], LOW); // turn LED OFF
    delay(444); // for awhile
  }
}

```

A tests sketch should be short and foolproof, but mebbe some fun could be squeezed in.

a7

---

<div class="post-metadata">

**Author:** ![revdruid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/revdruid/32/1102562_2.png) [@revdruid](https://forum.arduino.cc/u/revdruid)\
**Post date:** [November 26, 2024, 7:28pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/12 "2024-11-26T19:28:00Z")

</div>

I want a 60 seconds bit clock, liKe:  
for(int i = 0; i\>59; i++){  
Convert i do binary, in this case zero=0000, then 1, 2, 3... until 59  
put the number in an array:  
array[0]=0;  
array[1]=0;  
array[2]=0;  
array[3]=0;  
then make a for cicle to high the 1 and low the 0 of the elements in array.  
for(j=0; j\>3; j++){  
if (array[j]) ==1){  
digitalWrite(ledPin[0], HIGH);  
else { digitalWrite(ledPin[J], LOW); }

}

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 26, 2024, 7:49pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/13 "2024-11-26T19:49:51Z")

</div>

> [@revdruid](#):
>
> I want a 60 seconds bit clock

Does it work?

> [@revdruid](#):
>
> i\>59

Probably not.

> [@revdruid](#):
>
> put the number in an array

Why an array? You have six "bits" not four numbers. You only need to test the bits (shift/divide) of the "integer" value.

I gave you the decimal to binary code. Do your LEDs work?

If you use [TRANSLATE.GOOGLE.COM](http://TRANSLATE.GOOGLE.COM) you can write in your own language (Magyar?) and it will be translated to English (or whatever language you want), then you can post that translation here. It works well.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 26, 2024, 9:56pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/14 "2024-11-26T21:56:14Z")

</div>

> [@xfpd](#):
>
> Do your LEDs work?

Yeah, @revdruid, I thought we working in getting your LEDs to do anything.

The other stuff is irrelephant until they do.

And as @xfpd sez, you are going to trouble you needn't with the array stuff. Is someone making you do things in a roundabout manner for learning purposes?

a7

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 26, 2024, 10:40pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/15 "2024-11-26T22:40:22Z")

</div>

Although post topic says "c(h)ronometer" OP stated "activate the LEDs of a specific number"

> [@revdruid](#):
>
> i want a cronometer in binary tha goes from 000000 to 111011

The OP attempt at coding was non-functional... but attempts at explaining. I have a feeling when OP gets "a chronometer that goes from 000000 to 111011" the direction will be changed... therefore... imma code dump this. It mindlessly counts from zero to 59 in binary, and has the option to be keyboard entry that can be used for external data entry of a value....

```arduino
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();
  // 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
  }
}

```

> **diagram.json for wokwi.com**
>
> ```arduino
> {
> "version": 1,
> "author": "Anonymous maker",
> "editor": "wokwi",
> "parts": [
> { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
> {
> "type": "wokwi-led-bar-graph",
> "id": "bargraph1",
> "top": -91.4,
> "left": 53,
> "rotate": 270,
> "attrs": { "color": "lime" }
> }
> ],
> "connections": [
> ["nano:2", "bargraph1:A10", "green", ["v0"] ],
> ["nano:3", "bargraph1:A9", "green", ["v0"] ],
> ["nano:4", "bargraph1:A8", "green", ["v0"] ],
> ["nano:5", "bargraph1:A7", "green", ["v0"] ],
> ["nano:6", "bargraph1:A6", "green", ["v0"] ],
> ["nano:7", "bargraph1:A5", "green", ["v0"] ],
> ["nano:GND.2", "bargraph1:C10", "black", ["v0"] ],
> ["bargraph1:C10", "bargraph1:C9", "green", ["v0"] ],
> ["bargraph1:C9", "bargraph1:C8", "green", ["v0"] ],
> ["bargraph1:C8", "bargraph1:C7", "green", ["v0"] ],
> ["bargraph1:C7", "bargraph1:C6", "green", ["v0"] ],
> ["bargraph1:C6", "bargraph1:C5", "green", ["v0"] ]
> ],
> "dependencies": {}
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 25, 2025, 10:40pm UTC](https://forum.arduino.cc/t/cronometer-in-binary/1325108/16 "2025-05-25T22:40:26Z")

</div>

Closed on May 25, 2025, 10:40pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
