// Code written by Hertz and Madden on 20/05/2021 const int SolenoidA = 10; // Connect the terminal 'A' of thesolenoid to Digital Pin 10 on the Arduino const int SolenoidB = 11; // Connect the terminal 'B' of the solenoid to Digital Pin 11 on the Arduino const int IRPin = 9; // Connect the output pin of the IR sensor to Digital Pin 9 on the Arduino int IRstate = 0; // Set the initial state of the IR output as 0 void setup() { pinMode(13,OUTPUT); // Set the Digital Pin 13 as an output pinMode(SolenoidA, OUTPUT); // Set the Digital Pin 10 as an output pinMode(SolenoidB, OUTPUT); // Set the Digital Pin 11 as an output pinMode(IRPin,INPUT); // Set the Digital Pin 9 as an input digitalWrite(13,LOW); // Set the status indicator LED to LOW digitalWrite(SolenoidA, LOW); // Set the solenoid valve to its OFF position digitalWrite(SolenoidB, HIGH); delay(500); } void loop() { IRstate = digitalRead(IRPin); // Read the output of the IR sensor and define it as IRstate if(IRstate==HIGH){ // If the IR sensor detects an object within its range digitalWrite(13,HIGH); // Turn on the status indicator light digitalWrite(SolenoidA, HIGH); // Provide a positive pulse to the solenoid valve to open it digitalWrite(SolenoidB, LOW); delay(2000); // latch it for 2 seconds } else if (IRstate==LOW) // If the IR sensor doesn't detect an object in its proximity { digitalWrite(13,LOW); // Turn off the status indicator light digitalWrite(SolenoidA, LOW); // Provide a negative pulse to the solenoid valve to close it digitalWrite(SolenoidB, HIGH); delay(1000); } }