So I try to compile this little program and get the error message and it stops
at:
long duration;
in this sketch:
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
const int ledPin = 13 // the pin that the LED is attached to
long duration;
long distance; // Duration used to calculate distance
int lastsensorDistance = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // see if there's incoming serial data:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance <= 20 && lastsensorDistance >= 40){
digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin,LOW);
} else {
lastsensorDistance = distance;
}
}
{