If...Else not working as expected? variable value conversion
im trying write sketch read 4-bit switch input, depending on switch value is, change variable value , print on serial monitor.
the switch reading code , serial code working find (not own code!), use of if...else if conversion doesnt work - sets variable 'mhz' 1 (first if...) regardless of read value is,
in mind should work doesnt, either ive done silly or im not getting how this, thoughts?
the switch reading code , serial code working find (not own code!), use of if...else if conversion doesnt work - sets variable 'mhz' 1 (first if...) regardless of read value is,
in mind should work doesnt, either ive done silly or im not getting how this, thoughts?
code: [select]
// test sketch reading 4-bit switch, converting , sending serial monitor
// set initial variables
byte dip1pins[] = {2,3,4,5};
void setup() {
// set serial comms
serial.begin(9600);
// set pins inputs
(int cnt1 = 0; cnt1 < sizeof(dip1pins); cnt1++)
{
pinmode(dip1pins[cnt1], input_pullup);
}
}
void loop() {
// read pins, store each pin in assigned bit
byte val = 0;
byte mhz = 0;
(int cnt1 = 0; cnt1 < sizeof(dip1pins); cnt1++)
{
val |= (!digitalread(dip1pins[cnt1])) << cnt1;
}
serial.print("value = ");
serial.println(val);
if (val=8)
{
mhz=1;
}
else if (val=1)
{
mhz=2;
}
else if (val=9)
{
mhz=3;
}
else if (val=5)
{
mhz=4;
}
else if (val=13)
{
mhz=5;
}
else if (val=3)
{
mhz=6;
}
else if (val=11)
{
mhz=7;
}
else if (val=7)
{
mhz=8;
}
else if (val=15)
{
mhz=9;
}
else
{
mhz=0;
}
delay(1000);
serial.print("mhz = ");
serial.println(mhz);
}
your variable dip1pins ever 2, 3, 4, or 5. index values 0, 1, 2, 3.
it's never equal 8.
sizeof() made of in loop.
it's never equal 8.
sizeof() made of in loop.
Arduino Forum > Using Arduino > Programming Questions > If...Else not working as expected? variable value conversion
arduino
Comments
Post a Comment