Saturday, January 15, 2011

Here is the agenda for class 2:
1. Soldering (lab)
2. Comment (code) - review from last week
3. Variables (code) - add to what we learned last week
4. Program structure (code) - add to what we learned last week
5. PWM - introduction to Pulse Width Modulation
6. Fading using timer (lab)
7. Where to find components/tools/cool stuff

Soldering:
Soldering is simple in concept but takes some practice to become proficient. Before you begin soldering you will first need the necessary tools: soldering iron, solder and a copper scrubber. Some additional tools to mention are: solder flux, helping hands, and a solder vacuum. Out of all of the tools, having great solder and a nice clean tip on your iron will be the biggest help.
Soldering breaks down to a few simple steps. First, apply a beat of solder to the tip of a hot iron. This is called tinning the tip and will allow for easier transfer of heat between the tip and the metals to be soldered together. Next, apply the tip to the contacts(metal part of the component/wire to be soldered) and allow the metals to heat for a second or three. Now place the solder onto the contacts and wait for the solder to flow onto the metal. If done properly the solder will hug close to the contacts, you will know when it happens and it is spectacular. It took me a few hours of soldering to really get the hang of the it.

By far the most difficult part of soldering is juggling all of the components and tools. Two ways to help with this problem are to use a breadboard to hold the components or use helping hands. Using the breadboard is perfect for soldering breakout boards, boards made to have a component interface with a breadboard, but not as good as helping hands at most other soldering applications. One trick my friend Paul Rothman taught me was to use the helping hands to hold the solder instead of holding the components. This works if you can easily hold both components with one hand and the iron with the other.

There are tons of tutorials online that show you detailed examples of all types of soldering. I have embedded a great beginner video for reference.

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.

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}

Variables (code):
Variables are a way of storing and accessing values in programming. 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. So far we have only used integers(int) in our code. An integer is a whole number with a range from -32,768 to 32,767. Today we will learn about floats, a number with a floating point or decimal. Floats are great for fine detail in rate of change as they allow us to have access to the numbers between whole numbers.

As we progress in class we will learn and use more variable types. If you want to learn more ahead of time you can check out the variables column on the Arduino website.

Program Structure (code):
/*
Smoothing

Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.
The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis

http://www.arduino.cc/en/Tutorial/Smoothing
This example code is in the public domain.
*/


// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading <>
readings[thisReading] = 0;
}

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
Serial.println(average, DEC);
}



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 timer (lab):


/*
PWM LED
dim an led using analogWrite

code by Matt Richard

*/

int pinRed = 11;
int pwmValueRed = 0;
int pwmIncrementRed = 1;

int pinGreen = 3;
int pwmValueGreen = 0;
int pwmIncrementGreen = 2;

int pinBlue = 6;
int pwmValueBlue = 0;
int pwmIncrementBlue = 3;

int pwmMin = 0;// the lowest pwm value - 0 = off
int pwmMax = 255;// the highest pwm value - 255 = full on

void setup(){
Serial.begin(9600);
}

void loop(){
red();
green();
blue();
delay(20);
}

void red(){
pwmValueRed = pwmValueRed + pwmIncrementRed;
Serial.println(pwmValueRed);
// pwmValue += pwmIncrement;
// this takes care of the pwmValue being smaller than pwmMin
if(pwmValueRed <= pwmMin){
Serial.println("we reached red's bottom!");
pwmIncrementRed = pwmIncrementRed * -1;
pwmValueRed = pwmValueRed + pwmIncrementRed;
}
// this takes care of the pwmValue being larger than pwmMax
if(pwmValueRed >= pwmMax){
Serial.println("we reached red's top!");
pwmIncrementRed = pwmIncrementRed * -1;
pwmValueRed = pwmValueRed + pwmIncrementRed;
}
analogWrite(pinRed, pwmValueRed);
}

void green(){
pwmValueGreen = pwmValueGreen + pwmIncrementGreen;
Serial.println(pwmValueGreen);
// pwmValue += pwmIncrement;
// this takes care of the pwmValue being smaller than pwmMin
if(pwmValueGreen <= pwmMin){
Serial.println("we reached Green's bottom!");
pwmIncrementGreen = pwmIncrementGreen * -1;
pwmValueGreen = pwmValueGreen + pwmIncrementGreen;
}
// this takes care of the pwmValue being larger than pwmMax
if(pwmValueGreen >= pwmMax){
Serial.println("we reached Green's top!");
pwmIncrementGreen = pwmIncrementGreen * -1;
pwmValueGreen = pwmValueGreen + pwmIncrementGreen;
}
analogWrite(pinGreen, pwmValueGreen);
}

void blue(){
pwmValueBlue = pwmValueBlue + pwmIncrementBlue;
Serial.println(pwmValueBlue);
// pwmValue += pwmIncrement;
// this takes care of the pwmValue being smaller than pwmMin
if(pwmValueBlue <= pwmMin){
Serial.println("we reached Blue's bottom!");
pwmIncrementBlue = pwmIncrementBlue * -1;
pwmValueBlue = pwmValueBlue + pwmIncrementBlue;
}
// this takes care of the pwmValue being larger than pwmMax
if(pwmValueBlue >= pwmMax){
Serial.println("we reached Blue's top!");
pwmIncrementBlue = pwmIncrementBlue * -1;
pwmValueBlue = pwmValueBlue + pwmIncrementBlue;
}
analogWrite(pinBlue, pwmValueBlue);
}

Where to find components/tools/cool stuff:
Do to the change in culture here in America, hobbyist electronics is not as common as it once was. If you want to walk to a store to purchase electronic components your only option is likely to be Radio Shack. Radio Shack has an assortment of resistors and capacitors as well as a few IC(intergraded circuits). If you want a much better selection and lower cost, you will need to look online. Here is a list of some great electronics suppliers that I like.
Electronics:

Robotics:

Hardware/Materials:

1 comment:

  1. I have been a herpes carrier for 2 years and I tried every possible means of curing but all of no useful until I saw a health promotion on a herbalist from West Africa who prepares herbal medicines to cure all sorts of diseases including #HSV and many others sickness but look at me today am very much happy and healthy, I'm telling you today don't lose hope keep trying God is with you, If you needs Dr.Chala help contact him on his email dr.chalaherbalhome@gmail.com or you can visit his website on http://drchalaherbalhome.godaddysites.com or https://mywa.link/dr.chalaherbalhome

    ReplyDelete