Back to Parent

const int RED_LED_PIN = A2;
const int GREEN_LED_PIN = A1;
const int BLUE_LED_PIN = A0;
int temperaturePin = A5;
int inputPin = D1;
int val = 0;
int state = 1;
int tempHigh = 28;
int tempLow = 24;

int greenIntensity = 0;
int blueIntensity = 0;


void setup() {


    pinMode(RED_LED_PIN, OUTPUT);
    pinMode(GREEN_LED_PIN, OUTPUT);
    pinMode(BLUE_LED_PIN, OUTPUT);

    pinMode(temperaturePin, INPUT);
    pinMode(inputPin, INPUT);

    Serial.begin(9600);
}

void loop() {
    val = digitalRead(inputPin);
    double temperature = (analogRead(temperaturePin) * 3.3) / 4095;
    temperature = (temperature - 0.5) * 100;
    Serial.println(temperature);
    int adjRed = (int) (255 - redVal(temperature));
    int adjBlue = (int) (255 - blueVal(temperature));
    if (val == LOW) {
        state = !state;
        delay(250);

    }

    if (!state) {
        adjRed = 255;
        adjBlue = 255;
    }


    analogWrite(GREEN_LED_PIN, 255);

    analogWrite(BLUE_LED_PIN, adjBlue);
    analogWrite(RED_LED_PIN, adjRed);

    Serial.println("red: " + String(adjRed));
    Serial.println("blue: " + String(adjBlue));
    Serial.println(state);

}



double redVal(double temp){
    if (temp >= tempHigh){
        return 255;
    } else if (temp <= tempLow){
        return 0;
    }
    double mulFactor = (256/(tempHigh - tempLow));
    double retVal = mulFactor*(temp - tempLow);
    if (retVal > 0){
        retVal = retVal - 1;
    }

    return retVal;
}

double blueVal(double temp){

    double retVal = redVal(temp); 
    retVal = 255 - retVal;
    return retVal;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0