Hi everyone;
I have 7 leds and i need to put their status in a byte
for example:
Led 1 OFF
Led 2 OFF
Led 3 OFF
.
.
.
Led 7 ON
Result --> 0000001
Who can help me with the easiest way.
Thanks a lot.
Hi everyone;
I have 7 leds and i need to put their status in a byte
for example:
Led 1 OFF
Led 2 OFF
Led 3 OFF
.
.
.
Led 7 ON
Result --> 0000001
Who can help me with the easiest way.
Thanks a lot.
This article might help you.
https://docs.arduino.cc/language-reference/en/functions/bits-and-bytes/bitWrite/
byte status = 0b0000;
const byte led2 = 0b0010;
const byte led3 = 0b0100;
status = status || led2;
status = status || led3;
status = status && !led2;
Status should now be 0b0100...
Hi,
Thanks.
I would like to use first digitalRead function (So to read the status) then put the result for each Led in a byte.
Is it possible?
Post your current sketch to see how we can help.
Hi
Right now i'm in draft version.
Nothing is ready or working (stil ldevelopping the idea).
Which Arduino and which pins are your LEDs connected to?
LED1 -> pin ?, ...... LED7 -> pin?
Post it.
something along the lines of (running on a ESP32) using bitwise | (OR) and << (shift left)
#define LED1 16
#define LED2 17
#define LED3 4
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
byte status=0;
digitalWrite(LED1, 1);
digitalWrite(LED2, 0);
digitalWrite(LED3, 1);
status=digitalRead(LED1) | (digitalRead(LED2)<< 1) | (digitalRead(LED3) << 2);
Serial.println(status, BIN);
}
void loop() {}
serial monitor displays
101
edit. I miss one too, the bitwise negation operator… and the result is correct.
byte status = 0b0000;
const byte led2 = 0b0010;
const byte led3 = 0b0100;
status = status | led2;
status = status | led3;
status = status & ~led2
I didn't check your maths, but bitwise operators are def the way to calculate that.
a7
Save the states of LEDs to a gobal variable during operation.
Hi paulpaulson,
can't follow you
may be you can show me that?
Thanks
The LEDs are switched on or off in the sketch and the status of the LED can also be set in a global variable at this time.
Thanks for pointing that out!
Should be ~ instead of !
Hi
I'm always getting 0
please have look on simulation may be have made something wrong.
Haha, yes, I missed that one! (See what I did there?)
With |, & and ~ the result is correct.
a7
have sent to you the sketch and everything and honestly i'm not getting an accurate answer or a single sketch from you.
My request is simple.
read the value of the led put the bit in the byte and that's it.
forget about the sketch and what i have done othewhise you will be lost.
...
what board are you simulating?
I used a real ESP32 module for the test run
you may need to change the pin number to suit your board
arduino Mega
and of course i'm using my own pin numbers