Pages: [1]   Go Down
Author Topic: Will this basic code work?  (Read 654 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, my first ever attempt at writing code.... pretty much copied the led flash, but edited a little bit so I was wondering will it work?

/*
 *
 *
 * Using Arduino to control an analogue switch to turn on and off a keyboard hack at random intervals.
 *
 */
 
int aswitch = 12;          // Analogue switch connected to digital pin 12
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time


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

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
}


Thanks!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why don't you try it?

(when posting code, please use the # icon on the editor's toolbar)
Logged

"Pete, it's a fool 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.

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 361
Posts: 17255
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 193
The people the people they see me and they know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool 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.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley-grin
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 smiley-razz I'll use hash from now on!)
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 193
The people the people they see me and they know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 361
Posts: 17255
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley-grin

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 smiley-razz I'll use hash from now on!)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
And finally, why "byte"?
Why byte what? If it's a donut, isn't it obvious?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks everyone! I *think* I've everything clear in my head now smiley-grin Couldn't have done it without you!
Logged

Pages: [1]   Go Up
Jump to: