110 likes | 235 Views
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 pin How to read the value of an analog pin IF statements
E N D
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.
New Component • Thermistor • Variable resistor • Resistance depends on temperature
Set Up Your Device Hook up LED: Hook up thermistor and potentiometer: to ground to digital output pin Remember: The middle leg of the potentiometer goes to analog in; one outer pin goes to ground and the other to power.
What will the program do? • Turn an LED ON when we breathe out • Turn the LED OFF when we breathe in.
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!
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 ..
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 longstart_time = 0; • Now the variable start_time can be added/subtracted/multiplied to other variables ONLY if they are also unsigned long
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
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.
Finally … Breathing Monitors!