Hi There1
I am setting up a distance monitor using a HCSR04 sensor with Arduino Uno and I want to be able to turn on led's at different distance ranges,
eg distance >100 (turns on 1 led)
distance <100>70
(I can't seem to be able to select a range of between 70 and 100 (turns off the above but lights another when in the range)
distance >49<70 which turns off previous led's and turns on another.
the problem seems to be in the expression eg <49>70 where I want the operation to be TRUE when the distance is anyweare in between 49 and 70.
Thank you in antisipation
Doug
//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);
}
{
if (distance<99);
digitalWrite(sbyGreen,LOW);
digitalWrite(ledGreen, HIGH);
}
}