Errors in Programming

I’m getting such a headache =(…I’m new to Arduino and really don’t know what to do at this point. :~

The point of this project is to take a thermometer to use LEDs to indicate the temperature, as well as outputting the actual temperature value. 20 C is the base temp.
• If the temperature reading is greater than the base temperature, light up one LED.
• If the temperature reading is greater than the base temperature but less than the (base
temperature + 2 o C), light up two LEDs.
• If the temperature reading is greater than or equal to the (base temperature + 2 o C) and
less than (base temperature + 4 o C), light up three LEDs.
• If the temperature reading is greater than or equal to the (base temperature + 4 o C) and
less than (base temperature + 6 o C), light up four LEDs.
• The thermometer must output the temperature in Celsius and in Fahrenheit onto the serial
monitor as it is reading the temperature.
• Have a two second delay between temperature readings.
Remember the linear relationship of our thermoresistor is given in the spec sheet as follows:
10 mV/°C, 750 mV at 25oC (500 mV offset)

Here is my current code

/*

Example sketch 07

TEMPERATURE SENSOR
Version 2.0 6/2012 MDG
*/

// use analog input 0 to measure the temperature sensor's signal pin.

const int temperaturePin = 0;
                        //(500 mV offset) to make negative temperatures an option

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;

void setup()
{
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);

  Serial.begin(9600); //using serial port to send text back to main computer
                      //speed is measured in bits per sec - baud rate.
                      //9600 will transfer ~10 characters per sec
}

void loop()
{

  float voltage, degreesC, degreesF; //declaring 3 float-point variables

  voltage = getVoltage(temperaturePin); //returns the true voltage (o to 5 V) present on an analog input pin

  degreesC = (voltage - 0.5) * 100.0; //convert voltage to degrees C
                                      //converting from 10mv per degree wit 
                                      //500 mV offset to degrees((voltage - 500mV) times 100)
 degreesF = degreesC * (9.0/5.0) + 32.0; 

} 
// if statement here

 if(degreesC > 20){
  digitalWrite(led1, HIGH);
  delay(1000);
}
else if(degreesC > 20 && degreesC < 22){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  delay(1000);
}
else if(degreesC > 22 && degreesC < 24){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  delay(1000);
}
else if(degreesC >24 && degreesC < 26){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(1000);
  
  
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF);

  delay(1000); // repeat once per second
}


float getVoltage(int pin)
{
  // Here's the return statement for this function. We're doing
  // all the math we need to do within this statement:
  
  return (analogRead(pin) * 0.004882814); // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 5.0 value that is the true voltage
  // being read at that pin.
}

I keep getting a “expected unqualified-id before ‘if/else’” Every time I fix something, another error pops up. Please help…

Hello and welcome,

blufrenzy93: // if statement here

You can't put if statements here. You probably want to put it inside loop(), not outside.

So I deleted a “}” under the //if statement here, but now I’m getting - surprise - another error.

/*

Example sketch 07

TEMPERATURE SENSOR
Version 2.0 6/2012 MDG
*/

// use analog input 0 to measure the temperature sensor's signal pin.

const int temperaturePin = 0;
                        //(500 mV offset) to make negative temperatures an option

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;

void setup()
{
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);

  Serial.begin(9600); //using serial port to send text back to main computer
                      //speed is measured in bits per sec - baud rate.
                      //9600 will transfer ~10 characters per sec
}

void loop()
{

  float voltage, degreesC, degreesF; //declaring 3 float-point variables

  voltage = getVoltage(temperaturePin); //returns the true voltage (o to 5 V) present on an analog input pin

  degreesC = (voltage - 0.5) * 100.0; //convert voltage to degrees C
                                      //converting from 10mv per degree wit 
                                      //500 mV offset to degrees((voltage - 500mV) times 100)
 degreesF = degreesC * (9.0/5.0) + 32.0; 
// if statement here

 if(degreesC > 20){
  digitalWrite(led1, HIGH);
  delay(1000);
}
else if(degreesC > 20 && degreesC < 22){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  delay(1000);
}
else if(degreesC > 22 && degreesC < 24){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  delay(1000);
}
else if(degreesC >24 && degreesC < 26){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(1000);
  
  
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF);

  delay(1000); // repeat once per second
}


