Go Down

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

PaulS

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

Think arrays, for one thing.

Code: [Select]
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.

Quote
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.

JumpKit

Quote
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.

PaulS

Quote
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?

Quote
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.

Zack_Johnston

#8
Nov 11, 2012, 05:49 pm Last Edit: Nov 12, 2012, 01:09 am by Coding Badly Reason: 1
Maybe this will help: Its a random ON OFF generator


Code: [Select]
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: [code] [/code] tags added.

JumpKit

Quote
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.

Go Up