/*
Solenoid cycle
1) press momentary switch.
2) solenoid #1 activates for 4 seconds then turns off.
3) solenoid #2 waits 1 second after solenoid #1 activates, then it activates.
4) solenoid #3 waits 5 seconds after solenoid #1 activates, then it activates for 1 second and turns off.
The circuit:
pushbutton attached to the pin 2 and to the ground
solenoid #1 attached to pin 4
solenoid #2 attached to pin 5
solenoid #3 attached to pin 6
https://forum.arduino.cc/t/anyone-know-of-a-good-youtube-tutorial-on-how-to-make-a-code-repeat/1027637/3
*/
constexpr byte buttonPin = 2; // the number of the pushbutton pin
struct Solenoid {
const uint8_t pin;
const uint32_t onDelay;
const uint32_t offDelay;
byte done;
};
Solenoid solenoid[] {
{4, 0, 4000},
{5, 1000, 5500},
{6, 5000, 1000},
};
constexpr size_t noOfSolenoid = sizeof(solenoid) / sizeof(solenoid[0]); // how many solenoids are set?
// Variables will change:
byte isCycleRunning = LOW; //state of the cycle
uint32_t previousMillis = 0; // time when button was pressed
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
// set initial outputs state
for (auto &i : solenoid)
{
digitalWrite(i.pin, LOW);
pinMode(i.pin, OUTPUT);
i.done = false;
}
}
void startCycle()
{
isCycleRunning = true; // start cycle
previousMillis = millis();
for (auto &i : solenoid) i.done = false; // reset all markers
}
void buttonRead()
{
if (!isCycleRunning && digitalRead(buttonPin) == LOW) startCycle();
}
void solenoidRun()
{
if (isCycleRunning)
{
uint32_t currentMillis = millis();
int doneSolenoids = 0; // counter how many solenoids are done
for (auto &i : solenoid)
{
if (digitalRead(i.pin) == LOW)
{
if (currentMillis - previousMillis > i.onDelay && currentMillis - previousMillis < (i.onDelay + i.offDelay) )
{
// activate at the beginning of the on time, but not after the off time
digitalWrite(i.pin, HIGH);
}
}
else
{
if (currentMillis - previousMillis > (i.onDelay + i.offDelay))
{
// deactivate at the beginning of the offtime
digitalWrite(i.pin, LOW);
i.done = true;
}
}
if (i.done) doneSolenoids++;
}
// check if all solenoids are done
if (doneSolenoids == noOfSolenoid) isCycleRunning = false;
}
}
void loop() {
buttonRead();
solenoidRun();
}
// not used functions
void buttonReadDebounce()
{
byte buttonState = HIGH; //the current reading from the input pin
static byte lastButtonState = HIGH; //the previous reading from the input pin
// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
static unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the bit if the new button state is LOW
if (buttonState == LOW ) {
if (!isCycleRunning) startCycle();
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}