Arduino Starter Kit Project 13 Touchy-feely Lamp Error

Hello,

I am working on Project 13 in the Guide Book from the Starter Kit and I have wrote the code exactly as the book says but when I go to upload I get the error message 'else without a previous 'if'. I then proceeded to add the 'if' as it said in the error message, I clicked upload but then prompted me with another error. expected '(' before 'else' so again I add the ( before else click upload and again another error message, expected primary-expression before 'else' and now Im stuck please help.

I attached the .INO below

All error Messages

Arduino: 1.8.10 (Mac OS X), Board: "Arduino/Genuino Uno"

In function 'void loop()':
Arduino_Lamp:21:4: error: 'else' without a previous 'if'
else {
^~~~

Arduino: 1.8.10 (Mac OS X), Board: "Arduino/Genuino Uno"

In function 'void loop()':
Arduino_Lamp:21:7: error: expected '(' before 'else'
if else {
^~~~

Arduino: 1.8.10 (Mac OS X), Board: "Arduino/Genuino Uno"

warning: init-statement in selection statements only available with -std=c++1z or -std=gnu++1z
if (else {
^~~~
Arduino_Lamp:21:8: error: expected primary-expression before 'else'
Arduino_Lamp:25:13: error: expected ')' before ';' token
delay (10);
^
Arduino_Lamp:25:9: error: could not convert 'delay(10)' from 'void' to 'bool'
delay (10);


[Arduino_Touchy-Feely_Lamp.ino|attachment](upload://qozAMcbLeHXFJPuZaejeJGg7hXf.ino) (437 Bytes)

@JakeStobert's Arduino_Touchy-Feely_Lamp.ino:

#include <CapacitiveSensor.h> 
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

int threshold = 1000;
const int ledPin = 12;


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  long sensorValue = capSensor.capacitiveSensor(30);
  Serial.println(sensorValue);

  if(sensorValue > threshold); {
    digitalWrite(ledPin, HIGH);
  }
    if (else {
      digitalWrite(ledPin, LOW);
  }
  
  delay (10);
}

In the future, please post code directly in the forum post, rather than as an attachment. This will make it easier for people to look at the code, and thus make it more likely to get help.

In the event your code exceeds the forum's 9000 character limit, it is OK to post it as an attachment, but this sketch is nowhere near 9000 characters so use of an attachment can only have a harmful effect.


In this line:

  if(sensorValue > threshold); {

You have added an unintended semicolon ( ; ). This causes the code to have a different behavior than you expect. The semicolon terminates the if statement, so your code is the equivalent of:

  if(sensorValue > threshold) {
    ; // do nothing
  }

  {  // pointless brace
    digitalWrite(ledPin, HIGH);  // this line will always execute
  }

You can learn about the correct if syntax here:

Then you have this line:

    if (else {

I suspect you had that one correct originally, but then added the "if (" part in an attempt to fix the error caused by that semicolon.

Feedback noted, Thank you so much.

I've fixed it up for you. This is the proper code. Like the comment above states, you also gotta watch out for putting semicolons at the end of an if statement. Don't do that. But yeah it was complaining about "else without if" because of that semicolon.

#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

int threshold = 1000;
const int ledPin = 12;


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  long sensorValue = capSensor.capacitiveSensor(30);
  Serial.println(sensorValue);

  if(sensorValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } else {
      digitalWrite(ledPin, LOW);
  }
 
  delay (10);
}

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
which are found first in the list of threads.