A (barely working) CPR Simulator

This week’s project is designed for CPR training. Sometimes people are not sure how hard they should compress and this device will provide them with guidance!  The light turns on after each press on the pressure censor. Blue indicates the press is too light, green just right and red too hard.

The following is my code:

int fsrAnalogPin = A0; // FSR is connected to analog 0
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int fsrReading; // the analog reading from the FSR resistor divider
float smoothedValue = 0;

void setup(void) {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
}

void loop(void) {

fsrReading = analogRead(fsrAnalogPin);
smoothedValue += (fsrReading-smoothedValue)*0.05;
delay(1);

Serial.print(fsrReading);
Serial.print(” “);
Serial.println(smoothedValue);

if (smoothedValue < 150 && smoothedValue > 50 ) {
analogWrite(BLUE_PIN, 120);
analogWrite(GREEN_PIN, 0);
analogWrite(RED_PIN, 0);

}
else if(smoothedValue<500 && smoothedValue >=150 ){
analogWrite(BLUE_PIN, 0);
analogWrite(GREEN_PIN, 120);
analogWrite(RED_PIN, 0);

}
else if( smoothedValue >=500 ){
analogWrite(BLUE_PIN, 0);
analogWrite(GREEN_PIN, 0);
analogWrite(RED_PIN, 120);

}

else {
analogWrite(BLUE_PIN, 0);
analogWrite(GREEN_PIN, 0);
analogWrite(RED_PIN, 0);
}

}

———–MISTAKES AND CORRECTIONS——

My circuit stops working as I was trying to do some final adjustment before class. I was really frustrated because the device worked before but it does not read the analog input and display the correct colour even with the same code as before. Luckily, Aaron helped me with debugging after class.

We started by commenting out most of the code and only left a very small part that works and then we slowly built on it and make sure each small part works. This is great debugging technique.

Also, the pressure censor is very sensitive to changes in pressure and the input data fluctuates a lot.   As a result, the led changes colour too rapidly sometimes. So we added :

smoothedValue += (fsrReading-smoothedValue)*0.05;

so that the changes in data are more gradual and the transitions between different colours in the LED is a lot smoother.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *