Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lesson 4: Breathing Monitors

2 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.

3 New Component Thermistor Variable resistor Resistance depends on temperature

4 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

5 What will the program do? Turn an LED ON when we breathe out Turn the LED OFF when we breathe in.

6 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!

7 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 2 nd 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..

8 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

9 Example Program: unsigned long time = 0; // variable to store the time. Note // the different data type unsigned 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 KEY POINT: ((millis() – time) > alarm_time) What does this statement say in English? * All of the values are of type unsigned long

10 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.

11 Finally … Breathing Monitors!


Download ppt "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."

Similar presentations


Ads by Google