#define trigPinA 13
#define echoPinA 12
#define trigPinB 11
#define echoPinB 10
#define trigPinC 9
#define echoPinC 8
// constants won't change. Used here to set a pin number:
const byte RELAY1 = 2;
const byte RELAY2 = 3;
const byte RELAY3 = 4;
// Variables will change:
int RELAY1State = LOW;
int RELAY2State = LOW;
int RELAY3State = LOW;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long RELAY1previousMillis = 0;
unsigned long RELAY2previousMillis = 0;
unsigned long RELAY3previousMillis = 0;
const unsigned int onTime = 100;
// Used to track if LED should be on or off
boolean RELAY2state = true;
boolean RELAY3state = true;
// Interval is how long we wait
int RELAY2interval = onTime;
int RELAY3interval = onTime;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
Serial.begin (9600);
pinMode(trigPinA, OUTPUT);
pinMode(echoPinA, INPUT);
pinMode(trigPinB, OUTPUT);
pinMode(echoPinB, INPUT);
pinMode(trigPinC, OUTPUT);
pinMode(echoPinC, INPUT);
// set the digital pin as output:
pinMode (RELAY1, OUTPUT);
pinMode (RELAY2, OUTPUT);
pinMode (RELAY3, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
//PROXIMITY A
long durationA, distanceA;
digitalWrite(trigPinA, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinA, HIGH);
delayMicroseconds(5); // Added this line
digitalWrite(trigPinA, LOW);
durationA = pulseIn(echoPinA, HIGH);
distanceA = (durationA/2 / 29.1);
//DistanceA averaging:
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = distanceA;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
Serial.print(distanceA);
Serial.println(" cm A");
//PROXIMITY B
long durationB, distanceB;
digitalWrite(trigPinB, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinB, HIGH);
delayMicroseconds(5); // Added this line
digitalWrite(trigPinB, LOW);
durationB = pulseIn(echoPinB, HIGH);
distanceB = (durationB/2) / 29.1;
Serial.print(distanceB);
Serial.println(" cm B");
//PROXIMITY C
long durationC, distanceC;
digitalWrite(trigPinC, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinC, HIGH);
delayMicroseconds(5); // Added this line
digitalWrite(trigPinC, LOW);
durationC = pulseIn(echoPinC, HIGH);
distanceC = (durationC/2) / 29.1;
Serial.print(distanceC);
Serial.println(" cm C");
// here is where you'd put code that needs to be running all the time.
// 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 RELAY1currentMillis = millis();
unsigned long RELAY2currentMillis = millis();
unsigned long RELAY3currentMillis = millis();
//RELAY 1
if (average <60) { // DO FLASH WITHIN 60CM (MIN) DISTANCE
int RELAY1interval = distanceA;
if (RELAY1currentMillis - RELAY1previousMillis >= RELAY1interval) {
// save the last time you blinked the LED
RELAY1previousMillis = RELAY1currentMillis;
// if the LED is off turn it on and vice-versa:
if (RELAY1State == LOW) {
RELAY1State = HIGH;
} else {
RELAY1State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(RELAY1, RELAY1State);
}
}
else if ((average > 80) && (average < 300)) { // TURN ON BETWEEN MAX(300cm)/MIN(80cm)
digitalWrite(RELAY1, LOW);
}
else { // TURN OFF in all other contexts
digitalWrite(RELAY1, HIGH);
}
//RELAY 2
if (distanceB < 260) { // DO FLASH WITHIN 260CM (MIN) DISTANCE
int RELAY2interval = distanceB;
if (RELAY2currentMillis - RELAY2previousMillis >= RELAY2interval) {
// save the last time you blinked the LED
RELAY2previousMillis = RELAY2currentMillis;
// if the LED is off turn it on and vice-versa:
if (RELAY2State == LOW) {
RELAY2State = HIGH;
} else {
RELAY2State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(RELAY2, RELAY2State);
}
}
else { //OTHERWISE 'BLINK' RANDOMLY
int offTime = random(10000,20000);
// Set Pin 13 to state of RELAY3state each timethrough loop()
// If RELAY3State hasn't changed, neither will the pin
digitalWrite(3, RELAY2state);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
//----REMOVED unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(RELAY2currentMillis - RELAY2previousMillis) >= RELAY2interval) {
// Change wait interval, based on current LED state
if (RELAY2state) {
// LED is currently on, set time to stay off
RELAY2interval = onTime;
} else {
// LED is currently off, set time to stay on
RELAY2interval = offTime;
}
// Toggle the LED's state
RELAY2state = !(RELAY2state);
// Save the current time to compare "later"
RELAY2previousMillis = RELAY2currentMillis;
}
}
//RELAY 3
if (distanceC < 260) { // DO FLASH WITHIN 260CM (MIN) DISTANCE
int RELAY3interval = distanceC;
if (RELAY3currentMillis - RELAY3previousMillis >= RELAY3interval) {
// save the last time you blinked the LED
RELAY3previousMillis = RELAY3currentMillis;
// if the LED is off turn it on and vice-versa:
if (RELAY3State == LOW) {
RELAY3State = HIGH;
} else {
RELAY3State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(RELAY3, RELAY3State);
}
}
else { //OTHERWISE 'BLINK' RANDOMLY
int offTime = random(10000,20000);
// Set Pin 13 to state of RELAY3state each timethrough loop()
// If RELAY3State hasn't changed, neither will the pin
digitalWrite(4, RELAY3state);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
//----REMOVED unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(RELAY3currentMillis - RELAY3previousMillis) >= RELAY3interval) {
// Change wait interval, based on current LED state
if (RELAY3state) {
// LED is currently on, set time to stay off
RELAY3interval = onTime;
} else {
// LED is currently off, set time to stay on
RELAY3interval = offTime;
}
// Toggle the LED's state, Fancy, eh!?
RELAY3state = !(RELAY3state);
// Save the current time to compare "later"
RELAY3previousMillis = RELAY3currentMillis;
}
}
}