Cronometer in binary

Hi, thanks for having me here. :slightly_smiling_face:
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

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);
}
}
}
}

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) to make it easier to read and copy for examination

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.

1 Like

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

2 Likes

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

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

LSB or MSB first?

What door? What doors?

Your code instructs them to do this.

// #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)

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
{
  "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": {}
}
2 Likes

Yes, i will try 6 and 7.

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

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
1 Like

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

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...

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
  }
}
1 Like

Or blink.ino them one by one:

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

1 Like

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); }

}

Does it work?

Probably not.

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 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.

1 Like

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

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

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....

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
{
  "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": {}
}
1 Like

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