brg

We can finally do something!  This tutorial will cover basic coding and how to hook up the LED to the board.  We will talk about syntax (Arduino needs to recognize a specific set of instructions) and different coding techniques. We wrote our own code, but there are also example code snippets in the Arduino software.

We highly encourage you to type out what you see on the screen.  This helps you remember what everything is!  Later on, this will help you debug and problem solve.  

The early tutorials will be heavy on definitions and explanations.  Bear with us!  If you have questions, we can answer them directly on the forum!

  
Coding and Syntax Notes:

When writing your code you want to make sure that you comment everything and create variable names that are meaningful.  Arduino is based on the C programming language. There is great documentation on the Arduino site on all of the commands that can be used. Check it out @https://www.arduino.cc/en/Reference/HomePage

Here is a screenshot of the program (attached as well).


2016-11-08_20-57-59.png 
Glossary of Terms

Comment: this is additional text that does not interfere with the main program.  You can see what the comments are, but the micro-controller will skip over them.  

How to implement Comments:  If you want a comment after a command type '//'.  If you want a comment block (shown at the top) start the comment block with /* and then finish with */  this closes the comment block.  Pretend they are like parentheses. 


Line Numbers: These are the rows that you see.  For example, the first code block spans lines 1 - 6.  Later if there are any problems with our code, we can refer back to which line is giving us an error.  

Variables:  This is literally anything you want it to be.  I could name something robotic_banana.  However, you cannot have any spaces between a single variable name. Example:  Robotic Banana <-WILL NOT WORK.  Robotic_Banana <-WILL WORK.  

Integers: Arduino is only as smart as its user.  Integers are whole numbers (counting numbers).  If we want to define a variable as an integer we must use the 'int' denotation. Example:  int Green_LED = 7;  <-found in line 8.  This assigned the variable 'Green_LED' the integer value of 7.  This is important to remember when we hook up the LED!


Code Syntax
void setup(){.....}:  What? void what?  This is where we have to set each Arduino pin a specific task.  Line 12 gives the syntax for assigning the Green_LED pin as an OUTPUT pin. The orange text are built in commands in Arduino.  If we had 5 LED's that we wanted to operate individually, we would have to write 5 pinMode's.  <-We'll do a RGB example of this later.

void loop() {.....}:  This literally goes on forever.  Every command we place in the void loop will be repeated from top to bottom continuously.  

pinMode(Pin_Number, Function):  This is a built in command to assign a pin a certain function.  The comma separates the Pin_number (can be a variable) and the desired function (input, output, etc).  

digitalWrite(Pin_Number, Value): This is a built in command that assigns a pin a certain value.  If it is a digital pin, it will only be 'on' or 'off'.  Synonyms for On: '1', 'HIGH'. Synonyms for Off: '0', 'LOW'.

delay(time):  Delay is the same thing as a pause.  If I send a command, I need to tell it for how long.  If delay comes after turning the LED 'ON' then it will hold the LED 'ON' for the given time delay.  

BRG...What does this program actually do?
I thought you said we were going to do stuff?  What gives?  Before the all out coup, let's take a deep breath and continue.  We've explained different portions of the code but what does it look like when we put them all together?  

Code Reading:
Line 8: 
This is where we assign the integer variable 'Green_LED' to pin number '7'; Therefore every time we see Green_LED we can think '7'.  Green_LED = 7, one more time.  Green_LED is number 7.  (*repetition is key).

Lines 10 - 14: This is the void setup.  We need to setup what we want our Green_LED (Pin 7) to do.  We are going to set Pin 7 (Green_LED) to 'OUTPUT'.  This means the digital pin will give an output signal!  

Lines 19 - 22: This is the void loop.  Everything inside this loop will run continuously. 'digitalWrite(Green_LED, HIGH);' turns the Green_LED on.  For how long though?  The next line 'delay(1000)' allows the Green_LED to be turned on for 1000 milliseconds (1 second). The next command 'digitalWrite(Green_LED, LOW);' turns the Green_LED off.  For how long? For 1000 milliseconds (1 second).  This will create a constant blinking every other second.  1 second turns on, 1 second turns off!

Compile this code:
Go to Sketch ->Upload OR Ctrl+U at the same time to upload the code that you wrote.  If all goes well then we are good to go!  You should see your LED blink on and off.  

BRG...You told me how to code, but how do I hook this up?
Always asking the right questions.  It's quite amazing.  We are going to hook this up with the mini breadboard provided in the Hobby Hand Kit.  Your mini breadboard will look different then the one shown (The one you will get is going to be translucent).  Apologies for the pictures, we may update at a later date.


This is the breadboard...in all of its glory.  Depending on orientation, rows can be columns and columns can be rows (we are actually rotated the wrong direction).  What is important to know about this is that the columns are connected in groups of 5 pins (columns in this orientation).  


20161108_201153.jpgTaking off the double sided tape reveals the true nature of this breadboard.  
20161108_201232.jpgThe metal strips connect each column (or 5 pins).  There is a break in the middle.
 


Green LED and RGB LED.  Notice how there is one longer leg on the Green LED.  This is the (+) side of the LED.  The shorter leg goes to Ground (GND).

20161108_201305.jpg 

This is the Arduino Mega WITHOUT the sensor shield attached.  We can directly connect pins into the board.  From our previous code, we picked pin 7 as our green LED (+). We use hook up wire to connect Pin 7 from the Arduino board to the Row the LED (+) leg is on.  We use another hook up wire (black) to attach the ground leg to the Arduino board ground (The picture makes it look like its on AREF, but it isn't).  

20161108_201623.jpg 
20161108_201635.jpg 

Compile this code:
Go to Sketch ->Upload OR Ctrl+U at the same time to upload the code that you wrote.  If all goes well then we are good to go!  You should see your LED blink on and off.  If you didn't do this from the previous step, do it now!

20161108_202614.jpg 
After the sketch successfully uploads, you should see an infinite blinking light!  Try messing with the delays to make the LED blink asynchronously.  

Question to Ponder:
If I change the delay to a very small time increment, what happens?  Why does this happen?



Quote 0 0
UA-63837278-7