Programs in Arduino are know as “sketches”, to create a new sketch go to File –> New. You should now have a blank sketch that looks like the picture on the left. The first part of a sketch is setup. The setup code runs only once at the start, this is where you set up variables, ports, sensors, … The second part of a sketch is loop, the loop code runs continuously.
On the left you can see an example of a simple sketch, let’s look at all the code … Let’s start with the setup, in setup we are telling Arduino that we are going to use pin 10 as an output, that means that we can tell the Arduino to send information to pin 10. Whenever you want to use a port you have to tell Arduino which ports you want to use and if they will be inputs or outputs.
The first command in loop tells Arduino to turn on port 10 (digitalWrite), what this means is that the port will go from 0V to 5V (HIGH) and if you have an LED connected to this port it will light up. The next command (delay) tells the Arduino to do nothing for 1000 milliseconds or 1 second. Then we turn port 10 off, it will go back to 0V (LOW) and the LED will turn off as well. Lastly we have another wait command. After the wait command the program will go back to the start of loop and the program will start again. The result is an LED that will blink with a 1 second interval.
Things to try:
- Change the delays and see what happens
- Connect an LED to port 7 and change the code accordingly
- Connect 2 LEDs to 2 different ports and make both blink at the same time
- Create different blinking patterns for both LEDs