How Can I Force A Loop Restart

Hello There,

I'm very new to arduino, and looking for a quick bit of advice.

I'm working on a lottery numbers project, its fairly basic. Firstly random numbers scroll very quickly with a delay of 50. Then when the button is pressed to "choose" a set of numbers the delay is set very large (1000000).

I then have a second button that I want to "reset" the loop, so it goes back to scrolling quickly with a delay of 50.

It seems to be at the moment, its waiting until the huge delay is over until it changes the delay back again, do you have any idea of another way I can achieve the same results, or to "override" the large delay.

Here is the delay code:

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == HIGH)
    {
    delay(50);
    }
  
  
  
  if (buttonState == HIGH) 
  {
    delay(1000000);
    digitalWrite(ledPin, HIGH);
  }
  else 
  {
    delay(50);
  }

Get rid of the delay() calls and use the concepts demonstrated in the Blink Without Delay example.

The delay() method really needs to be renamed. It should be called something like doNOTHINGButTwiddleYourThumbsForHoweverLong().

Perhaps then you'd understand what your problem is.

You need to look at the blink without delay example, to learn how to get rid of all of your delay() calls.

Thanks for the advice,

I will try have a look through the Blink Without Delay example.

With my little understanding of arduino, I assumed the Delay() function, was more of a universal "setting" for the loops which could change dependant on if conditions.

I will post the full code, It works but I'm certain it can be heavily improved.

#include <LiquidCrystal.h>

long randNumber1;
long randNumber2;
long randNumber3;
long randNumber4;
long randNumber5;
long randNumber6;
long randNumber7;

const int buttonPin = 8;
const int buttonPin2 = 7;
const int ledPin = 11;

int buttonState = 0;
int buttonState2 = 0;

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13;    // pin 13 will control the backlight

void setup()
{
  pinMode(backLight, OUTPUT);        	//set pin 13 as output
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin,INPUT);
  analogWrite(backLight, 200);       	//controls the backlight intensity 0-254
  
  lcd.begin(16,2);                		// columns, rows. size of display
  lcd.clear();                    		// clear the screen

}

void loop()
{
  lcd.clear();                    		// clear the screen
  randNumber1 = random(1, 50);
  randNumber2 = random(1, 50);
  randNumber3 = random(1, 50);
  randNumber4 = random(1, 50);
  randNumber5 = random(1, 50);
  randNumber6 = random(1, 50);
  randNumber7 = random(1, 50);
  
//************Begin First Row******************//
  
//----Repeat Break Point----//

if ((randNumber1 == randNumber2) || (randNumber1 == randNumber3) || (randNumber1 == randNumber4) || (randNumber1 == randNumber5) || (randNumber1 == randNumber6) || (randNumber1 == randNumber7))
  return;
  
if ((randNumber2 == randNumber3) || (randNumber2 == randNumber4) || (randNumber2 == randNumber5) || (randNumber2 == randNumber6) || (randNumber2 == randNumber7))
  return;
  
if ((randNumber3 == randNumber4) || (randNumber3 == randNumber5) || (randNumber3 == randNumber6) || (randNumber3 == randNumber7))
  return;
  
if ((randNumber4 == randNumber5) || (randNumber4 == randNumber6) || (randNumber4 == randNumber7))
  return;
  
if ((randNumber5 == randNumber6) || (randNumber5 == randNumber7))
  return;
  
if ((randNumber6 == randNumber7))
  return;
  
//------First Number---------//
  
  if (randNumber1 < 10)
  {
    lcd.setCursor(1,0);
    lcd.print("0");
    lcd.setCursor(2,0);
  }
  else
  lcd.setCursor(1,0);
  
  lcd.println(randNumber1);

//------Second Number---------//

  lcd.setCursor(3,0);
  lcd.print(".");
  
    if (randNumber2 < 10)
  {
    lcd.setCursor(4,0);
    lcd.print("0");
    lcd.setCursor(5,0);
  }
  else
  lcd.setCursor(4,0);
  
  lcd.println(randNumber2);
  
  
//--------Third Number-------//

  lcd.setCursor(6,0);
  lcd.print(".");
  
    if (randNumber3 < 10)
  {
    lcd.setCursor(7,0);
    lcd.print("0");
    lcd.setCursor(8,0);
  }
  else
  lcd.setCursor(7,0);
  
  lcd.println(randNumber3);
  
//-------Fourth Number-------//

  lcd.setCursor(9,0);
  lcd.print(".");
  
    if (randNumber4 < 10)
  {
    lcd.setCursor(10,0);
    lcd.print("0");
    lcd.setCursor(11,0);
  }
  else
  lcd.setCursor(10,0);
  
  lcd.println(randNumber4);
  
//------Fifth Number------//

  lcd.setCursor(12,0);
  lcd.print(".");
  
    if (randNumber5 < 10)
  {
    lcd.setCursor(13,0);
    lcd.print("0");
    lcd.setCursor(14,0);
  }
  else
  lcd.setCursor(13,0);
  
  lcd.println(randNumber5);
  
//------Clear 16th Char------//

  lcd.setCursor(15,0);
  lcd.print(" ");
  
//************Finished First Row******************//

  
 //***********Start Second Row****************//
 
 lcd.setCursor(0,2);
 lcd.print("Final Number: ");
 
 //-------Seventh Number--------//
 
     if (randNumber7 < 10)
  {
    lcd.setCursor(14,2);
    lcd.print("0");
    lcd.setCursor(15,2);
  }
  else
  lcd.setCursor(14,2);
  
  lcd.println(randNumber7);

//-------Button State Dependant Delay-------//

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == HIGH)
    {
    delay(50);
    }
  
  
  
  if (buttonState == HIGH) 
  {
    delay(1000000);
    digitalWrite(ledPin, HIGH);
  }
  else 
  {
    delay(50);
  }
  
}

