My problem is with the coding at the end of the shetch after the delay of(3000) I cannot get the (ledRed) to turn OFF, it remains ON together with the (sbyRed) led
//Ultrasonic sensor HC-SR04 Dougs
//Pins connected to Sensor
const int trigPin = 2;
const int echoPin = 3;
//Led Pins
const int ledRed = 7;
const int ledYellow = 8;
const int ledGreen = 9;
const int sbyRed = 5;
const int sbyGreen = 6;
//Define Variables
long duration;
long distance;
int range = 600; //Range in centimeters
void setup() {
//initialise serial communication
Serial.begin(9600);
//initialise sensor pins
pinMode (trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Initialise Leds
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(sbyRed, OUTPUT);
pinMode(sbyGreen, OUTPUT);
//Set lEDs
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
digitalWrite(sbyRed, LOW);
digitalWrite(sbyGreen, LOW);
}
void loop() {
//Establish Variables for duration of the ping
// andthe distance result in cm
long duration, cm;
// The ping is triggered by a High Pulse of 2 or more microseconds
//Give a short low pulse beforehandhigh Pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Take reading on Echo Pin
duration = pulseIn(echoPin, HIGH);
distance = duration * .034 / 2;
//Convert Time to a Distance
//cm=Microseconds to Centimeters(duration)
Serial.print("Distance: ");
Serial.println(distance);
Serial.print(" cm");
Serial.println();
if (distance > 100)
{
digitalWrite(sbyGreen, HIGH);
digitalWrite(ledGreen, LOW);
digitalWrite(sbyRed, LOW);
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,LOW);
}
if ((distance >= 70) && (distance <= 100))
{
digitalWrite(ledGreen, HIGH);
digitalWrite(sbyGreen,LOW);
digitalWrite(sbyRed,LOW);
digitalWrite(ledYellow,LOW);
digitalWrite(ledRed,LOW);
}
if ((distance >= 50) && (distance < 70))
{
digitalWrite(ledYellow, HIGH);
digitalWrite(sbyRed,LOW);
digitalWrite(ledGreen,LOW);
digitalWrite(ledRed,LOW);
}
if ((distance >=47)&&(distance<=49))
{
digitalWrite(ledRed, HIGH);
digitalWrite(ledYellow,LOW);
}
delay(3000);
{
digitalWrite(sbyRed,HIGH);
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,LOW);
}
}
}
As mentioned - CTRL-T in the IDE will help you to "see " that.
For now it is irrelevant what it suppose to do - and if it does not compile it obviously won't execute.
First the easy part - make the complier happy than check the program flow / logic.
BTW code blocks {digitalWrite...} indicating common process are nice , adding comments what the process is would help to eliminate " what does it do " questions.
Looks like you're setting up some LED's to go on based on distance meter, or sonar meter?
I used NewPing.h for sonar. I haven't looked at this code in a long time, so hopefully it works. It waits for the change in the distance to be greater than 5. Then pulses a single LED 4 times on/off, But what you SHOULD do is keep an array of 5 last reads, throw away the high and low, then average the remaining three to debounce the crappy sonar. And don't sleep for more than 200ms in the loop, to capture the radar about 5 times a second.
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int lastDistance = 0;
int ledPin = 13;
int loopCtr=0; // used to do inline blinking without delay in processing
int loopCtrIncrement=0;
int trigger=0;
int LEDState=0;
int loopMSDelay=50;
int loopBlinkRate;
int numBlinks=4*2; // number of blinks once we detect movement, 4 + one cycle for ON, another for OFF, so times 2
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
digitalWrite(ledPin, LOW); // sets the LED on
loopBlinkRate=100/loopMSDelay;
}
void doLED(int &counter)
{
if (trigger)
{
digitalWrite(ledPin, LEDState?HIGH:LOW); // sets the LED on
if (counter>loopBlinkRate) // time to change LED state?
{
LEDState=(LEDState+1) & 1; // toggle 0->1 1->0
counter=0;
trigger--;
if (trigger==0)
loopCtrIncrement=0;
Serial.println("Blink");
}
}
else
digitalWrite(ledPin, LOW); // sets the LED on
}
void loop() {
int delta=0;
loopCtr+=loopCtrIncrement; // upped 20 times a second
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_IN); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.print(" in | ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.print(" cm");
delta=(uS/ US_ROUNDTRIP_IN)-lastDistance;
lastDistance=uS / US_ROUNDTRIP_IN;
delta=abs(delta); // Difference from previous led state and now
Serial.print(" | lastDistance: ");
Serial.print(lastDistance);
Serial.print(" | Delta: ");
Serial.print(delta);
Serial.print(" | loopCtr: ");
Serial.print(loopCtr);
Serial.print(" | Trigger: ");
Serial.print(trigger);
Serial.print(" | LEDState: ");
Serial.println(LEDState);
doLED(loopCtr);
if (trigger==0 && delta>5)
{
trigger=numBlinks;
loopCtrIncrement=1;
Serial.println("TRIGGER !!!!");
}
}