To practice programming the Arduino Virtualy you will need a copy of the Virtual Tutorial Board. Follow the link and select "Copy & Tinker".
When you open it you will get a window like the one below, which is a blank program. Every program you write MUST have the setup and loop functions. Functions are collections of code that run SEQUENTIALLY.
All code in the setup will run only once.
All code in the loop will run repeatedly (forever) in a loop.
As you move through the course and the programs build on each other, it is CRITICAL you don't lose or overwrite programs you have finished with ones that you are working on. Copy each program into the document "Lesson #1 programs" on the Google Classroom.
The LED is connected to one of the microcontroller pins, but these can control either inputs or outputs. Saying which one it will be is done with the pinMode command. Syntax matters in ArduinoC, so you need to capitalise the M and not the p in pinMode.
Type pinMode into your blank, but correctly saved Ex1_1_blink program. Indenting each line of code is critical for organisation and troubleshooting. Since the pinMode command is inside the setup function, it should be indented in one tab or space. You will later see how your program becomes difficult to de-bug if you don't do this.
The Green LED 3 is connected to pin 3 by green wires and it is an output, so fill that in the setup function, as shown below. Again, syntax is critical and you need to capitalise OUTPUT.
The semicolon (;) is also critical since it marks the end of the command. This is the number one most common error when trying to figure out why your code doesn't run.
To enjoy the fruits of your labour, you want to make the LED flash on and off repeatedly, so you want to put the code for this in the loop procedure. digitalWrite is the command for sending a HIGH or LOW voltage to the pin of your choosing. You can guess what each one does to the LED… Remembering that the LED we want to work with is connected to pin 3, the code will look something like:
Now you can run the program by pressing "Start Simulation".
Why is the LED just on all the time and not turning on and off?
Please put answers to your questions in your Google doc "Lesson #1 programs".
The processor executes commands very quickly (16 million per second), so you have no chance of seeing the transition from ON to OFF. You need to have the program pause between commands so you can perceive the difference. We do this with a delay command, with the parameter inside the command being the number of miliseconds the program should pause. Let's try 1 second.
Why a delay after each of the digitalWrite commands, and not just after the first one?
Copy your first program into your Google Doc "Lesson #1 programs" under Ex1_1_blink.
Your second program will be called Ex1_2_ProgressBar and you can use the just completed Blink program as a starting point, but you must copy it separately into your Google Doc BEFORE you actually start work, remember?
This program should make LEDs 1-8 turn ON sequentially until they are all on, after which they should all turn OFF. You now have what looks like a loading/progress bar. (If your LEDs look a bit dim you might have forgotten to change the pins' Mode in the setup!)
HINT: copy (Ctrl+C) and paste (Ctrl+V) are VERY useful tools here.
Make sure the LEDs are making the progress bar effect before copying your program into "Lesson #1 programs" under Ex1_2_ProgressBar.
Save the previous program under Ex1_3_Cylon before starting work.
Same idea as the progress bar, except the LEDs should turn ON and OFF sequentially, sweeping from 1 to 8, then back from 8 to 1. Should look something like this.
Visualise in what order events (LED turning on, LED turning off, pause) happen and then translate that into code. Don't worry about getting it wrong, try it and make changes!
Again, copy and paste is the way to go.
Copy your program into "Lesson #1 programs" under Ex1_3_Cylon.
Write a program (Ex1_4_odds_evens) where the odd numbered LEDs are all on for 1 second, then all off, then the even numbered ones are on for 1 second, then all off.
Write a program (Ex1_5_building) where LED 1 is on for 1 second, then off, then LEDs 1 and 2 are on for 1 second, then both off, then LEDs 1, 2 and 3 are on for 1 second, then off, … , then LEDs 1-8 are on for 1 second, then all off.
Now move on to the next lesson: Modulation.