float getVoltage(int pin)
{

  // Here's the return statement for this function. We're doing
  // all the math we need to do within this statement:
  
  return (analogRead(pin) * 0.004882814); // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 5.0 value that is the true voltage
  // being read at that pin.
}

it says: “a function-definition is not allowed here before ‘{’ token.” It highlighted the “{” underneath the float getVoltage(int pin) at the bottom. There’s also some stuff written in orange underneath the error message, but I’m not sure if I’m supposed to do anything with it.

Your missing another closing bracket } before your float getVoltage function. Also your using the serial port, so you might not want to use digital pin 1, for your LEDs. Shift them over and start from pin 2, and update code.

You can also shrink your code if you use the map function and some FOR loops.

In addition to what [u]HazardsMind[/u] said, here's a little tip for you.

Every left curly brace { requires a right curly brace } If you click on Tools in the IDE, and select Auto Format, you will cause your code to format with proper indents, making it easy to figure out what's enclosed in the curly braces. If you have too many left ones or too many right ones, you will be told, and it's usually fairly easy to scan down the code to figure out which one is missing, and where to put it.

Yay ! No more errors !..kinda…lol

The coding was successfully uploaded, however I’m not getting any readings in the serial port (no temperature readings).

/*

Example sketch 07

TEMPERATURE SENSOR
Version 2.0 6/2012 MDG
*/

// use analog input 0 to measure the temperature sensor's signal pin.

const int temperaturePin = 0;
                        //(500 mV offset) to make negative temperatures an option

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;

void setup()
{
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);

  Serial.begin(9600); //using serial port to send text back to main computer
                      //speed is measured in bits per sec - baud rate.
                      //9600 will transfer ~10 characters per sec
}

void loop()
{

  float voltage, degreesC, degreesF; //declaring 3 float-point variables

  voltage = getVoltage(temperaturePin); //returns the true voltage (o to 5 V) present on an analog input pin

  degreesC = (voltage - 0.5) * 100.0; //convert voltage to degrees C
                                      //converting from 10mv per degree wit 
                                      //500 mV offset to degrees((voltage - 500mV) times 100)
 degreesF = degreesC * (9.0/5.0) + 32.0; 
// if statement here

 if(degreesC > 20){
  digitalWrite(led1, HIGH);
  delay(1000);
}
else if(degreesC > 20 && degreesC < 22){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  delay(1000);
}
else if(degreesC > 22 && degreesC < 24){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  delay(1000);
}
else if(degreesC >24 && degreesC < 26){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(1000);
  
  
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF);

  delay(1000); // repeat once per second
}
}

float getVoltage(int pin)
{

  // Here's the return statement for this function. We're doing
  // all the math we need to do within this statement:
  
  return (analogRead(pin) * 0.004882814); // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 5.0 value that is the true voltage
  // being read at that pin.
}

As I mentioned, curly braces must match, and Auto Format will help you figure out what’s enclosed by them. Check the code in your sketch that says…

  else if(degreesC >24 && degreesC < 26){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);
    delay(1000);

    Serial.print("  deg C: ");
    Serial.print(degreesC);
    Serial.print("  deg F: ");
    Serial.println(degreesF);
    delay(1000); // repeat once per second
  }

This says that you will only print the temperature if it’s above 24 degrees C, and below 26 degrees C. You need to move your Serial.print stuff aout of that block of code.

Buy or download a book on basic C++ programming, so you can understand what you are doing, instead of writing random nonsense and then complaining that it doesn't work.

