Você está na página 1de 11

05/19/14

Microcontrollers
A microcontroller is a little computer, that has a processor, 1-2 kilobytes of RAM for holding data, and
a few kilobytes of EPROM aka flash memory for holding your programs.
I/O pins can be either digital or analog.

Voltage Regulators
Regulate voltage to a constant value.
Ex. (7V-12V) - - > regulates to - - > (5V)

Analog Inputs/Outputs on Arduino


These inputs are mostly only used to measure voltage levels since very little current can flow through
them. This is due to the large internal resistance.

Digital Inputs on Arduino


When used as outputs, they behave like power supply voltages since they are off or on (0V or 5V).
The digital pins can only handle 40 mA at 5V. Anything requiring a higher current requires use of a relay.

Microcontroller
The CPU controls everything that goes on within the device. It fetches instructions stored in the flash
memory and executes them. This could also mean fetching data from and to the RAM. The EEPROM
memory is similar to flash memory, but it is nonvolatile which means that it remains in the device even
after it is turned off. EEPROM is used whenever you dont want to lose data after being turned off.

Quartz Crystal Oscillator


Oscillates to generate precise timing to be utilized by the microcontroller.

Memory Types
RAM = working memory
Flash = computer programs aka (sketches)
EEPROM (Non-Volatile)

Blink Example with Arduino


Example Code below,

05/21/14

====================================================
/*
Blink
Turns on an LED on and off
*/

// giving led pin a name


int led = 13;

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


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

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


void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is on [5V])
delay(5000);

// wait for a delay time in mS

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

// wait for a delay time

=====================================================================

Explanation of above code


Top Section
/*
Blink
Turns on an LED on and off
*/

This section at the very top of the code is a multiline comment started by (/*) and ended by (*/). This
code section explains the code that will be written. It is not really needed. A single line comment is
started with a double slash (//).

Top Section Uncommented


// giving led pin a name
int led = 13;

This section initializes any variables that are required before the Setups Section
( int led = 13, initializes a variable called led and assigns it a value of 13 )

Setup Section
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

The Setup section initializes weather pins will be input or output pins. The setup portion of the code will
only run once at the beginning of the program or anytime the reset pin is pressed.
( pinMode(X,A), sets the mode of the pin X as either an INPUT or OUTPUT)
(A = INPUT) if the pin will be a input pin, or (A = OUTPUT) if it will be an output. Must be capitalized.

Loop Section
// the loop routine runs over and over again forever:
void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is on [5V])


delay(5000);
// wait for a delay time in mS
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
// wait for a delay time
}

The loop section runs after the setup section has run and will run forever or until the power source has
run out.
( digitalWrite(X,B), turns an output pin X on [HIGH] or off [LOW] )
(B = HIGH) if we want pin X to be on (5V) or (B = LOW) if we want pin X to be off (0V). Must be
capitalized.
( delay(X), will pause the microcontroller program by X milliseconds)
05/22/14

C Language Basics

digitalWrite(13, HIGH);
the digital write function must be spelled like shown and is used to turn on and off the pin. This example
above turns on pin #13. Everything should end with a semicolon.
05/23/14
void setup()
{
}
The code above defines a function called setup. Setup and loop must be defined in every
sketch/program that you write. The void means that these function do not return a value. After the
keyword void comes the name of the function and parentheses to contain any arguments.no semicolon
after setup() because the function is being created and not called. Anything within the curly braces is
known as a lock of code

pinMode(13, OUTPUT);

pinMode sets a particular pin to be either an input or an output. In this case, pin 13 is set as an output
pin. To set as an input, change OUTPUT to INPUT. Must be capitalized. pinMode is usually put in the
setup portion of the code and not in the loop section since there is no need to

int ledPin = 135;


the command above creates an integer variable with a value of 135;

int variable1;
the command above creates an integer but does not assign a value for it. This is useful when you dont
know the value of a variable.

variable1 = 2;
the command above assigns a value to a previously created variable.

Serial.begin(9600);
The line above initializes the serial communication between Arduino and software on computer. The
number inside parenthesis is the baud rate and is the speed at which the computer communicates with
the Arduino.

Serial.println(variable);
The line above prints the value of the variable.

If (a<b)
{
Put code here
}
The if statement above takes a condition that goes in the parenthesis. And the code to execute if the
condition is true goes in the curly braces.
5/27/14
Operator
<
>
<=
>=
==
!=

Meaning
Less than
Greater than
Less than or equal to
More than or equal to
Equal to
Not equal to

Example
9 < 10
10 > 10
9 < = 10
10 >= 9
9 == 9
9 != 9

Result
True
False
True
True
True
False

*do not confuse a declarative (=) to a comparison (==)

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


{
Code goes here
}
The for loop takes three input arguments. The first argument is a variable that will be used as a counter
with its initial value. The second input is a condition that must be true in order to stay in the loop. The
third input argument is what to do every time the microcontroller has done all the things in the loop.

