'else' without a previous 'if'

Hi everyone, I'm brand new to electronics and Arduino, but am keen to get up and running. I have the;

'Super Starter Kit Uno R3 Project'
'37 Sensor Kit v2.0'

I have version 1.8.13 of the Arduino IDE installed on my Windows 10 laptop.

So far I've read all the introductory and setup information and am working my way through the standard examples/lessons.

But I've pretty quickly come unstuck. I've connected the DHT11 (temp&humidity) sensor to the board, loaded the simple standard library and have successful output. Next I wanted to try tweaking it by building on the standard library code.

My intention: set the built-in LED to light when temperature exceeds a certain value and to switch off when it drops below the same value eg 22C. I'm trying to use 'if else' as currently that seems logical to me but get the "'else' without a previous 'if" error.

I've spent some time troubleshooting with different ideas, reading articles and forum posts here and elsewhere all with no success.

Below is my code, the error and a schematic I felt my way through KiCAD to make. Forgive me, I'm pretty sure it's a really simple fix and I'll gladly kick myself, I suspect I'm missing a pair of brackets but I'm just don't know where, I have checked and double checked that the brackets there are at least all paired.

Thanks for your help.

Without seeing your code then providing constructive help is impossible

I will make a wild guess that you have a semicolon at the end of an if statement

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

#include <dht_nonblocking.h>

/* Uncomment according to your sensortype. */
#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );



/*
   Initialize the serial port.
*/
void setup( )
{
  Serial.begin( 9600);
  pinMode(LED_BUILTIN, OUTPUT);
}



/*
   Poll for a measurement, keeping the state machine alive.  Returns
   true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if ( millis( ) - measurement_timestamp > 3000ul )
  {
    if ( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return ( true );
    }
  }

  return ( false );
}



/*
   Main program loop.
*/
void loop( )
{
  float temperature;
  float humidity;
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(LED_BUILTIN, LOW);

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if ( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
  //set LED to light when temp above a set temperature
  if
  (measure_environment); float (temperature >= 22); {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    (measure_environment); float (temperature <= 22); {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Error:
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
C:\Users\jpcle_000\sketchbook\CustomDHT_nonblocking\CustomDHT_nonblocking.ino: In function 'void loop()':

CustomDHT_nonblocking:94:5: error: 'else' without a previous 'if'

} else {

^~~~

exit status 1

'else' without a previous 'if'

Sorry Bob, I got caught in the five minute delay due to being a new member. I had all the code properly formatted ready to go, you were quick off the mark which I appreciate. Attached is the schematic also, as advised by the making your first post advice.

Hi,
What is this bit of code supposed to do?

 if
  (measure_environment); float (temperature >= 22); {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    (measure_environment); float (temperature <= 22); {
      digitalWrite(LED_BUILTIN, LOW);
    }

These may help;

Tom... :slight_smile:

  if
  (measure_environment);

As I predicted. A semicolon on the end of an if

The problem is better seen if you Auto Format the code in the IDE

  //set LED to light when temp above a set temperature
  if (measure_environment);
  float (temperature >= 22);
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {

The only code dependant on the if test is the trailing semicolon

  float (temperature >= 22);
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }

is executed unconditionally hence the else clause does not follow on from the if clause and you get an error

Remove the training semicolon and put the dependant code in braces

Hey Tom,

Thanks for the quick reply, that is the bit I wrote, hence why it's confused you I guess, I'm trying to tell it that if the temp value exceeds 22, switch on the builtin LED, if the value drops below 22, switch off the builtin LED.

I'd read the help articles you posted, reading them again I'm still unclear where I'm going wrong at this point. I'm making my way through Bob's suggestion using those articles as well.

Thanks.

Hi,
This is what you need;

if (temperature >= 22.0)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

Tom... :slight_smile:

Thanks very much Tom,

In a weird way I'm quite glad I managed to make it all so over-complicated. It works now :slight_smile:

Onwards and upwards.

__trip :astonished: __

Onwards and upwards.

Do you understand why you got the error with your original code ?

If not, then do not move on until you do, otherwise you will make the same mistake again

Thanks Bob, the misplaced semicolon you highlighted was cutting off the 'else' from the 'if' as they are used to end statements. I think I was also thrown by having additional semi-colons in there but I guess as they are placed inside braces they are self contained and so won't end the entire if statement;

{
    digitalWrite(LED_BUILTIN, HIGH);
  }

I also had a lot of unnecessary code in there getting in the way. I'm not giving up.

I also had a lot of unnecessary code in there getting in the way.

You certainly did, but all better now

Extra semicolons need not be a problem but just don't do it. Auto Formatting your code helps considerably in seeing problems with code blocks as does putting each { and } alone on lines and I have Auto Format configured tp do that for me

I'm not giving up.

Nor should you. Good luck with your future projects

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.