I've only ever used arduino one time before now so my code is pieced together from things I've found online and from the arduino examples. I'm using an arduino mega 2560 and 2 HC-SR04 ultrasonic sensors. I'm trying to get 2 LED's to light at 2 different frequencies for 2 different distances. If limit the code to one distance it works fine. Once I try to get it going for both distances it only works for 1 of them. I've been playing around with the first set of code so the second run doesn't match the first.
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int led1 = 12;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
int ledState1 = LOW;
long previousMillis = 0; // will store last time LED was updated
long previousMillis1 = 0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 5000; // interval at which to blink (milliseconds)
long interval1 = 1000;
//pin which triggers ultrasonic sound
const int pingPin1 = 11;
const int pingPin2 = 9;
//pin which delivers time to receive echo using pulseIn()
int inPin1 = 10;
int inPin2 = 8;
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
int safeZone = 10
;
int safeZone1 = 25;
void setup()
{
// initialize serial communication
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
//initializing the pin states
pinMode(pingPin1, OUTPUT);
pinMode(pingPin2, OUTPUT);
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(inPin1, INPUT);
duration = pulseIn(inPin1, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
Serial.print(cm);
Serial.print("cm1");
Serial.println();
delay(50);
if (cm > safeZone && cm < safeZone1)
{
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
}
if (cm < safeZone && cm < safeZone1)
{
if(currentMillis - previousMillis > interval1)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
}
else
{ledState = LOW;
// save the last time you blinked the LED
// previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
// if (ledState == LOW)
// ledState = HIGH;
// else
// ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
delay(20);
//run 2
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(inPin2, INPUT);
duration = pulseIn(inPin2, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
Serial.print(cm);
Serial.print("cm2");
Serial.println();
delay(50);
if (cm < safeZone)
{
if(currentMillis1 - previousMillis1 > interval)
{
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led1, ledState1);
}
}
else
{
// save the last time you blinked the LED
// previousMillis1 = currentMillis1;
// if the LED is off turn it on and vice-versa:
//if (ledState1 == LOW)
//ledState1 = HIGH;
// else
ledState1 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led1, ledState1);
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}