In this example FOR loop shown above, the counter is i with an initial value of zero. If i is below
20, the code will execute. After the code is executed but before it is executed again, i increases
value by one.

X ++;
The expression above is exactly the same as [ X = X + 1;]

while ( a < 20)


{
Code goes here
a ++
}
The while loop takes one input argument. The input argument is the condition that must be true to stay
in the while loop.

void loop()
{
static int count = 0;
code
count = count + 1;
}
The code above is just to demonstrate static int. static int makes sure that the int declaration is only
done at the beginning of the loop. If the static were not placed, count would never go above 1 because
it would be re-declared to zero at the beginning of the loop. However, since there is a static in front of
the integer declaration, once it is initialized with a value of zero, it will not be reinitialized.

Writing Functions for Arduino


void functionName( int input1, int input2)
{
Code goes here
}
A function is given inputs and its job is to perform calculations and. The void means that there is no
output to this function. Everytime the function is called, just type in the function name and give it the
inputs. For example functionName(4,6). However, the variables input1 and input2 can only be used
within the function.

int functName( int input3)


{
Code here
output = some number;
return(output)
}
The code above is also a function, however the int in front of the function name means that this
function outputs an integer. The return is used to specify what value will be returned as an output.

Global, Local, and Static Variables


A variable that is declared outside a function is a global variable. This means it can be used inside or
outside functions. If a variable is declared inside a function, it can only be utilized inside the function and
is therefore a local variable.

Floats
If the numbers required will require decimal points, integers cannot be used easily. Floats are used in
these situations. An example function with a float is shown below.
float centToFaren(float C)
{
float f = C * 9.0 / 5.0 + 32.0;
return f;
}
The above code will be a floating variable. The numbers are written with a .0 like 8.0 to treat them as
floats rather than ints.

// comments should be added constantly in order to keep you code easy to read and understand.

int array1[ ] = {2, 30, 50, 27, 65};


the line above creates an array (vector). In order to create an array, you have to put square brackets
after the name of the array. The values of the array are placed inside the curly braces and separated by
commas. Remember that the first value if the array is located in location zero. so array1(2) will return
50, and array1(0) will return 2.

?
?
20
2
3
2
?

An(6)
An(7)
Array2(0)
Array2(1)
Array2(2)
Array2(3)
Bn(0)

Be careful when calling arrays. When a array is created, the next available memory spaces are saved for
use by that array. If you try to call Array2(4), you will get a value, however who knows what will be
returned since that space is used by another array. Make sure that you do not go outside the size of the
array

6/11/14

Checking if computer is trying to communicate with the Arduino over UART.

Serial.avaliable()

The function above returns the number of bytes of data in the buffer waiting to be read.

6/12/14

Pull Up Resistors
Pull up resistors are used to prevent a pin from floating when it is not being used. See pg. 90.

Debouncing
Debouncing is placing a small delay after a button is pressed before the reading is actually taken. This is
because the value after a button is pressed bounces for a small amount of time which could create
problems when taking readings with a microcontroller. See pg. 94.

random(1, 10)
the function above creates a random number between 1 and 10. See pg. 105 for information on random
numbers. The random numbers are no really random since this is against the inner workings of the
microcontroller. If you want really random numbers, youre going to need to seed values from the real
world.

6/16/14

Hexadecimal and binary information (pg 109)

Interrupts
If the Arduino receives a signal in a specified way, the Arduino processor will suspend what its
doing and run a function attached to that interrupt. See pg 113

6/19/14

int interruptPin = 2;
int ledPin = 13;
int period = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT);
digitalWrite(interruptPin, HIGH); // pullup
attachInterrupt(0, goFast, FALLING);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(period);
digitalWrite(ledPin, LOW);
delay(period);
}
void goFast()
{
period = 100;
}

=============================================================
The key to interrupts is the attachInterrupt function that is placed in the setup part of the code. The first
of the inputs is the pin that will be used. Since the Arduino uno only has only two interrupt pins, the first
input to the attach function will be 0 or 1 (pin 2 or 3). The second input is the name of the function that
will be executed when the interrupt is started. The last input is the condition that will cause the
interrupt to start running the code. (CHANGE, FALLING, or RISING). The signal is five volts max, so a
falling signal is from 5V to 0V. a rising signal is 0V to 5V, and a change is any of the two.

6/20/14

Arduino Data Storage


The Arduino has 2k bits of ram memory to create constants, however, whenever memory is a bit
tight, constants can be stored in the 32k of flash memory used to store programs rather than the 2k of
ram.

PROGMEM

To save data in flash memory, the progmem library has to be included using the following
statement.
#include <avr/pgmspace.h>

EEPROM
EEPROM is designed to store data for years. (electrically erasable read only memory). Despite
the name it is not really read-only. You can write to it. It a little more work to store memory here since
you have to store one byte at a time. (pg 117)

Você também pode gostar