how do i add an if statement into this

so I have an RGB led, one with R, G, B, and GND connections and I have them set up in my code and what pin they are on but I would like to have it change a certain colour depending on the temperature, I was trying to use the if statement but I couldn’t figure it out, I was wondering if someone could help me add it to this code, so pretty much if it is above 30 degrees on the temp sensor it goes red but if its under that temp its green. any help is very much appreciated, thanks!

#include <ACI_10K_an.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

int redPin= 7;
int greenPin = 6;
int bluePin = 5;

void setup() {
 Serial.begin(9600);

 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
 
  display.display();
  delay(2000);

  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.setTextColor(WHITE);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void loop() {
  
  Aci_10K an10k;
  display.setCursor(0, 0);
  display.print("temp: ");
  display.println(an10k.getTemp(analogRead(0)));
  display.display();
  delay(1000);
  display.clearDisplay();
  
}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
  
}

i currently have this code setup so it can display what temperature it is on an OLED display.

Before you go adding your if statement you may need to reconfigure your pins for the RGB LED if you are using an Uno. Since the RGB LED works on a PWM signal, you must connect it to pins that can produce a PWM signal. I'll let you do some googling to figure out which pins might be appropriate to use if that is the case.

Another thing you might find useful is saving the temperature into a variable useful. You read the temperature so you can print it to the screen, but you don't save it so if you wanted to use it for an if statement you would have to read it again. An easier way to handle it is to read the temperature and save that into a variable, then print that variable to the screen and also use the same variable for your if statement.

The if statement should be a simple if with an else. Probably the most appropriate time to decide what colour to set your LED to is after reading the temperature. Inside the if statement you will want to make an appropriate call to your setColor function.

Good luck.

Metallor:
Before you go adding your if statement you may need to reconfigure your pins for the RGB LED if you are using an Uno. Since the RGB LED works on a PWM signal, you must connect it to pins that can produce a PWM signal. I'll let you do some googling to figure out which pins might be appropriate to use if that is the case.

Another thing you might find useful is saving the temperature into a variable useful. You read the temperature so you can print it to the screen, but you don't save it so if you wanted to use it for an if statement you would have to read it again. An easier way to handle it is to read the temperature and save that into a variable, then print that variable to the screen and also use the same variable for your if statement.

The if statement should be a simple if with an else. Probably the most appropriate time to decide what colour to set your LED to is after reading the temperature. Inside the if statement you will want to make an appropriate call to your setColor function.

Good luck.

okay, im using a nano and I've been able to use example code to make it do like a colour shifting rainbow effect so far so they are on the right pins.

also, how do I save it into a variable then read it to print it on the screen and use it in an if/else statement? im not very good on the coding side of things so i have a bit of trouble trying to figure this out.
thanks :slight_smile:

This is very elementary stuff. The relevant pages you want are variables, if and else.

Give these a read and try to understand how they work and have a go at implementing them in your program. If you can't get it working post your code that you tried and we can have a look at it. I could easily do this for you, but you don't really learn anything if I do that.

Here’s something that you can play with but I agree with Metallor; read the tutorials and look at the code to see if you can figure out what’s going on.

This code doesn’t use the temperature sensor but rather just counts a variable from 0.0 to 40.0 and back again repeatedly, lighting the blue, green and red LEDs depending on the “temperature.”

Read the comments, read the tutorials and you should be able to adapt your sensor to it.

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const byte redPin= 7;
const byte greenPin = 6;
const byte bluePin = 5;

float
    fTestVal;
char
    szTemp[20];
    
void setup( void )
{
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    fTestVal = 0;
    
}//setup

void loop (void )
{
    static unsigned long
        timeUpdate = 0;
    static bool
        bDir = true;
        
    setColors(fTestVal);

    //this section just decides if we're counting fTestVal "up" or "down"
    if( bDir )
    {
        //bDir is true so we're counting up
        fTestVal = fTestVal + 0.01;
        //when fTestVal reaches 40, make bDir false so we start counting down again
        if( fTestVal > 40.0 )
            bDir = false;
            
    }//if
    else
    {
        //bDir is false so we're counting down
        fTestVal = fTestVal - 0.01;
        //when it drops below 0, set bDir true to start counting up
        if( fTestVal < 0.0 )
            bDir = true;
        
    }//else

    //here we update the LCD but only once every 500mS
    if( millis() - timeUpdate > 500 )
    {
        //update the LCD timer to update again 500mS from now
        timeUpdate = millis();
        //I don't believe one needs to clear the entire display to update a line that
        //is printed over itself...
        sprintf( szTemp, "Temp: %3.1f ", fTestVal );
        display.setCursor( 0,0 );
        display.print( szTemp );
        
    }//if
       
}//loop


void setColors( float temp )
{
    byte
        r, g, b;
    int
        nTemp;

    //allows us to use some of the decimal places in temp(erature)
    //nTemp will range from 0 to 15000 (150oC)
    nTemp = (int)temp * 100;

    //OP wants red if temp > 30oC or nTemp = 3000
    //so constrain nTemp between 0 and 3000
    nTemp = constrain( nTemp, 0, 3000 );

    //as a zero-order guess, we'll assign the following
    //red 
    //  - off   <   2000    red starts PWM at ~2000 and is maxed at 3000
    //  - max   >=  3000
    //green
    //  - off   <   1500    grn starts a 1500, maxes at 2000 and is out by 2500
    //  - max   ==  2000
    //  - off   >   2500
    //blue
    //  - off   >=  2000    blu is max at 0 and turns off by 2000
    //  - max   ==  0

    //red
    if( nTemp < 2000 )
        r = 0;    
    else
        //use the map function to map nTemp down to 0-255 for PWM
        r = map( nTemp, 2000, 3000, 0, 255 );

    //green
    if( nTemp < 1500 || nTemp > 2500 )
        g = 0;
    else
    {
        //we ramp up from 1500 to 2000 and down from 2000 to 2500 so
        //two map calls are used
        if( nTemp <=2000 )
            g = map( nTemp, 1500, 2000, 0, 255 );
        else
            g = map( nTemp, 2000, 2500, 255, 0 );
    }

    //blue
    if( nTemp >= 2000 )
        b = 0;
    else
        b = map( nTemp, 0, 2000, 255, 0 );
        
    //assign new PWM values to the RGB led pins
    analogWrite(redPin, r);
    analogWrite(greenPin, g);
    analogWrite(bluePin, b);

}//setColors

@OP

You have the following line in your loop() function.

display.println(an10k.getTemp(analogRead(0)));

Now, get the answers to the following questions; you will be able to re-structure your program using if(-else) as per directives of Post#1.

1. Which part of the above code is acquiring temperature from a sensor connected at A0-pin?

2. Declare an integer type variable like int temp;.

3. Now, write code so that the temperature value acquired by the function/method of Step-1 is assigned to temp.

temp = ............(0);
display.println(temp);

//you should know the unit of temp (degC or degF; integer or float)

4. Now, apply if(-else) control structure

if(temp >30)
{
     //do this
}

if(temp < 30)
{
    //do that
}