+ All Categories
Home > Documents > Fan Project Complete Slides

Fan Project Complete Slides

Date post: 11-Feb-2018
Category:
Upload: abu-abdullah-sameer
View: 215 times
Download: 0 times
Share this document with a friend
64
Desktop Fan Project for the Arduino Inv entors Kit EAS 199A, Fall 2010 Gerald Recktenwald Portland State University [email protected]
Transcript
Page 1: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 1/64

Desktop Fan Projectfor the

Arduino Inventors KitEAS 199A, Fall 2010

Gerald RecktenwaldPortland State University

[email protected]

Page 2: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 2/64

Desktop fan: EAS 199A

Goal

• Build a desktop fan from parts in the Arduino Inventor’s Kit

• Work in teams of two

• Learn new skills! Controlling a servo and DC motor

! Make a 2D drawing with Solidworks! Send drawings to Laser cutter

! Soldering

• Due in two weeks! In-class demonstration of your working fan

2

Page 3: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 3/64

Desktop fan: EAS 199A

Tasks

3

• Measure servo and DC motors! prepare for structural design

! learn how to use your calipers

• Sketch design of support

structure on paper• Create Solidworks model of the

base and DC motor support

• Cut acrylic parts

• Re-solder the DC motor leads

• Assemble the system

• Write Arduino program tocontrol servo and DC motor

Page 4: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 4/64

Desktop fan: EAS 199A

Propellor and motors from Inventor’s Kit

4

Page 5: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 5/64

Desktop fan: EAS 199A

Acrylic parts after cutting and bending

5

Page 6: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 6/64

Desktop fan: EAS 199A

One idea for a base design

6

Page 7: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 7/64

Desktop fan: EAS 199A

Alternative base designs

7

Page 8: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 8/64

Desktop fan: EAS 199A

Servo motor parts

8

Page 9: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 9/64

Desktop fan: EAS 199A

Fan Project: First Steps

9

1. Make a hand sketch of the structural parts

2. Measure the servo and mounting screws

3. Use measurements to add dimensions to the sketch

4. Redraw the sketch as a 2D “flat” drawing in Solidworks

5. Email the drawing to the instructora. Laser cutter works on thin sheets in 2D

b. Use the acrylic bender after parts are cut

Watch this video to see the laser cutter and acrylic benderin action:

  http://www.youtube.com/watch?v=DJA8EmBUfLo

Page 10: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 10/64

Soldering Leads to the

DC MotorDesktop fan projectEAS 199A, Fall 2010

Page 11: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 11/64

Desktop fan: EAS 199A

Overview

The DC motor that comes with the Arduino Inventor’sKit has short and delicate leads. We need to replace theleads with more robust wiring and solderedconnections

11

Page 12: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 12/64

Desktop fan: EAS 199A

Temperature-controlled soldering iron and flux

12

Page 13: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 13/64

Desktop fan: EAS 199A

!

#!!

$!!

%!!

&!!

'!!

(!!

)!!

*!!

