LED and temperature measurment

Hello Guys,

I just bought the Genuino Starter Kit, and am currently working on project 3: Love-o-meter.
What I have done so far is:

  • If the temperature is increased by 2 degrees Celcius, 1 LED lights up.
  • If the temperature is increased by 4 degrees Celcius, 2 LED lights up.
  • If the temperature is increased by 6 degrees Celcius, 3 LED lights up.

But I would like to add my own touch to the program: If the temperature is increased by 8 degrees Celcius, all 3 LED’s should blink, and keep blinking, as long as the temperature is at least 8 degrees higher than the initial temperature. How can that be added to the code? I know that I should do something with the digitalwrite-command and a few delays, but I just cant wrap my mind around it!

This is my code so far:

//DEFINING CONSTANTS:
const int sensorPin = A0;                      //ANALOG INPUT 0
const float baselineTemp = 21.0;

void setup() {
  Serial.begin(9600);                         //OPEN A SERIAL PORT. THIS COMMAND OPENS A CONNECTION BETWEENT THE MCU AND THE COMPUTER.  
                                              //THAT ENABLES US TO SEE THE VALUES FROM THE ANALOG INPUT [0]. COMMUNICATION SPEED OF GENUINO IS SET TO 9600 BITS/S

  for(int pinNumber = 2; pinNumber < 5; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
}                                 

void loop() {
  int sensorVal = analogRead(sensorPin);      //sensorVal IS A LOCAL VARIABLE THAT STORES THE READING FROM THE SENSOR
  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:
  float temperature = (voltage-0.5)*100;
  Serial.println(temperature);

  if(temperature < baselineTemp+2){ 
    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);
        //IF TEMPERATURE IS INCREASED BY 2 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 1 LED TURNS ON.
  }
      else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        //IF TEMPERATURE IS INCREASED BY 4 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 2 LED TURNS ON.
  }
      else if(temperature >= baselineTemp+6){
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        //IF TEMPERATURE IS INCREASED BY 6 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 3 LED TURNS ON.
  }
  else if(temperature >= baselineTemp+6 && temperature < baselineTemp+8){
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
  }
  delay(1000);
  //MEASURE PR. 1 SECOND
  }

Subsequent else's get ignored if one is satisfied.

  int temperature = goGetTemperature;
  if(temperature > setpoint + 8)
  {
    blink();
  }
  else if (temperature > setpoint + 6)
  {
    
  }
  else if(temperature > setpoint + 4)
  {
    
  }
  else if (temperature > setpoint + 2)
  {
    
  }
  else
  {
    //turn led off
  }

@Gandhara, please do not cross-post. Other thread removed.

BulldogLowell:
Subsequent else’s get ignored if one is satisfied.

  int temperature = goGetTemperature;

if(temperature > setpoint + 8)
  {
    blink();
  }
  else if (temperature > setpoint + 6)
  {
   
  }
  else if(temperature > setpoint + 4)
  {
   
  }
  else if (temperature > setpoint + 2)
  {
   
  }
  else
  {
    //turn led off
  }

@BulldogLowell

But shouldn’t I be able to but a 4. else-if statement, after the 3 one, looking like this:

else if(temperature >= baselineTemp+6 && temperature < baselineTemp+8){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
And I still can’t figure out how to make all 3 LED’s blink. What should go into the paranthesis in the blink() command?

You're expected to write a function like:

void blink()
{ ...}

Look up functions.

Or, you can substitute your blink code for the blink().

Example:

if (digitalRead(2) == LOW)
  digitalWrite(2, HIGH);
else
  digitalWrite(2, LOW);
[... the same for the other two LEDs ...]
delay(500);

Disclaimer: this won't blink all the LEDs properly because some of them might be in different states. I leave it as an exercise to fix that. :slight_smile:

@aarg

Like this:

//DEFINING CONSTANTS:
const int sensorPin = A0;                      //ANALOG INPUT 0
const float baselineTemp = 21.0;

void setup() {
  Serial.begin(9600);                         //OPEN A SERIAL PORT. THIS COMMAND OPENS A CONNECTION BETWEENT THE MCU AND THE COMPUTER.  
                                              //THAT ENABLES US TO SEE THE VALUES FROM THE ANALOG INPUT [0]. COMMUNICATION SPEED OF GENUINO IS SET TO 9600 BITS/S

  for(int pinNumber = 2; pinNumber < 5; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
}                                 

void loop() {
  int sensorVal = analogRead(sensorPin);      //sensorVal IS A LOCAL VARIABLE THAT STORES THE READING FROM THE SENSOR
  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:
  float temperature = (voltage-0.5)*100;
  Serial.println(temperature);

  if(temperature < baselineTemp+2){ 
    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);
        //IF TEMPERATURE IS INCREASED BY 2 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 1 LED TURNS ON.
  }
      else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        //IF TEMPERATURE IS INCREASED BY 4 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 2 LED TURNS ON.
  }
      else if(temperature >= baselineTemp+6){
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
if (digitalRead(2) == LOW)
  digitalWrite(2, HIGH);
else
  digitalWrite(2, LOW);

if (digitalRead(3) == LOW)
  digitalWrite(3, HIGH);
else
  digitalWrite(3, LOW);

if (digitalRead(4) == LOW)
  digitalWrite(4, HIGH);
else
  digitalWrite(4, LOW);
        //IF TEMPERATURE IS INCREASED BY 6 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 3 LED TURNS ON.
  }
  
  delay(1000);
  //MEASURE PR. 1 SECOND
  }

It's still not working....

You have 3 LEDs, and each can either be in state 1 or 0. So, it is more efficient and elegant (not to mention more readable and less error-prone) to express the state of all 3 LEDs as a single octal digit. Write a function that will take an octal digit as an argument and set the LEDs accordingly. Then, make a blinking function that alternatively calls the LED lighting function with arguments 0 and 7. Try to avoid blocking delays: they tend to cause all sorts of problems.