/* Using a 1-Channel 12V H/L Level Optocoupler Relay since my solenoid is also 12V. VCC is +12V from power supply, IN to pin 4, and ground to arduino ground. I used a cheap $5 12V solenoid from eBay - check to make certain you power supply puts out enough amps to open the solenoid valve. */ #define trigPin 7 // TRIG Pin on HC-SR04 Ultrasonic Module #define echoPin 6 // ECHO Pin on HC-SR04 Ultrasonic Module #define relay 9 //relay IN pin #define BlueLed 10 // RGB Common Cathode LED #define GreenLed 11 #define RedLed 12 #define buttonPin 2 // Delay button int buzzPin = 3; //connect buzzer on digital pin 3 int count; //Used to keep track of how many times air is fired so you know when to refill tank int MaxBlasts = 12; //Change this to how many blasts your air tank is good for int blinkrate; int Zone1 = 16; // Getting this close (inches) to sensor triggers slow LED blink int Zone2 = 10; // Getting this close (inches)to sensor triggers faster LED blink and buzzer int Zone3 = 6; // At this distance (inches) there is a steady warning tone and then the solenoid fires int AirBurst = 75; //How long solenoid fires for int buttonState; // variable to store button state void setup() { count = 0; //Everytime arduino is reset to keep track of air bursts pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway pinMode(buzzPin, OUTPUT); digitalWrite(buzzPin,HIGH); digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(relay, OUTPUT); pinMode(BlueLed, OUTPUT); pinMode(GreenLed, OUTPUT); pinMode(RedLed, OUTPUT); } void loop() { buttonState = digitalRead(buttonPin); // read the button state and store //Reading the HC-SR04 Ultrasonic Module long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/58.138) * .39; //Calculates distance in inches while (buttonState == LOW) { // Pressing the button will not allow solenoid to fire // Handy for allowing you to get into "banned" area digitalWrite(GreenLed,HIGH); delay(500); digitalWrite(GreenLed,LOW); // Doesn't actually blink but was needed to stay in this button pressed loop buttonState = digitalRead(buttonPin); } if (distance > Zone1) blinkrate = 0; //Nothing in zone no blinking LED - not really needed but here in case you want to add a function if (Zone2 < distance && distance <= Zone1) { // Somethng enters Zone1 - Slow LED blink no beeping blinkrate = 500; digitalWrite(BlueLed,HIGH); //digitalWrite(buzzPin, LOW); //uncomment to add a beep delay(blinkrate); //digitalWrite(buzzPin, HIGH); digitalWrite(BlueLed,LOW); delay(blinkrate); } if (Zone3 < distance && distance <= Zone2){ //Something enters Zone2 - Faster LED blink and beeping blinkrate = 250; digitalWrite(BlueLed,HIGH); digitalWrite(buzzPin, LOW); delay(blinkrate); digitalWrite(buzzPin, HIGH); digitalWrite(BlueLed,LOW); delay(blinkrate); } if (0 < distance && distance <= Zone3) { //Something enters Zone3, first give a warning tone digitalWrite(GreenLed,HIGH); //Green LED Comes on digitalWrite(buzzPin, LOW); delay(1100); //give tone before air blast - increase delay for longer warning digitalWrite(buzzPin, HIGH); digitalWrite(GreenLed,LOW); //Now check again to see if something is still in Zone 3 //Reading the HC-SR04 Ultrasonic Module again digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/58.138) * .39; //Calculates distance in inches } if (0 < distance && distance <= Zone3){//If something is still in zone 3 after warning tone now fire solenoid digitalWrite(relay, HIGH); //Solenoid Fires delay(AirBurst); //How long Solenoid fires for digitalWrite(relay, LOW); digitalWrite(GreenLed,HIGH); delay(1500); digitalWrite(buzzPin, LOW); delay(200); digitalWrite(GreenLed,LOW); //Quick blink of LED to show it's resetting digitalWrite(buzzPin, HIGH); count ++; //Add 1 to the firing count } if (count >= MaxBlasts) digitalWrite(RedLed,HIGH); //Turns on Red LED when MaxBlasts is reached }