'+$$ (+## )+# )+$# *+#! *+%! ,+#, #!+,

   !   "   #   $    %    %   &   '   "   (   *   '   +    ,   -   $   .

   "   (

/+('

0+%% 1232 !"#$%%&'"( 4'#5.5 6&'

-.. /-0

-.. 1/

/-0 234567849

1/ 234567849

Soldering work surface with vise

13

Page 14: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 14/64

Desktop fan: EAS 199A

Helping Hands

14

Page 15: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 15/64

Desktop fan: EAS 199A

Procedure

15

1. Cut a length of wire

2. Strip and tin the ends of the wire

3. Make note of polarity and remove leads from DC motor

4. Insert tinned wire through tabs and bend into position

5. Secure new leads by soldering to motor tabs

Page 16: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 16/64

Desktop fan: EAS 199A

Cut new lead wires

16

Page 17: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 17/64

Desktop fan: EAS 199A

Strip the leads

17

Page 18: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 18/64

Desktop fan: EAS 199A

Tin the leads

18

Page 19: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 19/64

Desktop fan: EAS 199A

!

#!!

$!!

%!!

&!!

'!!

(!!

)!!

*!!

'+$$ (+## )+# )+$# *+#! *+%! ,+#, #!+,

   !   "   #   $    %    %   &   '   "   (   *   '   +    ,   -   $   .   "   (

/+('

0+%% 1232 !"#$%%&'"( 4'#5.5 6&'

-.. /-0

-.. 1/

/-0 234567849

1/ 234567849

Remove the old leads and note the polarity

19

Page 20: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 20/64

Desktop fan: EAS 199A

Motor supported. Extension wires in place

20

Page 21: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 21/64

Desktop fan: EAS 199A

Bend the tinned wires around the supports

21

Page 22: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 22/64

Desktop fan: EAS 199A

Secure the leads with solder

22

Page 23: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 23/64

Basic DC Motor Circuits

Desktop fan projectEAS 199A, Fall 2010

Page 24: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 24/64

Desktop fan: EAS 199A

Simplest DC Motor Circuits

Connect the motor to a DC power supply

+5V +5V

 I 

Switch closed

24

Page 25: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 25/64

Desktop fan: EAS 199A

Current continues after switch is opened

Opening the switch does not immediately stop currentin the motor windings.

25

Page 26: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 26/64

Desktop fan: EAS 199A

Reverse current

Charge build-up can cause damage

+5V

 I 

+

 –

Arc acrossthe switch

Current surge throughthe voltage supply

26

Page 27: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 27/64

Desktop fan: EAS 199A

Motor Model

Simple model of a DC motor:! Windings have inductance and resistance

! Inductor causes a storage of electrical charge in the windings

! We need to provide a way to safely dissipate the charge stored inthe motor windings

+5V

 I 

+5V

27

Page 28: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 28/64

Desktop fan: EAS 199A

Flyback Diode

A flyback diode allows the stored charge to dissipatesafely

28

Page 29: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 29/64

Desktop fan: EAS 199A

Replace the Switch with a Transistor

A transistor allows on/off control to be automated

+5V

motorPin

330Ω

29

Page 30: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 30/64

Desktop fan: EAS 199A

Control the DC motor with PWM Output

// Function: PWM_output//// PWM output to control a DC motor

int motor_pin = 5; // must be a PWM digital output

void setup(){

  pinMode(motor_pin, OUTPUT)}

void loop(){  int motor_speed=200; // must be >0 and <= 255 analogWrite( motor_pin, motor_speed);

}

30

Page 31: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 31/64

Arduino Programming:PWM Control of 

 DC motor speedDesktop fan projectEAS 199A, Fall 2010

Page 32: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 32/64

Desktop fan: EAS 199A

Overview

Part I! Circuits and code to control the speed of a small DC motor.

! Use potentiometer for dynamic user input.

! Use PWM output from an Arduino to control a transistor.

!

Transistor acts as variable voltage switch for the DC motor.Part II

! Consolidate code into reusable functions.

! One function maps 10-bit analog input to 8-bit PWM output.

! Another function controls the motor speed.

! Functions developed here are useful for more complex controltasks, e.g. the desktop fan project.

32

Page 33: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 33/64

Desktop fan: EAS 199A

Part 1: Control motor speed with a pot

Increase complexity gradually. Test at each stage.

1. Use a potentiometer to generate a voltage signal

a. Read voltage with analog input

b. Print voltage to serial monitor to verify

2. Convert 10-bit voltage scale to 8-bit PWM scalea. Voltage input is in the range 0 to 1023

b. PWM output needs to be in the range 0 to 255

c. Print voltage to serial monitor to verify

3. Connect PWM output to DC motor

4. Write a function to linearly scale the data

5. Write a function to update the motor

33

Page 34: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 34/64

Desktop fan: EAS 199A

Potentiometer Circuit

Use the potentiometer from the Arduino Inventor’s Kit

34

Analoginput pin

5V

V in

V out

Page 35: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 35/64

Desktop fan: EAS 199A

Code to print potentiometer reading

// Function: read_potentiometer//// Read a potentiometer and print the reading

int sensor_pin = 3; // Wire sweeper of pot to  // analog input pin 3void setup(){

  Serial.begin(9600);}

void loop(){  int val; val = analogRead( sensor_pin );

  Serial.print("reading = ");  Serial.println( val );}

35

Page 36: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 36/64

Desktop fan: EAS 199A

DC Motor Control Circuit

36

+5V

motorPin

330Ω

Add this to thebreadboard with thepotentiometer circuit

Page 37: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 37/64

Desktop fan: EAS 199A

DC Motor Control Circuit

37

+5V

motorPin

330Ω

Analoginput pin

5V

Arduino

Page 38: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 38/64

Desktop fan: EAS 199A

Control the DC motor with PWM Output

// Function: DC_motor_control_pot//// Use a potentiometer to control a DC motor

int sensor_pin = 3;int motor_pin = 5; // must be a PWM digital output

void setup()

{  Serial.begin(9600);  pinMode(motor_pin, OUTPUT)}

void loop(){  int pot_val, motor_speed;

 pot_val = analogRead( sensor_pin );

  motor_speed = pot_val*255.0/1024.0; // Include decimal  analogWrite( motor_pin, motor_speed);}

38

Subtle: Don’t use integer values of 255 and1024 here. Aggressive compilers pre-computethe integer division of 255/1024 as zero.

Page 39: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 39/64

Desktop fan: EAS 199A

// Function: DC_motor_control_pot//// Use a potentiometer to control a DC motor

int sensor_pin = 3;int motor_pin = 5; // must be a PWM digital output

void setup()

{  Serial.begin(9600);  pinMode(motor_pin, OUTPUT)}

void loop(){  int pot_val, motor_speed;

 pot_val = analogRead( sensor_pin );

  motor_speed = pot_val*255.0/1024.0; // Include decimal  analogWrite( motor_pin, motor_speed);}

Part II: Create functions for reusable code

39

Adjust motor speed

Map input values to output scale

Page 40: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 40/64

Desktop fan: EAS 199A

adjust_motor_speed takes careof the two main tasks: reading thepotentiometer outputs and setting

the PWM signal to the transistor

// Function: DC_motor_control_pot

//// Use a potentiometer to control a DC motor

int sensor_pin = 3;int motor_pin = 5; // must be a PWM digital output

void setup(){

  Serial.begin(9600);  pinMode(motor_pin, OUTPUT)}

void loop(){  adjust_motor_speed( sensor_pin, motor_pin);

  ... // do other useful stuff}

Final version of the loop() function

40

Page 41: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 41/64

Desktop fan: EAS 199A

Using and Writing Functions

Additional information on the Arduino web site! http://www.arduino.cc/en/Reference/FunctionDeclaration

Functions are reusable code modules:! Functions encapsulate tasks into larger building blocks

! Functions hide details and variables local to each task 

! Well-written functions can be reused

! Functions can accept input (or not) and return output (or not)

! All Arduino sketches have at least two functions

! setup: runs once to configure the system

! loop: runs repeatedly after start-up is complete

! Users can add functions in the main sketch file, or in separate files

41

Page 42: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 42/64

Desktop fan: EAS 199A

The setup() Function

Consider the simple blink sketch

42

// Blink.pde: Turn on an LED for one second, then

// off for one second. Repeat continuously.

void setup() {

pinMode(13, OUTPUT);

}

void loop() {  digitalWrite(13, HIGH); // set the LED on

  delay(1000); // wait for a second

  digitalWrite(13, LOW); // set the LED off

  delay(1000); // wait for a second

}

No inputs

“setup” is the name of the function

“void” means Returns nothing

Page 43: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 43/64

Desktop fan: EAS 199A

A Function to Translate Linear Scales

Linear scaling from x valuesto y values:

   y = f ( x)

where f  is a linear mapping

43

 xmin

  xmax

 x

 y

 ymax

 ymin

In words: Given x, xmin, xmax, ymin, and ymax, compute y 

Page 44: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 44/64

Desktop fan: EAS 199A

A Function to Translate Linear Scales

Enter the code at the bottom into your sketch! The code is not inside any other program block(like setup or void)

How would you test that this function is working?

44

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  int y;

 

y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin);

  return(y);

}

N.B. This code is essentially a reimplementationof the built-in map function.See http://arduino.cc/en/Reference/Map

Page 45: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 45/64

Desktop fan: EAS 199A

A Function to Translate Linear Scales

45

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  int y;

 

y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin);

  return(y);

}

returns an int

name is int_scalefirst input is anint named “x”

return the value stored in “y”

Use float for better precision

Page 46: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 46/64

Desktop fan: EAS 199A

Functions are not nested

46

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  ...

}

void setup()

{

  ...

}

void loop()

{

  ...

}

// Contents of sketch, e.g. motor_control.pde

Page 47: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 47/64

Desktop fan: EAS 199A

Functions call other functions

47

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  ...

  return( y );

}

void setup()

{

  ...

}

void loop()

{

  ...

  motor_speed = int_scale( pot_val, 0, 1024, 0, 255);

}

// Contents of sketch, e.g. motor_control.pde

Page 48: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 48/64

Desktop fan: EAS 199A

Functions call other functions

48

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  ...

  return( y );

}

void setup()

{

  ...

}

void loop()

{

  ...

  motor_speed = int_scale( pot_val, 0, 1024, 0, 255);

}

// Contents of sketch, e.g. motor_control.pde

Page 49: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 49/64

Desktop fan: EAS 199A

// Function: DC_motor_control_pot//

// Use a potentiometer to control a DC motor

int sensor_pin = 3;int motor_pin = 5; // must be a PWM digital output

void setup(){  Serial.begin(9600);  pinMode(motor_pin, OUTPUT)}

void loop(){  int pot_val, motor_speed; pot_val = analogRead( sensor_pin );

  motor_speed = int_scale( pot_val, 0, 1024, 0, 255;  analogWrite( motor_pin, motor_speed);}

int int_scale(int x, int xmin, int xmax, int ymin, int ymax){  int y; y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin);

  return(y);}

Use the int_scale function

49

Page 50: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 50/64

Desktop fan: EAS 199A

A Function to update motor speedInputs

!

sensor pin! motor output pin

Tasks:! Read potentiometer voltage

! Convert voltage from 10 bit to 8 bit scales

! Change motor speed

50

void adjust_motor_speed(int sensor_pin, int motor_pin)

{

  int motor_speed, sensor_value;

 

sensor_value = analogRead(sensor_pin);

  motor_speed = int_scale(sensor_value, 0, 1024, 0, 255);

  analogWrite( motor_pin, motor_speed);

  Serial.print("Pot input, motor output = ");

  Serial.print(sensor_value);

  Serial.print(" "); Serial.println(motor_speed);

}

Page 51: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 51/64

Desktop fan: EAS 199A

Functions call functions, call functions, ...

51

int int_scale(int x, int xmin, int xmax, int ymin, int ymax)

{

  ...

  return( y );

}

void setup()

{

  ...

}

void loop()

{

  ...  adjust_motor_speed( ..., ... )

}

// Contents of sketch, e.g. motor_control.pde

void adjust_motor_speed(int sensor_pin, int motor_pin)

{

  ...

  motor_speed = int_scale( ..., ..., ..., ..., ... );

}

Page 52: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 52/64

Button Input:

On/off state changeDesktop fan projectEAS 199A, Fall 2010

Page 53: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 53/64

Desktop fan: EAS 199A

User input features of the fan

• Potentiometer for speed control! Continually variable input makes sense for speed control

! Previously discussed

• Start/stop! Could use a conventional power switch

! Push button (momentary) switch

• Lock or limit rotation angle! Button click to hold/release fan in one position

! Potentiometer to set range limit

53

Page 54: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 54/64

Desktop fan: EAS 199A

Conventional on/off switch

Basic light switch or rocker switch! Makes or breaks connection to power

! Switch stays in position: On or Off 

! Toggle position indicates the state

! NOT in the Arduino Inventors Kit

54

Image from lowes.comImage from sparkfun.com

Page 55: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 55/64

Desktop fan: EAS 199A

Momentary or push-button switches

• Temporary “click” input! Two types: normally closed or normally open

• Normally open! electrical contact is made when button is pressed

• Normally closed! electrical contact is broken when button is pressed

• Internal spring returns button to its un-pressed state

55

Image from sparkfun.comOpen Closed

Page 56: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 56/64

Desktop fan: EAS 199A

Momentary Button and LED Circuit

Digital input with a pull-down resistor ! When switch is open (button not pressed):

! Digital input pin is tied to ground

! No current flows, so there is no voltagedifference from input pin to ground

! Reading on digital input is LOW! When switch is closed (button is pressed):

! Current flows from 5V to ground, causing LEDto light up.

! The 330" resistor limits the current draw bythe input pin.

! The 10k resistor causes a large voltage dropbetween 5V and ground, which causes thedigital input pin to be closer to 5V.

! Reading on digital input is HIGH

56

LED

10 kΩ

330 Ω

5V

Digitalinput pin

Push-buttonswitch

Page 57: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 57/64

Desktop fan: EAS 199A

Programs for the LED/Button Circuit

1. Continuous monitor of button state! Program is completely occupied by monitoring the button

! Used as a demonstration — not practically useful

2. Wait for button input

! Blocks execution while waiting! May be useful as a start button

3. Interrupt Handler! Most versatile

!

Does not block execution! Interrupt is used to change a flag that indicates state

! Regular code in loop function checks the sate of the flag

All three programs use the same electrical circuit

57

Page 58: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 58/64

Desktop fan: EAS 199A

Continuous monitor of button state

58

int button_pin = 4; // pin used to read the button

void setup() {

  pinMode( button_pin, INPUT);

  Serial.begin(9600); // Button state is sent to host

}

void loop() {

  int button;

 

button = digitalRead( button_pin );

  if ( button == HIGH ) {

  Serial.println("on");

  } else {  Serial.println("off");

  }

}

Serial monitor showsa continuous stream

of “on” or “off”

This program does not control the LED

Page 59: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 59/64

Desktop fan: EAS 199A

Wait for button input

59

int button_pin = 4; // pin used to read the button

void setup() {  int start_click = LOW; // Initial state: no click yet

 

pinMode( button_pin, INPUT);

  Serial.begin(9600);

  while ( !start_click ) {

  start_click = digitalRead( button_pin );

  Serial.println("Waiting for button press");  }

}

void loop() {

  int button;

 

button = digitalRead( button_pin );

  if ( button == HIGH ) {

  Serial.println("on");

  } else {

  Serial.println("off");

  }

}

Same loop()function as

before

while loop continuesas long as start_clickis FALSE

Page 60: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 60/64

Desktop fan: EAS 199A

Interrupt handler for button input

60

int button_interrupt = 0; // Interrupt 0 is on pin 2 !!

int toggle_on = false; // Button click switches state

void setup() {

  Serial.begin(9600);

  attachInterrupt( button_interrupt, handle_click, RISING); // Register handler

}

void loop() {

  if ( toggle_on ) {

  Serial.println("on");  } else {

  Serial.println("off");

  }

}

void handle_click()

{

  static unsigned long last_interrupt_time = 0; // Zero only at start

 unsigned long interrupt_time = millis(); // Read the clock

  if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec

  toggle_on = !toggle_on;

  }

  last_interrupt_time = interrupt_time;

}

