Hello everyone,
I've been trying to design a system with LEDs / solenoids / and IR photo-interrupters where an LED turns off after being on for 5 seconds and the solenoid is turned on to dispense water right after LED turns off. After that, I would like the LEDs to wait in an idle position and wait for the next signal which would be waiting for 10 seconds.
Ultimately, I would like to vary the wait time by setting some values within an array.
ex)
int wait_interval[5] = {10, 15, 7, 25, 13}
I also want an IR detector which records IR beams breaking on and off in the background, independent of the LEDs and solenoids.
So basically, the schematic is:
LED ON (5s) ---> LED OFF --(True)--> Solenoid On (1s) ---> Solenoid Off --(True) --> WAIT for 10s -->
LED ON (5s) ---> LED OFF --(True)--> Solenoid On (1s) ---> Solenoid Off --(True) --> WAIT for 15s -->
LED ON (5s) ---> LED OFF --(True)--> Solenoid On (1s) ---> Solenoid Off --(True) --> WAIT for 7s -->
etc.
These are some codes I have written down but I can't seem to combine the two sketches in a working fashion / make the solenoid operation dependent on LED turning off. (Right now, I have the led blinking, but that's not what I intend to do...)
I've been looking around and thought using state machines would be useful but I was wondering if I could just code this using state variables.
Any help would be appreciated!
I've been stuck on this for weeks now and it's getting depressing...
// Arduino Sketch 11/5/18
// solenoids / LEDs / IRs
int port_sol = 6;
int port_LED = 3;
int port_IR = 4;
int LED_on_duration = 5000;
unsigned long IRBreakTime;
unsigned long IROffTime;
unsigned long led_previous_time = 0;
boolean previous_State = HIGH;
boolean ledState = LOW;
void setup() {
pinMode(port_LED, OUTPUT);
pinMode(port_IR, INPUT);
pinMode(port_sol, OUTPUT);
Serial.begin(9600);
delay(2000); // wait 2 seconds until program starts
Serial.println(" Hello ");
}
void loop() {
led_operate();
record_IR();
}
void led_operate() {
if (millis() - led_previous_time > LED_on_duration) {
led_previous_time = millis();
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
digitalWrite(port_LED, ledState) ;
}
}
void record_IR() {
int read_Gate = digitalRead(port_IR);
if (read_Gate != previous_State) {
if (read_Gate == LOW) { // IR Broken
// portNum = 1;
IRBreakTime = millis();
Serial.print("IRBreakTime: ");
Serial.println(IRBreakTime);
}
if (read_Gate == HIGH) {
IROffTime = millis();
Serial.print("IROffTime: ");
Serial.println(IROffTime);
}
}
previous_State = read_Gate;
}