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?
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.
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
}
}
}
}
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:
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
}
}
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.
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); }
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.
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?
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
}
}