JumpKit:
With my little understanding of arduino, I assumed the Delay() function was...

There ought to be a sticky thread here about the problems caused by "delay()"...

Best advice: Don't ever use it.

It works but I'm certain it can be heavily improved.

Think arrays, for one thing.

long randNumber1;
long randNumber2;
long randNumber3;
long randNumber4;
long randNumber5;
long randNumber6;
long randNumber7;

The odds of winning your lottery are infinitesimally small. I don't think I'll be buying any lottery tickets.

randNumber1 = random(1, 50);

On the other hand, it makes no sense to use a 4 byte variable to hold a one byte value. Waste. Waste. Waste.

Arrays and for loops are in your future, unless you like to type. Keep in mind that more code means more places for the bugs to hide.

The odds of winning your lottery are infinitesimally small. I don't think I'll be buying any lottery tickets.

I am only making a random number generator, not an actual lottery system (way too complicated for me) haha.

The only way I could find to generate each number randomly with my limited knowledge was to use randNumber's.

I will look into arrays after I have this second button working.

I will look into arrays after I have this second button working.

The operation of the switches has nothing to do with arrays. In fact, you need to dump all that code, and simply print the state of each switch each pass through loop. You need to confirm that the switches are wired correctly.

You are not using the internal pullup resistors. This means that you need an external resistor wired with the switch, which makes for much more complex wiring. How are the switches wired?

What do you want the second switch to do?

It seems to be at the moment, its waiting until the huge delay is over until it changes the delay back again, do you have any idea of another way I can achieve the same results, or to "override" the large delay.

That is exactly what is happening, which is why we've been telling you not to use it. You need to study the blink without delay example.

Maybe this will help: Its a random ON OFF generator

int ledPin = 13;                  // LED connected to (digital) pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time
int led = 13;                     //ignore
int brightness = 0;               //ignore
int fadeAmount = 10;              //ignore


void setup()                      // run once, when the sketch starts
{
  randomSeed (analogRead (0));    // randomize
  pinMode(ledPin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
  randOn = random (20, 600);    // generate ON time between x seconds (fiddle with these values to get the length of time ON)
  randOff = random (20, 600);    // generate OFF time between y seconds (fiddle with these values to get the length of time OFF)
    digitalWrite(ledPin, HIGH);   // sets the LED on
    delay(randOn);                // waits for a random time while ON
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(randOff);               // waits for a random time while OFF
      analogWrite(led, brightness);    //ignore

  
  brightness = brightness + fadeAmount;  //ignore

   
  if (brightness == 0 || brightness == 255) //ignore
    fadeAmount = -fadeAmount ;               //ignore
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

The operation of the switches has nothing to do with arrays. In fact, you need to dump all that code, and simply print the state of each switch each pass through loop. You need to confirm that the switches are wired correctly.

I'm fairly certain the switches are wired correctly, each is wired individually with a pullDown resistor.

Here's the fritz of the button switches.

I will look into the random on off generator, however it still uses the delay() function, which I have been told explicitly to avoid.

You can create a variation of the delay() function, using millis().

unsigned long waitStart = millis();
unsigned long waitTime = 1000000;
while(millis() - waitStart < waitTime && digitalRead(buttonPin2) == HIGH)
{
   // Don't do a thing
}

This while loop will complete after 1000 seconds OR when the switch connected to the pin is pressed.

It is easy enough to change the while condition, or to add an if statement before it. The if statement could start the while loop, which could end when the switch is pressed again, or when it is released.

If you define exactly what should cause the long wait to start, and what should cause the long wait to stop, we can help you get the code right.

If you define exactly what should cause the long wait to start, and what should cause the long wait to stop, we can help you get the code right.

This is exactly what I'm looking for.

Thanks for all the feedback, I have a lot to get working on. :slight_smile: