Go Down

Topic: How Can I Force A Loop Restart (Read 702 times) previous topic - next topic

JumpKit

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:

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


Arrch

#1
Nov 11, 2012, 04:10 pm Last Edit: Nov 11, 2012, 04:16 pm by Arrch Reason: 1
Get rid of the delay() calls and use the concepts demonstrated in the Blink Without Delay example.

PaulS

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.

JumpKit

#3
Nov 11, 2012, 04:16 pm Last Edit: Nov 11, 2012, 04:19 pm by JumpKit Reason: 1
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.
Code: [Select]

#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);
 }
 
}

fungus


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.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Go Up