Commenting (review)
Basic Programming Structure
Pulse Width Modulation(pwm)
Where to find components/tools/cool stuff
Commenting (code):
Commenting code is an important part of making sense of what is going on in the code, especially when you come back to your program at a later date. Using comments, you can leave descriptions of the processes and variables used in your program. There are two kinds of commenting, line commenting(//...) and block comments(/*...*/).
Line commenting occurs each time you place two forward slashes in your code. The rest of the text on the line with the slashes is disregarded by the compiler. This is a great way to turn off one line of code but still have it available for a later time. Block comments use a combination of */ to begin the comment and */ to end the comment. Anything inside of those characters will be disregarded by the compiler. This allows you to turn off big chunks of code and create proper code descriptions.
/*
This is a block comment. Everything inside of the "/*" and "*/" is commented out.
*/
// this is also a comment
Variables (code):
Variables are a way of storing and accessing values in a program. There are many different types of variables, each type has a limit to how big of a number or even what type of number can be stored inside.
The standard variables available to us through Arduino are:
boolean: A boolean holds one of two values, true or false. (Each boolean variable occupies one byte of memory.)
char: A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").
byte: A byte stores an 8-bit unsigned number, from 0 to 255.
int: Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
word: A word stores a 16-bit unsigned number, from 0 to 65535. Same as an unsigned int.
long: Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.
float: Datatype for floating-point numbers, a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
double: Double precision floating point number. Occupies 4 bytes. Same thing as a float.
string: Strings are represented as arrays of type char.
array: An array is a collection of variables that are accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively straightforward.
Program Structure (code):
It is important to have a standard structure when writing a program. A structure will make it easy for you and others to locate all of the components of your program.
The first part of the program should be a description of what the program is using block comments.
The second section is where you place all of your variables. Start with constants first and then group them according to their purpose. Don't forget to leave detailed comments.
The third section is for the setup() function. Void setup() is where you initialize your digital pins and communication lines.
The fourth section is for the loop() function. Void loop() is what gets executed by the microcontroller on every loop or frame. This will get repeated over and over until the microcontroller is turned off.
The fifth section(not depicted) is used for functions you create.
PWM - introduction to Pulse Width Modulation:
Pulse Width Modulation is a process of taking a digital pin(on/off) and creating a gradient value or change. By pulsing the pin on and off in varying square waves you can achieve a range of values which can be used to "dim" an LED. I say "dim" because really the LED is being turned on and off so fast that our eyes perceive it as dim.
Fading using delay(lab):
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.*/
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
// declare pin 6 to be an output:
pinMode(6, OUTPUT);
}
void loop() {
// set the brightness of pin 6:
analogWrite(6, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Fading using timer(lab):
/*
Fade RGB LEDs
This example shows how to fade an LED on pin 3,5,6
using the analogWrite() function.
This example code is in the public domain.
*/
int brightnessBlue = 0; // how bright the BLUE LED is
int fadeAmountBlue = 5; // how many points to fade the BLUE LED by
int brightnessGreen = 85; // how bright the GREEN LED is
int fadeAmountGreen = 5; // how many points to fade the BLUE LED by
int brightnessRed = 170; // how bright the RED LED is
int fadeAmountRed = 5; // how many points to fade the BLUE LED by
void setup() {
// declare pin 9 to be an output:
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
// set the brightness of pin 3, 5, and 6:
analogWrite(3, brightnessBlue);
analogWrite(5, brightnessGreen);
analogWrite(6, brightnessRed);
// BLUE LED LOGIC BEGIN
// change the brightness for next time through the loop:
brightnessBlue = brightnessBlue + fadeAmountBlue;
// reverse the direction of the fading at the ends of the fade:
if (brightnessBlue == 0 || brightnessBlue == 255) {
fadeAmountBlue = -fadeAmountBlue ;
}
// BLUE LED LOGIC END
// GREEN LED LOGIC BEGIN
// change the brightness for next time through the loop:
brightnessGreen = brightnessGreen + fadeAmountGreen;
// reverse the direction of the fading at the ends of the fade:
if (brightnessGreen == 0 || brightnessGreen == 255) {
fadeAmountGreen = -fadeAmountGreen ;
}
// GREEN LED LOGIC END
// RED LED LOGIC BEGIN
// change the brightness for next time through the loop:
brightnessRed = brightnessRed + fadeAmountRed;
// reverse the direction of the fading at the ends of the fade:
if (brightnessRed == 0 || brightnessRed == 255) {
fadeAmountRed = -fadeAmountRed ;
}
// RED LED LOGIC END
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Where to find components/tools/cool stuff:
At some point you are going to need to add to your project. If you need it local your only options are a RadioShack or a small electronics seller in China Town. The address is 269 Canal Street and the store is located in the back. If you don't need it today, then you have a much wider option for price and availability online. I have included some of my most frequented online stores along with what they stock.
Local:
RadioShack - common components, wires and tools
269 Canal Electronics - rarer components, call and ask, not cheap, in China Town
Electronics:
SparkFun Electronics - Hacker/Hobbyist Electronics and Arduino
Adafruit - Hacker/Hobbyist Electronics and Arduino
Evil Mad Science - Arduino and cheap LEDs
MaceTech - LED control for Arduino
Electronics Goldmine - Close out prices on electronic parts and tools, mostly older
All Electronics - Close out prices on electronic parts and tools, mostly new


0 comments:
Post a Comment