Go Down

Topic: [ Proj 03 ] Room temperature calibration with a push button (Read 31823 times) previous topic - next topic

Andreadoria

Hi, I made some little changes to the number 3 project, Love O Meter.
I thought it would be nice to have the possibility to set the room temperature directly from the device instead that manually set the temperature in the code.
I used a push button, and a yellow led; when the pin connected to the switch is high, the program turns off the red led in the case they are still on, turn on a yellow led that indicates the device is storing a new value for the baselineTemp variable, counts for about 25 seconds which is the time needed to cool down the temp sensor and finally the sensor actually read and store the new value for baselineTemp.


The piece of code I added to the original one is between the **** marks, then I changed the constant baselineTemp in a global variable, added the yellow pin(6) in void setup and added 1.9 degrees as threshold temperature for the first red led to turn on.

In this way, it is possible to continue using the sensor and set new room temperature values without interrupting the program. Please let me know if I could do it differently with fewer lines of code. Your feedback is very important to me.

Code: [Select]
const int sensorPin = A0;
float baselineTemp = 21.5;//room temperature initial value
int switchState = 0;
void setup() {
  Serial.begin(9600); // open a serial port
  for(int pinNumber = 2; pinNumber<6; pinNumber++){
    pinMode(pinNumber,OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
  pinMode(6, INPUT);
}
void loop() {
  //******
  switchState = digitalRead(6);
  //when switch is pressed set a new value for baselineTemp
  if (switchState == HIGH){
    for(int x=0; x<5; x++){//turn off the red leds
      digitalWrite(x, LOW);
    }
    digitalWrite(5, HIGH);//turn on a yellow led
    delay(pow(12.5,4));//time needed to 'cool down' the sensor ~25 sec
    baselineTemp = (((analogRead(sensorPin)/1024.0*5.0)-.5)*100);//convert sensor voltage in temp and store the new value
    digitalWrite(5, LOW);
  }
  //******
  int sensorVal = analogRead(sensorPin);
  Serial.print("Room Temp: ");
  Serial.print(baselineTemp);
  Serial.print(", Sensor Value: ");
  Serial.print(sensorVal);
  //convert the ADC reading to voltage
  float voltage = (sensorVal/1024.0)* 5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", degrees C: ");
  //convert the voltage to temperature in degrees
  float temperature = (voltage -.5)*100;
  Serial.println(temperature);
  if(temperature < baselineTemp+1.99){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+6){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(5);
}



This is the schematic. I used 560-ohm resistors for the led instead of the 220.


Go Up