RobotKitMenu-2-C

From ArduinoInfo
Jump to: navigation, search
print this page
BACK to MENU

MICROCOMPUTER SOFTWARE:

Editing and trouble-shooting Software Sketches


SOFTWARE TIME & WRITING ARDUINO SKETCHES

You have the Arduino IDE software installed and you have run and edited BLINK, right? You're getting there!
Now let's get used to writing Arduino Software, and then we'll come back and start connecting your Robot kit parts and making more interesting things. We will give you software examples to test each part of the Robot kit.

I know all this and I want to just Build The Robot! [/RobotKitMenu-3 (SKIP AHEAD)]</span>

On your desktop you should now have the Arduino ICON like this:</span>
external image Arduino-Icon1-68.jpg Click on it, if you haven't already, and you should see the "Arduino IDE Window" pop up like this:</span>
IDE-topE-900.jpg

You'll use this IDE (Integrated Development Environment) to make Software VISIBLE! With it you will develop your own software to make Arduino into what you want it to be.

FIRST, YOU MUST SET THE CORRECT BOARD TYPE AND SERIAL PORT:

In the Arduino IDE top menu click <Tools> and then <Board>. You will see a drop-down menu that looks like this (left).

NOTE: For the YourDuino RoboRED in all current kits, pick "Arduino UNO". For the older YourDuinoRobo1 click Arduino Duemilanove. (Same for PC and MAC)
SERIAL PORT: Now click <Tools> and then <Serial Port>. On the PC There will usually be only one choice. But you may have COM1 and then some higher number which is Arduino.
PC-Board-Select-450.jpgPC-Select-SerialPort-450.jpg









MAC-SelectSerialPort-450.jpg
On the MAC, see the example on the right. Pick the entry that has both "tty" and "usbserial" in it.

OK, you're ready to upload software sketches to your YourDuino or Arduino!


SKETCHES:

The visible text of an Arduino software program is
called a SKETCH. But the Sketch becomes alive when you upload it to your Arduino. Let's walk through
LCSS-IDE-Blink.jpg
Editing a Sketch and Verifying it and Uploading it:


Click on FILE and then move your mouse (slowly.. It's Fussy!) over EXAMPLES to BASICS and then to BLINK so it looks like this (On the right):</span>
And CLICK. There are lots of examples that come with the free Arduino software system, and we will look at some of them later, as well as make our own very soon.
A new IDE window should pop up and look like this:</span>

Wow.. A bunch of new stuff.
LCSS-IDE-BlinkCode.jpg
Let's slow down and "Watch Closely Now"!</span>

Notice that a lot of the text is GRAY. All of that is just "Comments" for you to read to help understand the Sketch. When you write software, you should also use "Comments" to explain to others what you are doing. (Arduino itself totally ignores these comments).

Now, let's go through Editing software, Verifying it, and Uploading it to see Arduino DO it. We will use the YourDuino version of Blink: YourDuinoStarter_Blink . Click on the link to see it's page. It will open in a separate window, so you can keep going on this page also.

Now, highlight all the code (in the gray area) , do <Ctrl>C to Copy.
In the IDE, click the NewButton.jpgNew button to get a blank page and do <ctrl>V to Paste it in.

NOTE: Newer versions of the IDE may fill in the New page with an example sketch. Delete that before you paste your new sketch in.''.
Now, click VerifyButton.jpgVerify to make sure it's OK.


NOTE Again: Newer versions of the IDE may pop you to a "Save" option. Just type in a name like "myblink" and click Save.
You should see "Compiling Sketch" and then "Compiling Done". Now look at the sketch in detail. Take some time to read it slowly.

STRUCTURE OF ARDUINO SOFTWARE:

Every Arduino Software Sketch has two main parts:
  • SETUP - Runs Once at the beginning
  • LOOP - Runs over and over again, forever


You will read a LOT of program "Code" that other people wrote, in the future. You HAVE to "Watch Closely Now" and really see the details. Read the YourDuinoStarter_Blink example through carefully, a couple of times. Note the colored special words that are </span> Instructions</span> . These are unique words that tell Arduino what to do. They have to be spelled perfectly!</span>

What SETUP does: Tells Arduino about things that need to be done once. Arduino Digital Pins can be either INPUT or OUTPUT. You have to tell Arduino when a Pin will be used as an OUTPUT. In this example, there is one line that tells Arduino that Pin 13 must be an OUTPUT.
pinMode.jpg

Note the COLOR of the lettering. The Arduino IDE changes the color of words as it recognizes them as special instructions. Let's mess with them:

pinMode: Note that when Instructions are two words run together, like pinMode, the beginning of the SECOND word is Capitalized. Change the Capital "M" to "m". Note the color changes to black. Hmmm. Click the VERIFY VerifyButton.jpg button.

You will get an ERROR message: external image ArduinoIDE-pinmode-error.jpg

Fussy, Fussy, Fussy! Yep, every letter has to be correct and also correct upper or lower case.

Change it back. Check the color. Click Verify again. OK??

What LOOP does: Loop contains all the active parts of your Sketch that continue to run after SETUP is finished. It will run over and over again, forever, or until you stop it or power down.

What does VERIFY do??VerifyButton.jpg

A LOT! More details later, but Verify is a program in your main computer that goes through every Instruction in your Sketch (Ignoring "Comments") and checks it against the list of valid Instructions, and checks that the structure and sequence of the statements in the Sketch are correct. Fussy, Fussy! If they're OK, then it "compiles" or "translates" the sketch into the machine code that Arduino actually runs on. It saves that 'ready-to-run' code for you to Upload to Arduino and run. Other systems would call this process "Make" or "Compile". Arduino refers to it as "Build" and if you want the gory details (Click Here).
What does UPLOAD do??UploadButton.jpg

First, Upload runs Verify to check and compile your program. Then it communicates to your Arduino over the USB connection, resets the Arduino chip, and talks to software already on Arduino (called the BOOTLOADER(W)) to load your new program into the Arduino's memory. Then it restarts Arduino and your program runs it's SETUP section and then repeats the LOOP section.
[NOTE: The (W) Means this is a Wikipedia link.]

Start Making Changes:


Ok, let's make a few changes to the YourDuinoStarter_Blink program:</span>

external image Code-Blink1.jpg

The LOOP section of your program does all the instructions in the section, and then "loops" back to the top and starts it again, over and over.

NOTE: the "Brackets" { and }

Notice that the beginning and end of the section is "inside brackets". You will see many sections of bigger programs that are grouped by these "brackets".

Now, let's look in detail at the instructions:

Instruction: digitalWrite


This instruction sets an OUTPUT PIN to either HIGH (connects it to +5 V) or LOW (Connects it to GND). Lots of new words will be thrown about here!

Thing To Remember: HIGH = 1 = ON = 5 Volts and LOW = 0 = OFF = 0.0 Volts

So, the first line in LOOP sets PIN 13 to HIGH. This means Pin 13 is connected to +5 Volts, and current flows through a resistor and LED that are already connected to pin 13 on the Arduino board. The LED lights up.

Instruction: delay


The delay instruction just waits for a period of time. The VALUE used with delay is in Milliseconds (1/1000 second). So delay(1000); waits for 1000/1000 seconds (1 second). We'll change that soon.

NOTE: the ";" (Semi-Colon)

Notice that every instruction is followed by the character " ; " which is like a period that tells the end of a sentence. Run-on sentences will make you stay after school to fix your error messages!

Change the delay so that the LED blinks differently:

Time to mess about and try some things! Maybe we'll break it. Then we'll fix it..

Suggestion: Save your own version of BLINK so you can always go back to the original one. Go to File and Save As and call it something like MyBlink. This will go in your SKETCHBOOK where you'll save your own programs.

Now go change the VALUE in a delay statement to change the way the LED blinks. Think about the 4 instructions in LOOP. What's happening??

  • Turn the LED on. Wait and look at the LED.
  • Turn the LED off. Wait and look at the dark.

So, let's change the ON time to be short. Maybe 50 Milliseconds. That's 1/20 of a second. Change the first delay instruction to be delay(50);

Then try 10 milliseconds. The LED is only on 1/100 of the time. Can you still see it blink? How about 1 millisecond?

Each time you make a change, click UploadUploadButton.jpg which will first Verify and Compile your program and then send it to Arduino. Notice that each time you do this the LEDS on the Arduino board that are marked "Tx" (Transmit) and "Rx" (receive) flash as your main computer communicates with your Arduino.

Try out some combinations of ON and OFF delay() times. Like ON 1000 and OFF 50.

Try making both ON and OFF times shorter and shorter. If you make the ON and OFF times short enough your eye will no longer see blinking, because of "Persistence of Vision"(W) which happens when there are more than about 25 blinks per second. So, hmmm.... if you make the LED be ON for 1/50 of a second and OFF for 1/50 of a second that should do it. So try 1000/50= 20 Milliseconds. Put 20 for both the ON and OFF times. What do you see?? How about 10 Milliseconds each? Depending on your personal eye's physiology, at some speed you will not see any more blinks. Young people can usually see faster blinks than old codgers like me :-)

All right. You are hacking code. You're the Programmer! You can save any of the sketches for use later on. Go to File>Sketchbook and you'll see them.

Next, we'll start hooking up the Parts in your Robot Kit! And we'll give you example Software Sketches for each one of them.
Go To the Next Section: [/RobotKitMenu-3 Physical Computing Concepts]
OR back to the Main Menu:


Please email comments, critiques, suggestions and questions to:</span>
terry@yourduino.com