Confusing for us too, since the code is invisible to us. The loop() function found in every Arduino program repeats. Anything written inside it will repeat, unless the code includes something that stops.
By the way, in my opinion, YouTube is one of the worst places to go to learn programming.
/*
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
*/
// constants won't change. They're used here to
const unsigned long solenoid_1_OffDelay = 4000; //4 seconds
const unsigned long solenoid_2_OnDelay = 1000; //1 second
const unsigned long solenoid_2_OffDelay = 5500; //1 second
const unsigned long solenoid_3_OnDelay = 5000; //5 seconds
const unsigned long solenoid_3_OffDelay = 1000; //1 second
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int solenoid1Pin = 4; // solenoid 1 pin
const int solenoid2Pin = 5; // solenoid 2 pin
const int solenoid3Pin = 6; // solenoid 3 pin
// Variables will change:
unsigned long solenoid_1_Time = 0; //time for solenoid #1
unsigned long solenoid_2_Time = 0; //time for solenoid #2
unsigned long solenoid_3_Time = 0; //time for solenoid #3
int buttonState = HIGH; //the current reading from the input pin
int lastButtonState = HIGH; //the previous reading from the input pin
int isCycleRunning = LOW; //state of the cycle
int runSolenoid_1 = LOW; //activate solenoid #1
int runSolenoid_2 = LOW; //activate solenoid #2
int runSolenoid_3 = LOW; //activate solenoid #3
int stepSolenoid_1 = 0; //steps of solenoid #1
int stepSolenoid_2 = 0; //steps of solenoid #2
int stepSolenoid_3 = 0; //steps of solenoid #3
int solenoid_1_done = LOW;
int solenoid_2_done = LOW;
int solenoid_3_done = LOW;
// 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.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(solenoid1Pin, OUTPUT);
pinMode(solenoid2Pin, OUTPUT);
pinMode(solenoid3Pin, OUTPUT);
// set initial outputs state
digitalWrite(solenoid1Pin, LOW);
digitalWrite(solenoid2Pin, LOW);
digitalWrite(solenoid3Pin, LOW);
}
void loop() {
// 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) {
isCycleRunning = !isCycleRunning;
runSolenoid_1 = HIGH;
}
}
}
if(!isCycleRunning){
// reset outputs state
digitalWrite(solenoid1Pin, LOW);
digitalWrite(solenoid2Pin, LOW);
digitalWrite(solenoid3Pin, LOW);
//reset the cycle
stepSolenoid_1 = 0;
stepSolenoid_2 = 0;
stepSolenoid_3 = 0;
runSolenoid_2 = LOW;
runSolenoid_3 = LOW;
solenoid_1_done = LOW;
solenoid_2_done = LOW;
solenoid_3_done = LOW;
}
//solenoid #1
switch(stepSolenoid_1) {
case 0: //start when the button is pressed
if (runSolenoid_1)
stepSolenoid_1 = 10;
break;
case 10: //solenoid #1 activates
digitalWrite(solenoid1Pin, HIGH);
runSolenoid_2 = HIGH;
runSolenoid_3 = HIGH;
stepSolenoid_1 = 11;
solenoid_1_Time = millis();
break;
case 11: //wait 4 seconds then turns solenoid #1 off
if(millis() - solenoid_1_Time >= solenoid_1_OffDelay)
stepSolenoid_1 = 99;
break;
case 99:
digitalWrite(solenoid1Pin, LOW);
runSolenoid_1 = LOW;
stepSolenoid_1 = 0;
solenoid_1_done = HIGH;
break;
default:
digitalWrite(solenoid1Pin, LOW);
runSolenoid_1 = LOW;
stepSolenoid_1 = 0;
solenoid_1_done = HIGH;
}
//solenoid #2
switch(stepSolenoid_2){
case 0: //run solenoid #2 cycle
if(runSolenoid_2){
stepSolenoid_2 = 10;
solenoid_2_Time = millis();
}
break;
case 10: //on delay
if (millis() - solenoid_2_Time >= solenoid_2_OnDelay){
stepSolenoid_2 = 20;
}
break;
case 20:
digitalWrite(solenoid2Pin, HIGH);
stepSolenoid_2 = 30;
solenoid_2_Time = millis();
break;
case 30:
if (millis() - solenoid_2_Time >= solenoid_2_OffDelay){
stepSolenoid_2 = 99;
}
break;
case 99:
digitalWrite(solenoid2Pin, LOW);
stepSolenoid_2 = 0;
solenoid_2_Time = millis();
runSolenoid_2 = LOW;
solenoid_2_done = HIGH;
break;
default:
digitalWrite(solenoid2Pin, LOW);
stepSolenoid_2 = 0;
solenoid_2_Time = millis();
runSolenoid_2 = LOW;
solenoid_2_done = HIGH;
}
//solenoid #3
switch(stepSolenoid_3){
case 0: //run solenoid #2 cycle
if(runSolenoid_3){
stepSolenoid_3 = 10;
solenoid_3_Time = millis();
}
break;
case 10: //on delay
if (millis() - solenoid_3_Time >= solenoid_3_OnDelay){
stepSolenoid_3 = 20;
}
break;
case 20:
digitalWrite(solenoid3Pin, HIGH);
stepSolenoid_3 = 30;
solenoid_3_Time = millis();
break;
case 30:
if (millis() - solenoid_3_Time >= solenoid_3_OffDelay){
stepSolenoid_3 = 99;
}
break;
case 99:
digitalWrite(solenoid3Pin, LOW);
stepSolenoid_3 = 0;
solenoid_3_Time = millis();
runSolenoid_3 = LOW;
solenoid_3_done = HIGH;
break;
default:
digitalWrite(solenoid3Pin, LOW);
stepSolenoid_3 = 0;
solenoid_3_Time = millis();
runSolenoid_3 = LOW;
solenoid_3_done = HIGH;
}
if (isCycleRunning && solenoid_1_done && solenoid_2_done && solenoid_3_done){
isCycleRunning = LOW;
solenoid_1_done = LOW;
solenoid_2_done = LOW;
solenoid_3_done = LOW;
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
The basic issue that I see - is that you wait for the push button every time you enter loop()
Your requirement is a bit vague, but if you really just want to wait for the button ONCE for each reset of the processor, simply move the button sensing code up into setup ().
That will reset, then wait for the. button - then fall into the repeating loop()
Did you follow and read the links that were provided in the previous message?
I'm confused because you appear to have written several pages of code for a custom built machine, yet haven't a clue about the most basic Arduino function that exists. How did the project actually arrive at the current state? Did you not write any prior, simpler test sketches or "learning" sketches?
Of course I'm misguided, I'm not a programmer and this is all new. Yes, I read the viewed the links. I'm still trying to understand what I'm reading.
I did not write the code. I hired someone on Fiverr to do it. The project is already completed and I later decided to add the repeat function. I thought it might be something that I could do but that does not appear to be the case.
I did not write the code. I hired someone on Fiverr to do it. The project is already completed and I later decided to add the repeat function. I thought it might be something that I could do but that does not appear to be the case.