frswa
1
Hi All,
Im using a esp8266 module for remote motoring of some alarms at client premises.
my query can I set a variable that would be the binary representation of all inputs? if so how?
for example
100 = Var = DigitalRead(0) & digitalRead(2) & digitalRead(3);
where as 0 is the only active input.
EDIT: the above syntax only saves the first input status in the variable.
this would allow fast change validation of the 8 inputs im using.
guix
2
Welcome,
I think you may be interested by this
~~https://www.arduino.cc/en/Reference/PortManipulation~~
Sorry I didn't read that you were using esp8266. But there may be a similar thing for esp8266.
Or you can do something like this:
uint8_t pin0 = digitalRead(0);
uint8_t pin2 = digitalRead(2);
uint8_t pin3 = digitalRead(3);
uint8_t value = pin0 << 2 | pin2 << 1 | pin3;
Adapt this for your own pin numbers
const byte pins[] = {A3, A2, A1, A0};
const byte PINCOUNT = sizeof(pins) / sizeof(pins[0]);
void setup()
{
Serial.begin(115200);
while (!Serial);
for (int p = 0; p < PINCOUNT; p++)
{
pinMode(pins[p], INPUT_PULLUP);
}
}
void loop()
{
byte value = 0;
for (int p = 0; p < PINCOUNT; p++)
{
bitWrite(value, p, digitalRead(pins[p]));
}
showBits(value);
delay(1000);
}
void showBits(byte number)
{
Serial.println();
for (int b = 0; b < PINCOUNT; b++)
{
Serial.print(bitRead(number, b));
}
Serial.println();
}
system
Closed
4
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.