Download Arduino presentation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Immunity-aware programming wikipedia , lookup

Transcript
Arduino Programming
THE ARDUINO IS A
MICROCONTROLLER –
A LOW COST, LOW
PERFORMANCE
COMPUTER
MICROCONTROLLERS DO
SIMPLE THINGS WELL
Basic sensing and response
LOW POWER
How much power does your
laptop need?
Most microcontrollers are capable of
going into a sleep mode where they
draw very little power
~50πœ‡π΄
www.mouser.com
~2𝐴
www.raspberrypi.org
ARDUINO
PROGRAMMING
Before you begin, go to tools, and
make sure the board is set to Uno, and
the Serial Port is set to the highest
number
Make sure you can upload the code
blink from examples/basic.
If it worked, there should be a light
slowly blinking on your Arduino
Every code your write needs to have
two sections, void setup and void loop,
but they can be empty
BASIC VARIABLES AND
MATH
The serial monitor lets you print
values from your code. It is going
to be your best friend when it
comes to debugging your code
Click
here
You will (hopefully not) make this
mistake!
The serial lines are connected to digital pins 0
and 1 on the Arduino. You usually don’t want to
connect anything else to these lines.
You can use variables to store
numbers, but you might get funny
results
int a = 6;
float a = 6;
int b = 2;
float b = 2;
void setup() {
void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
Serial.begin(9600);
Serial.begin(9600);
Serial.print("a divided by b is: ");
Serial.print("a divided by b is: ");
Serial.println(a/b);
Serial.println(a/b);
Serial.print("b divided by a is: ");
Serial.print("b divided by a is: ");
Serial.println(b/a);
Serial.println(b/a);
}
}
void loop() {
// put your main code here, to run
repeatedly:
void loop() {
// put your main code here, to run
repeatedly:
}
}
My advice would be to use floats for
all of your variables until you get
better at figuring out how these work.
CONTROLLING YOUR
CODE
If statements can be use to make basic
decisions in your code
Syntax
if(conditional statement)
{
Your code goes here
}
Example
if(testVal == number)
if(testVal < number)
if(testVal > number1 && testVal < number2 )
Else if and else
Syntax
if(conditional statement)
{
}
else if(conditional statement)
{
}
else
{
}
Anatomy of a for loop in Arduino
The variable i here is our counter
You need to declare the variable with
β€œint”
Example:
This section says to run
the loop while the
counter i is less than
10
for(int i = 0; i<10 ; i++)
{
Serial.println(i);
This section says to add 1 to
our counter each time
through the for loop
}
This code will now run 10 times
Serial monitor output
You can read a sensor with the
analogRead() function (see the
example in basic/analogReadSerial).
The Arduino gives each voltage
between 0 and 5 volts a number
between 0 and 1023.
How could we convert from the
number we get to a real voltage?
Use the LM35 temperature sensor to
measure temperature. The voltage
goes up 10 mV for each degree C. Print
the temperature to the serial monitor.