If / else does not work for me

Hi, everyone,

I'm sure I'm doing something really simple wrong here, but I've read and re-read the reference, and just cant see what it is.

I have a switch, connected to D3, that will select Fahrenheit or Celsius. The temperature is then sent to the display with an 'F' or 'C' after it.

The code to make this choice is:

unit = (digitalRead(3));
Serial.print(unit);

    if (unit = 0)
       { units = " F";}
    else
       {units = " C";}

  Serial.print(units);

Serial.print(unit) is showing either 0 or 1, as it should, depending on the position of the switch, but Serial.print(units) always shows 'C', whichever way the switch is.

If I comment out the 'else' part, Serial.print(units) shows 'F', as I'd expect.

What am I doing wrong, please?

Richard

What am I doing wrong, please?

The usual.

Compare is ==. Assignment is =. So you have assigned the value 0 to unit and 0 is the same as false so it skips the F.

We've all done it. But try not to do it again ;).

Steve

Thanks.

You've no idea how many times I messed with every other part of that and missed the missing '='.

Richard

I still mess that part and I started with Arduino in 2013 (according to the oldest files' creation date)