0% found this document useful (0 votes)
39 views

Arduino Programming Part2 PDF

This document provides an overview and introduction to variable types and loops in Arduino programming. It discusses the common variable types like integers, floats, and strings that can be used. Integers like int and long are used for counting, while floats are for numbers with fractional parts. Loops like for and while loops are covered, with for loops needing a counter variable. Examples are given of declaring and assigning variables as well as using loops to repeat code. The document concludes with a case study on using floats to store sensor values from a photoresistor circuit.

Uploaded by

Ahmed Slim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Arduino Programming Part2 PDF

This document provides an overview and introduction to variable types and loops in Arduino programming. It discusses the common variable types like integers, floats, and strings that can be used. Integers like int and long are used for counting, while floats are for numbers with fractional parts. Loops like for and while loops are covered, with for loops needing a counter variable. Examples are given of declaring and assigning variables as well as using loops to repeat code. The document concludes with a case study on using floats to store sensor values from a photoresistor circuit.

Uploaded by

Ahmed Slim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arduino Programming

Part 2
EAS 199A
Lecture 6
Fall 2011

Overview

• Variable types
❖ int
❖ float
• Loops
❖ for loops
❖ while loops (another day)

Arduino Programming Part 2: EAS 199A 2

Assigning and Using Variables


Arduino web site
❖ http://arduino.cc/en/Reference/HomePage
❖ http://www.arduino.cc/en/Tutorial/Variables
❖ http://arduino.cc/en/Tutorial/Foundations

The more common variable types are


❖ integers:
‣ int, long, unsigned int, unsigned long
❖ floating point values: (numbers with fractional parts
‣ float, double
❖ characters and character strings
‣ char, string, String
❖ arrays

Arduino Programming Part 2: EAS 199A 3


Integers are used for counting
int
❖ integers in the range –32,768 to 32,767
unsigned int
❖ integers in the range 0 to 65,535
long
❖ integers in the range –2,147,483,648 to 2,147,483,647
unsigned long
❖ integers in the range 0 to 4,294,967,295

Arduino Programming Part 2: EAS 199A 4

Practical usage of int and long


Use an int for most common tasks requiring integers
❖ Use an int for most loop counters:
int i, n=16;

for ( i=0; i<n; i++) {


// loop body
}

❖ An int is returned by a built-in functions, e.g. analogRead


int val, photo_pin=4;
val = analogRead(photo_pin);

Arduino Programming Part 2: EAS 199A 5

Practical usage of int and long


Use a long when the range of values is very large, e.g.
measuring the system time in milliseconds
long start_time, current_time;
long wait_time = 86400000; // one day

void setup() {
start_time = millis();
Serial.begin(9600);
}

void loop() {
current_time = millis();
if ( (current_time - start_time) > wait_time ) {
Serial.println(“24 hours has passed”);
start_time = current_time;
}
}

Arduino Programming Part 2: EAS 199A 6


Floating point numbers are used for
computing with fractional values
float
❖ numbers with fractional part
❖ values in the range –3.4028235 × 1038 to 3.4028235 × 1038

Practical advice
❖ Use a float in formulas when fractional values are needed
❖ A float can be very large or small
❖ floating point math involves small rounding errors

Arduino Programming Part 2: EAS 199A 7

Integer and floating point variables use


different arithmetic rules
Integer math: Division rounds to nearest int
int a, b, c;

a = 4;
b = 3;
c = a/b; // Value of 1 is stored in c

Floating point math


float x, y, z;

x = 4.0; // Include “point zero” to reinforce


y = 3.0; // that x and y are floats
z = x/z; // Value of 1.3333333 is stored in z

Arduino Programming Part 2: EAS 199A 8

Use conversion functions to change type

Convert to an integer:
a = int(x);

Convert to a floating point value:


x = float(i);

Practical Advice
Use explicit type conversion functions to convey your intent

Arduino Programming Part 2: EAS 199A 9


Defining and Using Variables
❖ All variables must be declared before use
❖ Declaration consists of a type specification and the variable name
❖ A declaration may also include an assignment
❖ Use meaningful variable names
❖ Add comments to further clarify meaning
Examples
int red_pin; // declaration only
int blue_pin = 5; // declaration and assignment
int greenPin = 0;

float voltage; // Voltage of the input signal


float maxVoltage = 5.0; // Maximum range of analog input

sensorVal = analogRead(sensorPin); // get reading

// convert to floating point voltage


voltage = float(sensorVal)*maxVoltage/float(range);

Arduino Programming Part 2: EAS 199A 10

Case study: Use floats to store sensor values

Use photo-resistor circuit to


5V
create sensor input
❖ Convert input reading to a voltage
using floating point variables
❖ Use loops to compute the average photoresistor
of sensor readings

Analog pin 3

10 kΩ

Arduino Programming Part 2: EAS 199A 11

Try it! Measure photoresistor output


5V
Build the photo-resistor circuit
and run this program
int sensorVal; photoresistor
int sensorPin = 3;
float voltage; Analog pin 3
float input2volts = 5.0/1023.0;
10 kΩ
void setup () {
Serial.begin(9600);
}

void loop () {
sensorVal = analogRead(sensorPin);
voltage = float(sensorVal)*input2volts;
Serial.print(“sensorVal, voltage = “);
Serial.print(sensorVal); Serial.print(“ “);
Serial.println(voltage);
}

Arduino Programming Part 2: EAS 199A 12


Loops

Loops

Loops allow code to be repeated


❖ Repeated code goes in a block, surrounded by { }
❖ for loops
‣ need a counter
❖ while loops
‣ need an escape

int i; // declare counter

for ( i=0; i<=12; i++ ) { // standard structure

Serial.println(i); // send value of i to serial monitor

Arduino Programming Part 2: EAS 199A 14

Loops
Initial value of counter
i=0 only on first pass through the loop
Stopping test: Continue while this
condition is true

int i; // declare counter

for ( i=0; i<=12; i++ ) { // standard structure

Serial.println(i); // send value of i to serial monitor

Increment: How to change i on each


pass through the loop
Arduino Programming Part 2: EAS 199A 15
Loops
Common loop: increment by one
for ( i=0; i<=12; i++ ) { // increment by one
... code block goes here
}

Common loop: increment by two


for ( i=0; i<=12; i+=2 ) { // increment by two
... code block goes here
}

Decrement by one
for ( i=12; i>=0; i-- ) { // decrement by one
... code block goes here
}

Arduino Programming Part 2: EAS 199A 16

Try it! Modify the photoresistor program


5V
Change the loop function
(modify your previous code) photoresistor

void loop () { Analog pin 3

float sensorAve; 10 kΩ
int sensorSum;
int nave=5;

sensor_sum = 0.0; This code contains errors


for ( i=1; i<=nave; i++ ) { that you will need to fix
sensorVal = analogRead(sensorPin); before it runs!
sensorSum = sensorSum + sensorVal;
}
sensorAve = float(sensorSum)/float(nave);
voltage = sensorAve*input2volts;
Serial.print(“Average voltage = “);
Serial.println(voltage);
}

Arduino Programming Part 2: EAS 199A 17

Test it! Break your code to learn how it works

Change nave
❖ Increase nave from 5 to 10, 50, 100, 500
❖ Why is the reading negative for large nave?
❖ How can you fix this by changing the variable type for sensorSum?
Add print statements inside the averaging loop
Serial.print(“\t Reading = “);
Serial.println(sensorVal);

Arduino Programming Part 2: EAS 199A 18

You might also like