void loop() // run over and over again{ randOn = random (150, 350); // generate ON time between 0.15 and 0.35 seconds randOff = random (200, 2000); // generate OFF time between 0.2 and 2.0 seconds digitalWrite(aswitch, HIGH); // sets the switch on delay(randOn); // waits for a random time while ON digitalWrite(aswitch, LOW); // sets the switch off delay(randOff); // waits for a random time while OFF}

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.Do not send technical questions via personal messaging - they will be ignored.I speak for myself, not Arduino.

You know that you can type in sketches to the arduino IDE software and hit the verify button even without having a board plugged into the PC. While a good verify doesn't mean the sketch will do what you want it to do, or that there aren't logical errors, it does tell you that the compiler didn't find any syntax errors or missing references.

Lefty

Kctess5

Looks like it will work but your using the long datatype for a number with a max of 2000. Thats overkill, just use int you'll save a little space. This wont matter for your purpose because its a small program but you don't wont to get into the habit. If you wanted to get fancy you could even use byte for aswitch because its just 12

but your using the long datatype for a number with a max of 2000. Thats overkill

It is also the correct datatype for the function's returned value.Using the correct datatype is good habit to get into.

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.Do not send technical questions via personal messaging - they will be ignored.I speak for myself, not Arduino.

acm01

Thanks for the feedback! I tried running the verify option and I got back "Binary sketch size: 1830 bytes (of a 32256 byte maximum)" I've no idea what that means and that's why I came here! So I've gotten a bit mixed up, should I be writing 2000 or int? There needs to be a 2 second limit so I thought you needed to put that top value in? And does it matter what pin I use, I just said 12 because it was originally 13, but I changed it because there's an LED there. Is any other pin more appropriate? And finally, why "byte"?

Thanks, and sorry about the list of questions, just beginning to get to grips with this programming lingo!

(oh and sorry about putting the code in wrong I'll use hash from now on!)

Kctess5

No the number itself doesn't really matter as long as it is below the max value for that datatype, your 2 second max is fine. The integer datatype uses less space than long (half as much) but it accomplishes the same thing. You really only have to use long for really big numbers like if you were averaging a bunch of numbers or doing a counter. Byte is another datatype even smaller than integer, look at the reference section

Thanks for the feedback! I tried running the verify option and I got back "Binary sketch size: 1830 bytes (of a 32256 byte maximum)" I've no idea what that means and that's why I came here!

That means it compiled successfully without errors, congratulations. Again it doesn't mean there might not be a flaw in the sketch, only running it and observing the output results will tell you that.

So I've gotten a bit mixed up, should I be writing 2000 or int? There needs to be a 2 second limit so I thought you needed to put that top value in?

You did it correctly. Random returns a long variable result and you defined you two variables as being long type.

And does it matter what pin I use, I just said 12 because it was originally 13, but I changed it because there's an LED there. Is any other pin more appropriate?

You selected a good pin to use. There are three pins that the arduino has something wired to, pins 0 and 1 for USB serial data communications, and pin 13 has a resistor led circuit on board. That doesn't mean you can't use those pin for other purposes, but if you can avoid using them it makes life simpler as you learn the arduino.

And finally, why "byte"?

The theory is if you are using a variable that will only hold values between 0 and 255 then defining it as a byte type will use the least amount of memory required. An int type is a 16 bit variable type so takes two bytes of memory. The processor chips used in arduino boards have very limited space for variables, arrays, and stack space that are stored in SRAM memory. This becomes a practical issue if you are writing sketches where you are trying to waste as little memory as possible. However some will say that good programming practice is to always scale variables only to the size they require.

Thanks, and sorry about the list of questions, just beginning to get to grips with this programming lingo!

Don't be sorry, questions are what this forum is all about.

(oh and sorry about putting the code in wrong I'll use hash from now on!)

I tried running the verify option and I got back "Binary sketch size: 1830 bytes (of a 32256 byte maximum)" I've no idea what that means and that's why I came here!

It means that your sketch successfully compiled and linked. It will take up 1,830 of the 32,256 bytes available in program memory.

Quote

So I've gotten a bit mixed up, should I be writing 2000 or int?

This isn't even an apples/oranges comparison. It's more like an apples vs. the Seahawks chances of winning the Superbowl. One is a variable type; the other is an upper limit on a value.

Perhaps you could clarify the question.

Quote

There needs to be a 2 second limit so I thought you needed to put that top value in?

Is this a statement or a question? The 2 second limit means that 2000 was a valid upper limit for the value to be returned by the random function.

Quote

And does it matter what pin I use, I just said 12 because it was originally 13, but I changed it because there's an LED there. Is any other pin more appropriate?

Some pins have specific functions. Pins 0 and 1 are the serial port. Pins 2 and 3 are where external interrupts can be attached. The special purpose pins are generally not used unless there is no choice. If you don't need external interrupts, any pin from 2 to 13 can be used. No one is better than any other.