The Japanino, The Blink Sketch
Going Further
Although the P.O.V. is a simple toy, it is possible to try different things with it without having to spend money on other shields.
What is the P.O.V.?
POV comes from "Persistence of Vision". It's an existing add-on for the Arduino, and it works based on the human eye's tendency to retain an afterimage of a light source for a short time. Basically, the POV is just a light stick containing 7 red LEDs, a small piezoelectric speaker and a microswitch. The
P.O.V. sketch
from the Gakken site uses the switch to tell when the LED stick has been cranked all the way to the right, in order to trigger the start of the LED animation that the eye interprets as "I (heart) U". The rest of the P.O.V. is just cheap moving plastic parts for making the LED stick swing around.
The LEDs are connected to pins D7 to D13. The switch is on D6 and the speaker is connected separately to A0 and Gnd.Go back to the IDE Up Arrow icon (open sketch from archive) and select
Display->Blink
.
In the workspace window, look at the program. If you've never used C++ before, it may look confusing.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
The circuit:
* LED connected from digital pin 13 to ground.
* Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.
Created 1 June 2005
By David Cuartielles
http://arduino.cc/en/Tutorial/Blink
based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
========================
First the "/*" characters mark the start of a long block of comments. The comments are ended with the "*/" characters. Anything within "/*" and "*/" will be ignored by the IDE.
"//" also marks a comment, but this is just until the end of the line. It's called a "line comment". Everything after "//" to the end of the line will be ignored.
In C++, we have to tell the IDE what variables we want to use, and what "type" they are. Some of the types are:
int - integer
char - character
boolean - boolean (1 or 0, HIGH or LOW, TRUE or FALSE)
In the program, "
int ledPin = 13
" creates an integer variable called
ledPin
, and assigns the value of 13 to it (since the onboard LED is hardwired to pin D13).
Next, we have "
void setup() { }
" and "
void loop() {
}
". These are called "functions". You can create a function by specifying the type of data to be returned by the function (
void
means "no return value"), the name of the function and the parentheses "()" (if you are passing data to the function you can create variable names inside the parentheses). And then we have the curly brackets "{" and "}". These brackets mark the beginning and the end of the function. We'll look more at functions later on.
There are two standard functions used by the IDE.
void setup()
-- setup is run once at the beginning of the program to initialize variables and pins.
void loop()
-- loop is the main body of the program, and runs in an infinite loop after setup is finished.
Note: If you're familiar with C++ then you may be wondering what happened to the
main()
function. The answer is that the IDE is plugging main() into the program for us during the compile phase and we don't need to specifically include it within the sketch ourselves.
//-------------//
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
The
pinMode()
command tells the Japanino to set the digital and analog pins to input or output modes. By default, the pins are
INPUT
at power on. The first parameter is a number that indicates the pin we want to set (
ledPin
was set to
13
). The second parameter is a pre-defined constant that the IDE understands. This can be either
INPUT
or
OUTPUT
.
pinMode(13, 1);
would have the exact same effect as
pinMode(ledPin, OUTPUT);
So, here we're just telling the Japanino on power on to make pin D13 an output pin.
//-------------//
In order to make the LED blink, we need to send a "1" to pin D13, followed by a "0". This is the same as telling the Japanino to put 5V on D13, and then 0V. There are two commands for digital pins -
digitalRead()
and
digitalWrite()
. For
digitalWrite()
, the first parameter is the pin number, and the second is the value to write to the pin.
HIGH
and
LOW
are constants. We could also just use "0" and "1".
The
delay()
command tells the Japanino to wait a certain number of milliseconds (thousandths of a second). In the below code, the program will wait 1 second for each delay() command.
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
So, start by writing a 1 (5V) to pin D13. Wait 1 second. Write a 0 (0V). Wait 1 second. Repeat until the Japanino is turned off.
This sketch is very simple. Look at it with the comments removed.
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
We can make it simpler by programming D13 directly, and not using a variable. The below code is still exactly the same as the above Blink sketch, just not as easy to understand when you read it.
void setup() {
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, 1);
delay(1000);
digitalWrite(13, 0);
delay(1000);
}
If you want to write your own sketches, you may want to get a beginner's book on C++ programming. But, you can also use the example archive programs available from the IDE (Up Arrow icon - Open Sketch from Archive) to try to understand how they work on your own. Additionally, you can go to the
Arduino reference pages
and look at the definitions of the various commands.
Return to Index
Learning About Operators