Page 61: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 61/64

Desktop fan: EAS 199A

Interrupt handler for button input

61

int button_interrupt = 0; // Interrupt 0 is on pin 2 !!

int toggle_on = false; // Button click switches state

void setup() {

  Serial.begin(9600);

  attachInterrupt( button_interrupt, handle_click, RISING); // Register handler

}

void loop() {

  if ( toggle_on ) {

  Serial.println("on");  } else {

  Serial.println("off");

  }

}

void handle_click()

{

  static unsigned long last_interrupt_time = 0; // Zero only at start

 unsigned long interrupt_time = millis(); // Read the clock

  if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec

  toggle_on = !toggle_on;

  }

  last_interrupt_time = interrupt_time;

}

Interrupt handler must be registered when program starts

The interrupt handler, handle_click,is a user-written function that is

called when an interrupt is detected

button_interrupt is the IDor number of the interrupt.

It must be 0 or 1

A RISING interrupt occurs when the

pin changes from LOW to HIGH

Page 62: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 62/64

Desktop fan: EAS 199A

Interrupt handler for button input

62

int button_interrupt = 0; // Interrupt 0 is on pin 2 !!

int toggle_on = false; // Button click switches state

void setup() {

  Serial.begin(9600);

  attachInterrupt( button_interrupt, handle_click, RISING); // Register handler

}

