Toggle Switch to change Celsius to Fahrenheit

Hello,

I am not a programmer but I was able to adapt someone else’s sketch and change it to do what I want. This is a very short and simple program.

I have searched for this solution quite a bit before posting here.

I would like to add a toggle switch so I can change between Celsius and Fahrenheit. A toggle switch would be better than a button so that a power loss / battery change / reboot will start up in the preferred display value. A two position switch with three pins.

You can see towards the bottom of my code I have both methods C and F but Celsius is currently commented out.

Code below is working but want to add to it.

#include <Wire.h>
#include "i2c.h"
#include "i2c_SI7021.h"
#include "U8glib.h"

SI7021 si7021;
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI 
//U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI 
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI 


void setup() {
si7021.initialize();
}

void loop() {
    static float humi, temp;
    si7021.getHumidity(humi);
    si7021.getTemperature(temp);
    si7021.triggerMeasurement();

  u8g.firstPage();  
  do {
      //u8g.setFont(u8g_font_osb21);
      u8g.setFont(u8g_font_7x14);
      u8g.setPrintPos (0, 10);
      u8g.print("Humidity "); u8g.print(humi); u8g.print(" %");
      //u8g.setFont(u8g_font_unifont);
      u8g.setPrintPos (0, 30);
    //u8g.print("Temp     ");u8g.print(temp); u8g.print(" \xB0""C");         // Temp Displayed in Celsius
      u8g.print("Temp     ");u8g.print(temp*1.8+32); u8g.print(" \xB0""F");  // Temp Displayed in Fahrenheit
  } while( u8g.nextPage() );
  
  delay(500);
}

Thank you,

Declare two const variables with appropriate names and assign pin numbers to them
Use pinMode(pinNumber, INPUT_PULLUP) for both of them
Connect the common terminal of the switch to GND and the other two to the pins previously declared
In the code do something like

If (digitalRead(celciusPin))
{
  //call function to output value as Celcius
}
else
{
  //call function to output value as Fahrenheit
}

datasiphon:
A two position switch with three pins.

A two position switch usually has 2 pins. In one position, the pins are shorted together in the other position they are open. Wire one side to ground, the other to a pin and then follow @UKHeliBob's code.

A two position switch usually has 2 pins.

There is no reason why a 2 position switch should not have 3 pins. 1 common pin switched between the other 2

Hello,

It has been a while but I was waiting on parts.

I seem to have got it to kind of work. I uploaded the below code but it immediately freezes and then gets some duplicates on the screen after a while. The switch works, if I have it in one position then plug in the power it shows C, flip the switch and it shows F.

Also, if I try to re-upload any other sketch, I can no longer select it’s port from the IDE. Have I bricked it? I have done this to two Pro Micro Clones. So it kind of works and doesn’t is some other terrible ways.

#include <Wire.h>
#include "i2c.h"
#include "i2c_SI7021.h"
#include "U8glib.h"
SI7021 si7021;
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
//U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
int celciusPin = 7; //change this to the correct C pin/switch
static float humi, temp;

void setup() {
    si7021.initialize();
    pinMode(celciusPin, INPUT_PULLUP);
    si7021.getHumidity(humi);
    si7021.getTemperature(temp);
    si7021.triggerMeasurement();
    u8g.firstPage(); 
}
void loop() {
    do {
        //u8g.setFont(u8g_font_osb21);
        u8g.setFont(u8g_font_7x14);
        u8g.setPrintPos (0, 10);
        u8g.print("Humidity "); u8g.print(humi); u8g.print(" %");
        //u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos (0, 30);
        if (digitalRead(celciusPin)) {
            u8g.print("Temp     ");
            u8g.print(temp);
            u8g.print(" \xB0""C");   // Temp Displayed in Celsius
        }
        else {
            u8g.print("Temp     ");
            u8g.print(temp*1.8+32);
            u8g.print(" \xB0""F");  // Temp Displayed in Fahrenheit
        }
        
    } while( u8g.nextPage() );
    delay(500);
}

This is how I have it wired with a I2c Testing Jig I had made from multiple I2c Devices and their different pinouts.

You can un-brick a 32U4. I've done it a few times. Just google it.

This is a strange program. You measure the temperature and humidity just once, then print it endlessly.

What does nextPage() do? Maybe you're allocating display memory every time you do that.

It worked fine before adding the switch and the "if" statement. It refreshes to the screen the new values of humidity and temp every half second.

I am just unsure of how to implement the "if" correctly.

What does nextPage() do? I see that it is provided in u8g to run a "picture loop" or frame buffer animation. Why are you doing that? Are you doing it correctly?

As presented, it could never "work fine". You never acquire sensor data inside loop().

UKHeliBob:
Declare two const variables with appropriate names and assign pin numbers to them
Use pinMode(pinNumber, INPUT_PULLUP) for both of them
Connect the common terminal of the switch to GND and the other two to the pins previously declared
In the code do something like

If (digitalRead(celciusPin))

{
  //call function to output value as Celcius
}
else
{
  //call function to output value as Fahrenheit
}

What is the benefit of using 2 pins? The way I’m looking at it is, he has 2 options; F or C. One pin has two possible states; high or low. If the pin is high, call Fahrenheit. If it is low, call Celsius. Why involve a second pin?

Joe

What is the benefit of using 2 pins?

None. In fact my pseudo code only uses one pin anyway as has previously been pointed out

UKHeliBob:

If (digitalRead(celciusPin))

{
 //call function to output value as Celcius
}
else
{
 //call function to output value as Fahrenheit
}

I also don't get this.
If (digitalRead(celciusPin))

How does that work? If digital read Celsius pin what? If it is high? If it is low? The read is going to return 1 of 2 values and this doesn't say which one the if is looking for so how does it work? I would expect something like:

   switchState = digitalRead(celsiusPin);
if switchState == HIGH
{//call celsius function
}

else
{
//call fahrenheit function
}

Thanks
Joe

JoeWillson:
What is the benefit of using 2 pins? The way I'm looking at it is, he has 2 options; F or C. One pin has two possible states; high or low. If the pin is high, call Fahrenheit. If it is low, call Celsius. Why involve a second pin?

Joe

This is what happens when variable names are not descriptive. Confusion!

If (digitalRead(celciusPin))
{
  //call function to output value as Celcius
}
else
{
  //call function to output value as Fahrenheit
}

would make perfect sense as:

#define CELSIUS true
[...]
If (digitalRead(tempUnitsSwitchPin == CELSIUS))
{
  //call function to output value as Celcius
}
else
{
  //call function to output value as Fahrenheit
}

aarg:
What does nextPage() do? I see that it is provided in u8g to run a “picture loop” or frame buffer animation. Why are you doing that? Are you doing it correctly?
tpictureloop · olikraus/u8glib Wiki · GitHub

As presented, it could never “work fine”. You never acquire sensor data inside loop().

I got the base code from Julian Ilett on youtube. https://youtu.be/4NAtijiSlOk
It did work fine before I added the switch functionality.
I know that it is part of of u8glib but I don’t anything other than that.

This was the base code.

#include <Wire.h>
#include "i2c.h"
#include "i2c_SI7021.h"
#include "U8glib.h"

SI7021 si7021;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI 

void setup() {
si7021.initialize();
}

void loop() {
   static float humi, temp;
   si7021.getHumidity(humi);
   si7021.getTemperature(temp);
   si7021.triggerMeasurement();

 u8g.firstPage();  
 do {
     u8g.setFont(u8g_font_osb21);
     u8g.setPrintPos (0, 31);
     u8g.print(humi); u8g.print("%");
     u8g.setFont(u8g_font_unifont);
     u8g.setPrintPos (0, 63);
     u8g.print(temp); u8g.print("\xB0""C");
 } while( u8g.nextPage() );
 
 delay(50);
}

So the code you posted, isn't the code that you say works fine? I suggest you start again fresh with Julian's code because you've mangled it pretty badly.

aarg:
So the code you posted, isn't the code that you say works fine? I suggest you start again fresh with Julian's code because you've mangled it pretty badly.

The code I put on the first post of this thread was my modification of Julian's code and it worked great. I don't understand the line of questioning. I am just trying to figure out how to get the Switch and "if" "else" working.

I will see what I can figure out when I get home tonight from the replies that were posted today.

Thank you

JoeWillson:
I also don’t get this.
If (digitalRead(celciusPin))

How does that work? If digital read Celsius pin what? If it is high? If it is low? The read is going to return 1 of 2 values and this doesn’t say which one the if is looking for so how does it work?

It’s a bit of coding shorthand. if() always looks for a true condition.

dougp:
It's a bit of coding shorthand. if() always looks for a true condition.

OK. HIGH is true, or maybe more correctly, LOW is false. Got it. Was able to look it up now. This kind of info is out there and available, but isn't always in a context that is presented like this. Sometimes ya gotta ask the easy questions to be able to find the easy answers.
Thank you
Joe