// include the library code: #include #include // initialize the library with the numbers of the interface pins LiquidCrystal lcd(7,6,5,4,3,2); long breath_rate_timer =0; long breath_duty_timer =0; int current_rate=0; int current_duty=0; int BPM_Pin = A0; // select the input pin for the potentiometer int Duty_Pin = A1; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int ValvePin = 12; int SwitchPin = 11; int BPM_Value = 0; // variable to store the value for BPM int Duty_Value =0; // variable to store the value for duty cycle boolean run_timer = 0; void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); pinMode(ValvePin, OUTPUT); pinMode(SwitchPin, INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setCursor(0,0); // Print a message to the LCD. lcd.print(" Breath-O-Matic "); Timer1.initialize(1000); // initialize timer1, and set a 1 ms period Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt } void callback() { if (run_timer == 1) { // increment current rate count and reset if equal or larger if (current_rate++ >= breath_rate_timer) { current_rate=0; current_duty = 0; digitalWrite(ledPin,1); digitalWrite(ValvePin,1); } if (current_duty++ >= breath_duty_timer) { digitalWrite(ledPin, 0); digitalWrite(ValvePin,0); } } else { current_rate=0; current_duty=0; digitalWrite(ledPin,0); digitalWrite(ValvePin,0); } } void loop() { // read the value from the sensor: BPM_Value = analogRead(BPM_Pin); BPM_Value = map(BPM_Value, 0, 1023, 1, 180); Duty_Value = analogRead(Duty_Pin); Duty_Value = map(Duty_Value, 0, 1023, 1, 99); breath_rate_timer = 60000/BPM_Value; breath_duty_timer = (breath_rate_timer*100)/(10000/Duty_Value); run_timer= digitalRead(SwitchPin); lcd.setCursor(0,1); lcd.print("BPM= "); lcd.print(BPM_Value); lcd.print(" DC= "); lcd.print(Duty_Value); lcd.print(" "); }