else if(degreesC > 22 && degreesC < 24){
...
}
else if(degreesC >24 && degreesC < 26){
...

You don’t need to test for both boundary conditions every time, since you know that it has already failed the preceding condition.

if(degreesC < 22)
{
}
else if(degreesC < 24)
{
    // we know it is >= 22 and < 24
}
else if(degreesC < 26)
{
    // we know it is >= 24 and < 26
}

Given the obvious relationship between the thresholds and the fact that there is a pin for each threshold, it would be cleaner to replace that long and repetitive if-else code with something that calculates the number of pins to turn on, and then turns them on. This would be reduced to a couple of lines of code by putting the LED pin numbers into an array.

const int LED_PINS[] = {  2, 3, 4, 5, 6 };
const int LED_COUNT = sizeof(LED_PINS) / sizeof(LED_PINS[0]);
const int BASE_TEMP = 20;
int ledsOn = (degreesC - BASE_TEMP) / 2;
for(int i = 0; i < ledsOn; i++)
{
    digitalWrite(LED_PINS[i], HIGH);
}

Also note that it’s best not to use pins 0 and 1 if possible since these are also used for serial communication with the PC.

PeterH: Also note that it's best not to use pins 0 and 1 if possible since these are also used for serial communication with the PC.

Um, he's not.

@cjdelphi She was actually, originally she was using pin 1 for her LED1. Its in her earlier post.

Edited.

@michinyon

No need to be like that. I'm not on here complaining. As I stated in my post, I'm new to Arduino and haven't dealt with C++ programing before. The closest I've dealt with was html coding (website building), and that was more than five years ago. I'm currently in an intro bioengineering class working (or finishing) my second assignment. I had little problems with the first assignment, but this one is more complex to me.

Btw, I'm a "she" not a "he." lol :)

@PeterH

Your tip just flew right over my head ! lol I guess I understand not having to define each point over and over again, but I'm still trying to get used to the basics.

Thanks for all the tips. As of now, I did get my code to stop showing errors. There is something wrong with my actual coding that a classmate and I can't figure out. I mean, the coding lights up an LED, but it doesn't work as intended. My classmate compared his code to mine - both are the same exact thing (he used different ports). I dunno...he knows a lot more than I do ! At this point, I dunno if I want to keep bugging you guys with this.

Also, I don't mean to come off as "complaining" as I'm actively trying to understand Arduino. I'm reading "Getting Started with Arduino."

I mean, the coding lights up an LED, but it doesn’t work as intended.

Im guessing the LEDs are not turning off when the temperature goes back down, am I right?

If so, try this. Please note you are needed to fix this to fit to your codes needs.

int j=0, Pin_num = 0;
const byte pins[4] = { // MY LEDS, YOU NEED TO CHANGE THIS FOR YOUR LEDS.
  2,3,4,13 };

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

  for(int i = 0; i < 4; i++) {
    pinMode(pins[i],OUTPUT);
  }
}

void loop()
{
  int data = analogRead(A0); // NOT NEEDED FOR YOU
  j = map(data, 0, 1023, 0, 4); // CHANGE THIS, to what PeterH gave you for "ledsOn"
  
  //Serial.println(Pin_num);
  if(Pin_num <= j) {  // This will turn the LEDs on
    for(Pin_num; Pin_num < j; Pin_num++) {
      digitalWrite(pins[Pin_num], HIGH);
    }
  }
  else {
    for(Pin_num; Pin_num >= j; Pin_num--) { // This will likewise, turn them back off.
      digitalWrite(pins[Pin_num], LOW);
    }
  }
}

No, I meant that by using my code (I’ll post it under this), only one LED is lit when at least all four are supposed to be on due to room temp. When I was in class today, most people’s board had all four LEDs lit. If the thermistor was cooler, then the number of lit LEDs would decrease.
-my code isn’t as sophisticated as what I’ve seen so far on here…-

