Physical Computing

Class 6: Save the Heart

 

This time I made a small project on Pcom, called Save the heart.

The idea came from a simple idea of Cardiopulmonary Resuscitation , and that using the force sensor to detect the force range, and using different colors of lights to show the force it’s reaching and using the p5 to show the exact number, and when you reach the red light, means that you have successfully save the heart, then a song “My Heart Will Go On” will be playing.

img_5335

img_5338

P5.js Code:

https://alpha.editor.p5js.org/projects/r1g0RJE1l

Arduino Code:
#include “pitches.h”
int melody[] = {
NOTE_C4, NOTE_D4, NOTE_G3, NOTE_G4, NOTE_F4,NOTE_E4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_B3,NOTE_C4,NOTE_B3,NOTE_A3
};

int noteDurations[] = {
9,6,3,3,3,3,6,3,6,3,3,3,3,3,6,3,6
};

int sensor = A1;
void setup() {

// put your setup code here, to run once:
Serial.begin(9600);
pinMode(6,OUTPUT);
pinMode(4,OUTPUT);
pinMode(2,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
sensor = analogRead(A1);
sensor=map(sensor,0,1023,0,100);
Serial.println(sensor);

if(sensor <= 30){
digitalWrite(2,HIGH);
delay(200);
digitalWrite(2,LOW);
}
else if (sensor > 30 && sensor <90){
digitalWrite(4,HIGH);
delay(200);
digitalWrite(4,LOW);
}
else if (sensor>= 90){
digitalWrite(6,HIGH);
delay(200);
digitalWrite(6,LOW);
for (int thisNote = 0; thisNote < 16; thisNote++) {

// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 100 * noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}

}
}

Leave a Reply

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