Introduction to Arduino programing

PASTE ALL CODE IN AN IDE OR NOTEPAD++ FOR BETTER READABILITY

PROS:

-Good for small control systems

-reading analog voltages across it’s A0-A5 pins

-outputting PWM signals

-powered via USB

-Metric ton of proprietary shields and documentations

CONS:

-Memory Limitations

-processing power limitations

- Pin Limitations (without extra shields)

PIN INFORMATION

A0-A5: Used for analog INPUTS (0-5v) (0-1023)

DIGITAL(PWM~)

0-1: TX RX used for serial communication

2,4,7,8,12,13: Used for digital out (0 OR 5v)

~#Pins: Used for PWM OUTPUT(0v 5v @ ___% dutycycle)

RESET BUTTON: Press it to restart your program, 1st iteration.

THE REAL STUFF:

Some basic background before we jump into coding.

Data Types:

int FOR NUMBERS (letters turn into ascii code)

char FOR THE LETTERS ( numbers are treated as strings)

bool FOR TRUE OR FALSE ( indicated by 1 or 0)

boolean SAME THING AS bool

How do I even use these:

int x; //instantiates an ‘x’ variable with NO VALUE.

x= 2; // previously instantiated x value now has a value of 2.

//Use semicolons “ ;” to indicate the end of a statement.

// Use “ // “ to write comments.

MATHMATICAL :

Arduino IDE recognizes these symbols:

+ or – Plus or minus.

* or / Multiplication or Division

( ) Parentheses

++ Increment

-- decrement

= EQUAL

== EQUALITY (Note. This is used differently than = . See example below)

!= NOT Equal

<= Lessthan or eq.

>= Greaterthan or eq.

What’s ‘==’?

if( x == 5)

{

Do stuff in this Scope. //SCOPE is gone over later.

}

What’s ++ and --?

X=0;

X++; // X=X+1

X++; //X=X+1

//So now X=2 after we INCREMENTED 2 times.

X--; // X = 1 after we DECREMENTED 1 time.

ANALOG VS DIGITAL

ANALOG : 0-5v using the AD converter on the Arduino we can take in an analog value and convert it into a number we can use (digital number) ranging from 0-1023.

DIGITAL: 0 or 5volts. false or true. Used by pins 2-13. We can use digitalRead() or digitalWrite() in our programs to take in values or set pin voltages.

SCOPE

So to understand scope lets look at a simple “Hello World Program”.

//////////////////////////////////

int x = 0; // GLOBAL scope variable.

void setup(){ //FUNCTION SCOPE indicated by ‘{ ‘ & ’}

Serial.begin(9600); //Opens serial communications @ 9600 baud //rate

//Object Serial, Function ‘begin( int x ) ‘

// Used to initialize pins here in this scope.

}

void loop(){ //Another function scope

Serial.Println(“Hello World”); // Prints hello world in com. window

x++; //increment x

Serial.Println(x); //prints x in com. window

}

BASIC PROGRAMING KNOWLEDGE: Functions used by arduino often

pinMode( x, OUTPUT/INPUT):used to inititialize pins on the arduino

digitalRead ( x) : reads value on specified pin as either HIGH (5v) or LOW (0v)

AnalogWrite(x, 0-255): PWM value to specified pin, MUST HAVE ‘ ~’ Adjacent to pin # on board

LAB 1: BUTTON -> LED

description: button drives LED

CODE:

//Global variables. Often used for

int led = 2;

int button = 3;

int BVal= 0;

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

pinMode(led, OUTPUT);

pinMode(button, INPUT);

}

// the loop routine runs over and over again forever:

void loop() {

BVal= digitalRead(button);

if(BVal==1){

digitalWrite(led, 1); ; // turn the LED on (HIGH is the voltage level)

}

if(BVal==0)

digitalWrite(led, 0);// turn the LED off by making the voltage LOW

}

NOTES:

IF STATEMENTS

-used for controls

-statement inside () can be either true or false.

-USE THE == FOR EQUALITY. DO NOT USE ‘=’. don’t worry it’s a common mistake even I do at times.

ARRAYS

-data structure.

-total bad ass.

-int foo[5] = {2,11,22,13,124};

^an example of array instantiation with 5 total elements

-int x =foo[3]; //x value is now = 22.

-int x= foo[0]; //x value is now = 2

-int x= foo[5]; //you’re drunk. go home. array element 5 does not exist.

-useful as a global variable to hold a bunch of values you don’t want to retype.

LAB 2:

BUTTON DEBOUNCING FOR A COUNTER:

note, extra part for resetting is included.

Same schematic as LAB 1.

Importance of debouncing can be found here:

CODE:

int buttonPin = 3;

int ledPin = 2;

int ledState = LOW;

boolean buttonState = LOW;

int pressed=0;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

}

void loop() {

if(debounceButton(buttonState) == HIGH & buttonState == LOW) //initial state is low on first iteration. if button state is low and debounce high, button is high.

{ //debouncing occurs within the first parameter of the if statement.

pressed++;

buttonState = HIGH;

}

else if(debounceButton(buttonState) == LOW & buttonState == HIGH) //edge detection from low to high, begining indication for button press does not constitute press++

{

buttonState = LOW; //buttonState to low if the debounce button is low and the state is low

}

if(pressed == 5)

{

digitalWrite(ledPin,HIGH);

}

if(pressed==6)

{

digitalWrite(ledPin,LOW);

pressed=0;

}

}

//////////////////////////////////////////////////////////////////////////////debouncing function

boolean debounceButton(boolean state)

{

boolean stateNow = digitalRead(buttonPin); //function re reads the state of the button

if(state!=stateNow) //if the button is not the same as what was passed to the state

{

delay(10); //wait 10ms for a stable button response

stateNow = digitalRead(buttonPin); // then apply the button's current value to stateNow variable

}

return stateNow; //return a value for the button's state (debounced);

}

LAB 3: LED BUTTON, DEBOUNCE, PWM BRIGHTNESS

Same Schematic. as lab 1

CODE:

int buttonPin = 2;

int ledPin = 3;

int ledState = LOW;

boolean buttonState = LOW;

int pressed=0;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

}

void loop() {

if(debounceButton(buttonState) == HIGH & buttonState == LOW) //initial state is low on first iteration. if button state is low and debounce high, button is high.

{ //debouncing occurs within the first parameter of the if statement.

pressed++;

buttonState = HIGH;

}

else if(debounceButton(buttonState) == LOW & buttonState == HIGH) //edge detection from low to high, begining indication for button press does not constitute press++

{

buttonState = LOW; //buttonState to low if the debounce button is low and the state is low

}

if(pressed==4) //resets counter

pressed = 0;

analogWrite(ledPin,85*pressed);

}

boolean debounceButton(boolean state)

{

boolean stateNow = digitalRead(buttonPin); //function re reads the state of the button

if(state!=stateNow) //if the button is not the same as what was passed to the state

{

delay(10); //wait 10ms for a stable button response

stateNow = digitalRead(buttonPin); // then apply the button's current value to stateNow variable

}

return stateNow; //return a value for the button's state (debounced);

}

EXPLAINED: analogWrite(ledPin,85*pressed);

sets pin ‘ledpin’ to either 0, 85, 170, 255. These indicate the voltage level on the pin

(0v, 1.25v, 2.5v,3.75v,5v)

NOTE, these values are done by doing 5v at different duty cycles. 5v @ 0% duty cycle, 5v @ 25% duty, 5v @ 50%, 5v @ 75%, 5v @ 100%.

The average value of the signal indicates the voltage level.

LAB 3.5.

USING AN ARRAY TO SET PINS.

No schematic is necessary for this, but we will be setting pins up as input and output.

int RGBpins[3] = {9,10,11}; //pins on led are on 9 10 and 11

int butPin = 2;

void setup(){

for(int i = 0; i<3; i++)

{

pinmode(RGBpins[i],OUTPUT) ;

pinmode(butPin, INPUT) ;

}

FOR LOOPS

For loops need 3 statements inside them while If statements only need 1. Break it down.

for( int i=0; ‘ i’ is used as a counter for this entire for loop.

for(int i=0; i<3; while ‘i’ is less than 3, stay in this for loop.

for(int i=0;i<3;i++) Each time you get to the end of the loop, increment I ONE TIME.

These three statements end up causing the for loop to go through three iterations . Each iteration results in the ‘i’ counter to be incremented.

Compare RGBpins[] to our array example. It’s used to hold values, and we don’t have to keep rewriting pinmode(9, OUTPUT)

pinmode(10, OUTPUT)

pinmode(11, OUTPUT)

Saves time, does not save memory.

Lab 4, Servo control and Potentiometer

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer

int val = 0; // variable to read the value from the analog pin

void setup() {

myservo.attach(3); // attaches the servo on pin 9 to the servo object

}

void loop() {

val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)

val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}

See Part 2 for : (Not yet completed)

DC motors

Transistors

I2C communication

Stepper Motors

Lab 1: DC Motors & Transistors