const int temperaturePin = 0;
//int led1 = 2;
//int led2 = 3;
//int led3 = 4;
//int led4 = 5;
void setup()
{
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  Serial.begin(9600); 
}
void loop()
{
  float voltage, degreesC, degreesF; //declaring 3 float-point variables
  voltage = getVoltage(temperaturePin); //returns the true voltage (o to 5 V) present on an analog input pin
  degreesC = (voltage - 0.5) * 100.0; //convert voltage to degrees C

  degreesF = degreesC * (9.0/5.0) + 32.0;
  // if statement here
  if(degreesC > 20){          //temp reading greater than base temp (20)
    digitalWrite(5, HIGH);    //1 LED lights up
    delay(2000);              //2 sec delay
  }
  else if(degreesC > 20 && degreesC < 22){  //temp reading greater than base temp (20) but less than 22
    digitalWrite(5, HIGH);                  //2 LEDs light up
    digitalWrite(4, HIGH);
    delay(2000);                            //2 sec delay
  }
  else if(degreesC >= 22 && degreesC < 24){  //temp reading greater than or equal to 22 but less than 24
    digitalWrite(5, HIGH);                   //3 LEDs light up
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH);
    delay(2000);                            //2 sec delay
  }
  else if(degreesC >=24 && degreesC < 26){  //temp reading greater than or equal to 24 but less than 26
    digitalWrite(5, HIGH);                  //4 LEDs light up
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);
    delay(2000);                            //2 sec delay
  }
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF);
  delay(2000);
}
float getVoltage(int pin)
{

  return (analogRead(pin) * 0.004882814); // This equation converts the 0 to 1023 value that analogRead()

}

@HazardsMind You are so far ahead of me in terms of skill ! I tested out your code just to see what it would do and all four LEDs would be lit until I touched my computer ! They would turn back on when I lifted my hand. Very interesting. I really wish I understood your functions ( I haven’t learned some of those yet).

I wrote my own little thermostat app to switch on a heater when it got cold… it’s similar to above but i might have put something in you may want to use for your own ?..

#include <OneWire.h>
#include <stdlib.h>
#include <DallasTemperature.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
//#include <Wire.h>
//#include <Adafruit_BMP085.h>
#define RelayPin 7
#define Rled 3
#define Gled 5
#define Bled 6
#define b2 8
#define sbled 9
#define PowerPin 12

boolean OldPower,Power = true;
float OldTemp;
boolean Relay = false;
boolean RelayM = false;
byte LightLevels[] = {0,10,50,80,100,150,200,225,255};
byte LL = 0;

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
LiquidCrystal_I2C lcd(0x27,16,2);
DallasTemperature sensors(&oneWire);


void setup(void)
{
  // start serial port
  pinMode(Rled,OUTPUT);
  pinMode(Gled,OUTPUT);
  pinMode(Bled,OUTPUT);
  pinMode(RelayPin,OUTPUT);  
  pinMode(8,INPUT);
  pinMode(sbled,OUTPUT);
  pinMode(b2,INPUT);
  pinMode(Power,INPUT);
  analogWrite(Rled,0);
  analogWrite(Gled,0);
  analogWrite(Bled,0);  

 for (int i=1; i<10; i++)   //3 pin RGB test
 { 
  analogWrite(Rled,225);
  delay(20);     
  analogWrite(Rled,0);  
  delay(20);     
  analogWrite(Gled,225);
  delay(20);     
  analogWrite(Gled,0);  
  delay(20);     
  analogWrite(Bled,225);
  delay(20);     
  analogWrite(Bled,0);  
  delay(20);       
 }
   
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  // Print a message to the LCD.
/*  for (int n=1; n<55;) 
   {
    n++;
    lcd.backlight();
    delay(100);
    lcd.noBacklight();  
    delay(100);  
   }*/
 // attachInterrupt(0,TriggerIRQ,RISING);
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

// if (!bmp.begin())
 //   Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  // Start up the library
  sensors.begin();
}



