Saturday, March 19, 2011

More fun with Arduino! PComp with Arduino Class 2

Here is a short list of the topics we will cover for class 2:

How to comment code and why it is important.

Variables: What they are and how to use them.

Basic Programming Structure

Lab: Inputs from sensors and intro to 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:

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: boolean isButtonPressed = false;

A boolean holds one of two values, true or false. (Each boolean variable occupies one byte of memory.)


char: char incomingLetter = 'a';

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'.


byte: byte ledPin = 13;

A byte stores an 8-bit unsigned number, from 0 to 255.


int: int potValue = 1023;

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: word earthCircumference_inMiles = 24860;

A word stores a 16-bit unsigned number, from 0 to 65535. Same as an unsigned int.


long: long seconds_in_a_year = 31536000;

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. Often used to reference time passed in milliseconds.


float: float pi = 3.14159;

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 phi = 1.618;

Double precision floating point number. Occupies 4 bytes. Same thing as a float.


String: String userName = "Matthew Richard";

Strings are represented as arrays of type char.


array: int ledPins[] = {3,5,6,9,10,11};

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:

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.


Sensors - how to read the world around you


At its core Arduino was designed to help gather information about the world for use in projects and art. The way we get that information with electronics is by using sensors. Sensors come in two basic flavors, digital and analog. Digital sensors send information as HIGH or LOW, or encoded in a serial protocol. Analog sensors work by changing the voltage of a circuit that is then compared to a standard. The change in voltage is the value attributed to the sensor.


The Switch and Serial Monitor:




The Switch and LED:




The Potentiometer and Serial Monitor:


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.


The Potentiometer and LED:




The Photocell and LED:


Two Potentiometers and One LED:




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

Jameco - Professional Electronic Parts and ICs

Mouser - Professional Electronic Parts and ICs


Robotics:

Pololu - Motor controllers and sensors

LynxMotion - Servo housings and metal robot kits


Hardware/Materials:

SmallParts - small mechanic components and hand tools



McMaster Carr - all sorts of awesome building materials and tools, delivers fast

2 comments:

  1. Componentbuy is one of the fastest growing distributors of Semiconductors and electronic Components in the world.Supports more than 20 Millions of Electronic components sourcing needs with Engineering Data support. Locating all your required components for your design and production needs.
    Our website will act as tool for the sourcing and Design engineers on the components needs.
    componentbuy.com

    ReplyDelete
  2. 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