Anyone know of a good Youtube tutorial on how to make a code repeat?

I'm pulling my hair out trying to figure out how to make the code repeat after it finishes without having to press the joystick again.

Searched on Youtube and tried several tutorials but it I can't get it to work. This is my first Arduino project ever and it is a little confusing.

Thank you much,
Dave

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.

3 Likes

If I were you, I'd use web pages instead of youtube videos.

What is your code?

1 Like




/*
 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;
}

It's for this machine I just finished building.

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()

I would like to press the joystick once and have the code run indefinitely, until I unplug the power supply.

I'm not familiar with the terminology. You said "simply move the button sensing code up into setup ()." Is there a tutorial on how to do that?

Usually the main loop() repeats forever. Look at the Blink Example.

Plus there are 3 other types of loops in C++.

for-loops are good if you want to do something 2 times or a million times.

A while loop() runs as long as some condition is true, such as while a button is pushed or while a button isn't pushed.

Or you can make a "do nothing" while() loop that just sits there looping (effectively pausing the program) until something happens.

A do while() loop is similar to a while loop except it always runs at least once.

2 Likes

Does that mean if I enter "5" inside "loop( )" that the code will repeat 5 times?
For example: loop (5)

You are seriously misguided. Please read the documentation:
https://www.arduino.cc/reference/en/language/structure/sketch/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?

1 Like

No!

The loop() function loops indefinitely. To repeat x times, do this:

for(i=0; i<x; i++){ // x would be times to repeat; "i" can be used inside for loop to find loop# (zero indexed)
    // code to repeat
}

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.

1 Like

You didn't mention anything about adding repetition until now. So if you need help with that, you need to specify exactly what you want to repeat.

1 Like

Thank you so much. I will try it now.
Am I able to upload a video to the forums to show the machine in use or are only pictures allowed?

It will not work with the non-blocking code that is presented. Sorry.

1 Like

You wrote the existing code…
Take the digitalRead(), and the if() block out of loop(), and insert it into setup().

Leave the functional code in loop(), that will run forever until the cpu is reset or restarted.

This is a good learning experience for you.

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.

Read his posts. Someone wrote it for him, he wants changes but understands nothing.
Good luck.
C

2 Likes

Why! It's much better to learn to code yourself. Otherwise, have the person on Fiverr go on Arduino Forum to fix the "problem"

Not helpful and doesn't make sense.

I'm a machinist, not a coder.

Wanted a revision done but since the project was completed, I didn't want to bother him and thought I could do it myself.