const float BETA = 3950;
const int REDPIN = 3;
const int GREENPIN = 2;
const int BLUEPIN = 1;
const int TEMPPIN = 28;
const int CLK = 27;
const int DT = 26;
int ledValue = 0;
float percent = 100;
float percentDecimal = 1;
void readEncoder() {
int dtValue = digitalRead(DT);
if (dtValue == HIGH) {
if(percent <= 90){
percent += 10;
percentDecimal = percent/100;
}
}
else{
if(percent >= 10){
percent -= 10;
percentDecimal = percent/100;
}
}
}
void readEncoderButton(){
}
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
pinMode(REDPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(TEMPPIN, INPUT);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
attachInterrupt(CLK, readEncoder, FALLING);
}
void loop() {
int analogValue = analogRead(TEMPPIN);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
float fahrenheit = (celsius * 1.8) + 32;
fahrenheit = constrain(fahrenheit, 0, 100);
if(fahrenheit >= 0 && fahrenheit <= 51){
ledValue = map(fahrenheit, 0, 50, 0, 255);
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, (255 - ledValue) * percentDecimal);
analogWrite(GREENPIN, ledValue * percentDecimal);
}
else if(fahrenheit >= 52 && fahrenheit <= 101){
ledValue = map(fahrenheit, 51, 100, 0, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN, (255 - ledValue) * percentDecimal);
analogWrite(REDPIN, ledValue * percentDecimal);
}
delay(100);
}