void loop(void)
{ 
  sensors.requestTemperatures(); // Send the command to get temperatures
  float T;
  char TStr[5];

// T= 21;
 // Serial.println(T);

  T = sensors.getTempCByIndex(0);  

  if (T<10)  //this is for a quick visual status 
   {
     //relay on, heater on.
 //    Serial.println("freezing cold");     
     analogWrite(Rled,0);
     analogWrite(Gled,0);     
     analogWrite(Bled,220);     
   }

   if ((T>=10) &(T<19))
    {
//      Serial.println("cold");     
     analogWrite(Rled,0);
     analogWrite(Bled,225);
     analogWrite(Gled,225);
    }

   if ((T>=19) & (T<26))
    {
 //    Serial.println("warm");     
     analogWrite(Rled,0);
     analogWrite(Gled,225);
     analogWrite(Bled,0);     
    }
    
   if ((T>=26))
    {
 //     Serial.println("hot");     
     analogWrite(Rled,225);
     analogWrite(Gled,0);
     analogWrite(Bled,0);     
     }

    if (RelayM==true)  //button press detected (although holding down is required depends on luck)
    {
        if (T>=20.0)  //max temp to reach with override button
       {
         RelayM=false;
         digitalWrite(RelayPin,RelayM);      
        } //heater off
    } 
     
     
  //pin 8 connects to a button to override and turn on the heater
  //ideally, I want to trigger this quicker, so a delay may have to go

  Power=digitalRead(PowerPin);
 if ((Power==LOW)) 
   {
      LL=2;
      RelayM=false;
      digitalWrite(RelayPin,RelayM); 
      analogWrite(sbled,LightLevels[LL]);
      delay(80);
      OldPower=Power;
   }



   if (digitalRead(2)==HIGH) 
   {
      delay(80);
      RelayM=!RelayM;
      digitalWrite(RelayPin,RelayM);
   }
   
    if (digitalRead(b2)==HIGH) 
   {
  //   Serial.println("Boo LL = ");
      delay(80);     
     LL=LL+1;      
     if (LL>=sizeof(LightLevels)) 
        LL=0;
      analogWrite(sbled,LightLevels[LL]);
      lcd.clear();  //i2c library LCD              
      lcd.setCursor(0,0);
      lcd.print("Brightness: ");  
      lcd.setCursor(1,1);
      lcd.print(LightLevels[LL]);
      lcd.println(" of 255 MAX");  
      delay(987);      

      OldTemp=0;
//Serial.println(LL);
   }

  if ((T>=18.5) && (RelayM==false)) //if override is off do normal operation
    {
      Relay=false; //turn off relay
      digitalWrite(RelayPin,Relay);      
    } //heater off
   else
  if (T<=16.5)
    {
      Relay=true;  //turn on relay as it's cold
      digitalWrite(RelayPin,Relay);
    }
    
//    Serial.println(T);
//    Serial.println(OldTemp);    
  if (OldTemp!=T) 
   {     
     dtostrf(T,5, 3, &TStr[0]);
     TStr[5]=0;   
     lcd.clear();  //i2c library LCD              
     lcd.setCursor(0,0);
     lcd.print("The Temperature:");  
     lcd.setCursor(1,1);
     lcd.print(TStr);
     lcd.println(" Degrees C");  
     delay(55);      
   }   
     OldTemp = T;      
     
   
}

I used a RGB LED to indicate how cold/warm it is, a relay pin to switch on a electric heater.

BUT!

I used the dallas temp sensor not a thermistor, so you’d have to literally strip lines away of the code to (search and replace is handy for quickly renaming variables)

@HazardsMind You are so far ahead of me in terms of skill ! I tested out your code just to see what it would do and all four LEDs would be lit until I touched my computer ! They would turn back on when I lifted my hand. Very interesting. I really wish I understood your functions ( I haven't learned some of those yet).

What functions don't you know? The map() function? all that does is scale down you input to manageable values.

I was using a potentiometer to test the LEDs, so I used the map function to take the values from 0 - 1023 from the pot, and scale them down to 0 - 4 as an manageable output value. Then I used that value with the aid of a FOR loop, to control which LEDs turn ON or OFF.