Programming in Arduino

Download Report

Transcript Programming in Arduino

Pre Pres. Notes
Casperelectronics
Photos from workshop?
Programming in Arduino
Materials:Arduino
Board
• Arduino.cc has the IDE programming
software for free download
Arduino Sketch
Arduino files are called “sketches”.
Arduino sketches must be compiled in the
IDE before being downloaded to the
Arduino board.
Program Structure
• Two required functions in an Arduino sketch,
setup() and loop().
• setup() The function is called when your program starts. Use
it to initialize your variables, pin modes, start using libraries, etc.
The setup function will only run once, after each powerup or
reset of the Arduino board.
• Loop() After creating a setup() function, which initializes and
sets the initial values, the loop() function does precisely what its
name suggests, and loops consecutively, allowing your program
to change and respond. Use it to actively control the Arduino
board.
What does “void” mean
• The void keyword is used only in
function declarations. It indicates that
the function is expected to return no
information to the function from which it
was called.
Libraries
• Extend the capabilities of Arduino IDE
• Offer simpiler ways of controlling
hardware they are written for.
• Advanced. will talk about later.
Variables
• Let you store a value.
• Must declare variables and tell
computer what kind of value to expect.
Int- number between -32,768 to 32,767
Byte- 0 -255
char- type character
and more……
Int variable
int- an integer is a whole number
(1,2,3,25)
• are your primary datatype for number
storage, and store a 2 byte value (take
2 bytes of memory). This yields a range
of -32,768 to 32,767
example:
int ledpin = 13; // sets the variable named ledpin to 13
Char variable
char- A data type that takes up 1 byte of
memory that stores a character value.
Both of these are equivalent and refer to the
letter A:
char myChar = 'A';
char myChar = 65; in ASCII
Byte variable
• Byte- an integer between 0 and 255
(byte uses less memory, only 1 byte)
• Made up of 8 bits.
Example in binary:
00000001, 00000011
Binary Numbering
(how computers count)
If computers only know 1 and 0 how do
they form complex operations?
• 128 64 32 16 8 4 2 1
• 00000001 = 1
• 00000010 = 2
• 00000011= 3
• 00000100 = 4
Programming- Functions
• Segmenting code into functions allows a programmer
to create modular pieces of code that perform a
defined task and then return to the area of code from
which the function was "called".
• Existing Functions- commands Arduino
automatically understands
• Custom Functions -The typical case for creating
a function is when one needs to perform the same
action multiple times in a program.
Example Functions:
pinMode(), digitalWrite(), and delay()
•
The pinMode() function configures a pin as either an input or an output.
pinMode(ledPin, OUTPUT);
// sets the digital pin as output
•
The digitalWrite() function outputs a value HIGH or LOW on a pin. For
example, the line:
digitalWrite(ledPin, HIGH); //sets pin high
•
The delay() causes the Arduino to wait for the specified number of
milliseconds before continuing on to the next line. There are 1000
milliseconds in a second, so the line:
delay(1000); // creates a delay of one second.
Pseudocode: Blink an Led
English:
I want an led to blink, on for a second and off for a second forever.
Pseudocode:
Turn led on
wait
Turn led off
Loop again
Pseudocode Translation
void loop()
// run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000);
// waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
// waits for a second
}
Blink an Led
(w/o comments)
int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
Blink an Led
int ledPin = 13;
// LED connected to digital pin 13
void setup()
// run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
// run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000);
// waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
// waits for a second
}
• Demo Programming board
HW : Bring in a video or webpage link that is related
to Physical Computing that you think is interesting
and be prepared to talk about it in class.