I thought since I spend all this time talking about the Arduino, I'd give a couple of examples of very simple sketches. A "sketch" is a program for the Arduino, and the name comes from the Processing programming language on which the Ardunio language is based.
I'll do a couple of posts on a really simple project, then a third post where we'll use the same circuit, but make it behave slightly differently. Of course, you can find these projects on lots and lots of other sites and tutorials, but I've always learned "see one, do one, teach one". I'm on the teach one phase of this really simple circuit.
You'll write the instructions on the computer using the Ardunio developer interface. Then, you compile the code you've written into instructions the board understands, and then upload the instructions from the computer to the board. The newer Ardunio's will receive the instructions and begin processing them immediately. Others require you to press a button on the board to receive the instructions, then the reset button to begin executing them.
The Processing language that the Arduino's language is based on is syntactically similar to C and Java, although not as complex as either. The basic layout of a sketch for the Ardunio looks like this:
void setup() {
// ...
}
void loop() {
// ...
}
The setup function gets called when the board is reset, and the loop function executes over and over and over, really, really fast like. Now let's change the sketch just a bit:
int inPin = 2;
int outPin = 13;
void setup() {
// ...
}
void loop() {
// ...
}
Here, we've defined two variables - inPin and outPin. These names have no special significance except that in reading them, it's evident to us what we'll use them for. The values on the other hand (2 for inPin and 13 for outPin) are significant. These are the numbers of the ports on the board that we'll use for input and output. Digital pins 0 through 13 can be used for digital input and output, which is all we'll be doing here.
Let's make another change here:
int inPin = 2;
int outPin = 13;
void setup() {
// ...
}
void loop() {
if (digitalRead(inPin) == HIGH) {
// ...
}
}
This looks just like an if statement in Java or C. We use the digitalRead function to read the value of the inPin - so we take a reading of pin 2. Then we compare (==) that value to the built-in constant HIGH to see if the pin is high - or there's current coming to it. If and only if the pin is high, will we execute the statements inside the block. So let's make that do something:
int inPin = 2;
int outPin = 13;
void setup() {
// ...
}
void loop() {
if (digitalRead(inPin) == HIGH) {
digitalWrite(outPin, HIGH);
}
}
So in this block, we'll set the digital pin 13 to high if pin 2 is high.
Now let's just flesh it out and make the converse come true:
int inPin = 2;
int outPin = 13;
void setup() {
// ...
}
void loop() {
if (digitalRead(inPin) == HIGH) {
digitalWrite(outPin, HIGH);
} else {
digitalWrite(outPin, LOW);
}
}
If you understood everything up to this point, this should be simple. If the if statement is not true, (pin 2 is not high), then we execute this block (turn pin 13 low);
Notice we didn't do anything with setup. You don't always need to, but it seems that you still must define it (I've been programming in Groovy for too long - everything is optional) even if it's empty.
So this is the complete sketch. Stay tuned for the next tutorial where I show you at least two ways to wire up the Arduino to make this turn into a blinky light.
0 comments:
Post a Comment