11
Lesson 4: Breathing Monitors

Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Lesson 4: Breathing Monitors

Page 2: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Breathing Monitors

• In the past 3 classes, we’ve learned– How to write to a digital pin– How to read the value of a digital pin– How to read the value of an analog pin– IF statements

• Today we’ll put these skills together to build a medical device - a breathing monitor.

Page 3: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

New Component

Thermistor

• Variable resistor• Resistance depends on temperature

Page 4: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Set Up Your Device

Remember: The middle leg of the potentiometer goes to analog in; one outer pin goes to ground and the other to power.

Hook up thermistor and potentiometer: Hook up LED:

to digital output pin

to ground

Page 5: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

What will the program do?

• Turn an LED ON when we breathe out• Turn the LED OFF when we breathe in.

Page 6: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Your Turn

• Initialize variables: – Define digital pin for the LED– Define analog pin from the potentiometer– Define variable you will store analog value!

• Set up:– pinMode(digitalPin, OUTPUT);

• Loop: – Use analogRead– Use a conditional IF-Statement

HINT:

This is VERY similar to the analogRead program you wrote last time!

Page 7: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Add an Alarm

• Now we want a SECOND LED to turn on if there is no breathing for 10 s. – We need to add another variable for a 2nd LED– We need a way to measure the time that has passed

in the program.

• Function: millis(); – Returns the time since the program began in

milliseconds– NOT an integer! New data type ..

Page 8: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Unsigned long

• millis() returns a number of type “unsigned long”

• When you initialize the variable that will store the value from millis(), it will look like:– unsigned long start_time = 0;

• Now the variable start_time can be added/subtracted/multiplied to other variables ONLY if they are also unsigned long

Page 9: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Example Program:unsigned long time = 0; // variable to store the time. Note // the different data typeunsigned long alarm_time = 10000; // value in ms. Amount of time

// that needs to pass before // the alarm will go

void setup() {time = millis(); // initialize time }

void loop(){if ((millis() – time) > alarm_time){

//do something!}

}

KEY POINT:

((millis() – time) > alarm_time)

What does this statement say in English?

* All of the values are of type unsigned long

Page 10: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

TRANSLATE to build your final projects!There are 6 variables: an analog pin, a variable to store the analog value, a pin to make the monitor LED turn on/off, a pin to make the alarm LED turn on/off, a variable to store the time, and a variable to store the amount of time to wait until the alarm goes off!

In the setup function, you’ll need to declare that the two LED pins are outputs. You’ll also need to store the initial time.

In the loop, first you need to read the value from the sensor and store it in one of your variables. Next, check to see if 10 s have passed. If they have, turn on the alarm LED. If they have not, turn off the alarm LED. Then, check to see if the breathing sensor is above the threshold. If it is, turn on the monitor LED. ** At this point you’ll also have to reset the time. This way, you’ll always be measuring the time since the last time the a breath occurred **. Lastly, if the breathing sensor is below the threshold, turn the monitor LED off.

Page 11: Lesson 4: Breathing Monitors. Breathing Monitors In the past 3 classes, we’ve learned – How to write to a digital pin – How to read the value of a digital

Finally …

Breathing Monitors!