if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == HIGH && s6 == HIGH)
Can I group all the HIGHs together, to decrease the amount of text?
if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == HIGH && s6 == HIGH)
Can I group all the HIGHs together, to decrease the amount of text?
You haven't posted your code, but it seems to me likely that you could combine the values into a single variable.
You could do this:
if ( (s1 & s2 & s3 & s4 & s5 & s6) == HIGH)
or even this:
if (s1 & s2 & s3 & s4 & s5 & s6)
But what do you gain by decreasing the amount of text? Your if statement is clear as to its purpose.
Shorter code is often more obscure anyway.
Pete
Thank you. I need this a few times for my project, so shorter makes it simpler.
QUESTION ANSWERED
I need this a few times for my project, so shorter makes it simpler.
No. Shorter makes for less typing. But, you do that only once. Simpler is a different story. Following AWOL's advice would lead to simpler (and shorter) code.
Note that the abbreviated versions that I posted will only work when testing for all the variables to be HIGH.
For example, this does not test that all six variables are LOW:
if ( (s1 & s2 & s3 & s4 & s5 & s6) == LOW)
If the condition succeeds, it only means that at least one of the six variables is LOW - not that they are all LOW.
Pete
spooner777:
if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == HIGH && s6 == HIGH)Can I group all the HIGHs together, to decrease the amount of text?
throw all of the values into a single uint8_t (byte) datatype and use bit masking tools:
byte sensor[] = {5,6,4,3,10,11}; // any arbitrary array of pins
void setup()
{
byte sensorOutput = 0;
for(int i = 0; i < sizeof(sensor)/sizeof(sensor[i]); i++) //I'm calling Sensor 1 zero in this byte
{
bitWrite(sensorOutput, i, digitalRead(sensor[i])); // lets say it produces a byte like your example: 0b00111110
}
if((sensorOutput ^ 0b00111110) == 0) // returns true if bits 1-5 are HIGH AND bit 0 is LOW // edited
{
//do something
}
}
void loop()
{
}
but you'll have to brush up on your bit math!