void loop() {

  if ( toggle_on ) {

  Serial.println("on");  } else {

  Serial.println("off");

  }

}

void handle_click()

{

  static unsigned long last_interrupt_time = 0; // Zero only at start

 unsigned long interrupt_time = millis(); // Read the clock

  if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec

  toggle_on = !toggle_on;

  }

  last_interrupt_time = interrupt_time;

}

toggle_on is a global variable that remembersthe “state”. It is either true or false (1 or 0).

The loop() function only checks the state

of toggle_on. The value of toggle_on isset in the interrupt handler, handle_click.

The value of toggle_on is flipped onlywhen a true interrupt even occurs. De-bouncing is described in the next slide.

Page 63: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 63/64

Desktop fan: EAS 199A

Interrupt handler for button input

63

int button_interrupt = 0; // Interrupt 0 is on pin 2 !!

int toggle_on = false; // Button click switches state

void setup() {

  Serial.begin(9600);

  attachInterrupt( button_interrupt, handle_click, RISING); // Register handler

}

void loop() {

  if ( toggle_on ) {

  Serial.println("on");  } else {

  Serial.println("off");

  }

}

void handle_click()

{

  static unsigned long last_interrupt_time = 0; // Zero only at start

 unsigned long interrupt_time = millis(); // Read the clock

  if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec

  toggle_on = !toggle_on;

  }

  last_interrupt_time = interrupt_time;

}

Value of a static  variable is always retained

Ignore events that occur in less than200 msec from each other. These are

likely to be mechanical bounces.

Use long : the time value in

milliseconds can become large

Clock time when current interrupt occurs

Save current time as the new “last” time

Page 64: Fan Project Complete Slides

7/23/2019 Fan Project Complete Slides

http://slidepdf.com/reader/full/fan-project-complete-slides 64/64

Other references

Ladyada tutorial! Excellent and detailed

! http://www.ladyada.net/learn/arduino/lesson5.html

Arduino reference

! Minimal explanation! http://www.arduino.cc/en/Tutorial/Button

! Using interrupts

! http://www.uchobby.com/index.php/2007/11/24/arduino-interrupts/

! http://www.arduino.cc/en/Reference/AttachInterrupt


Recommended