I’m having a problem with my code. My hopes were to have a code that would make a light flash whenever an object was farther than 20 cm away.
Here is my code:
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 300
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int led = 5;
void setup()
{
digitalWrite(led, HIGH);
delay(1000);
}
void loop()
{
delay(100);
unsigned int uS = sonar.ping();
if(uS / US_ROUNDTRIP_CM >= 20);
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
else if(uS / US_ROUNDTRIP_CM <= 20)
{
digitalWrite(led, LOW);
delay(100);
}
}
And here are my errors:
NewPingExample.ino: In function ‘void loop()’:
NewPingExample:33: error: ‘else’ without a previous ‘if’
The “error” is highlited
I can’t figure out why it would say this. Can you help?