We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I am working on my arduino project and am trying to send ultrasonic sensor data and pulse sensor data to processing. I've already put sound file to processing but I want the "heartbeat.mp3" file to be played whenever pulse is detected. I tried putting 'QS == true' and other ways like that, but I still have not figured out.
Also once I get the sound file play according to the pulse I would like to control the volume of it with data received by ultrasonic sensors. Like getting louder when the distance gets closer.
I'm a total beginner and just so confused how I should put code for both programs. Help me and thanks!
My arduino code :
// Variables int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0 int blinkPin = 13; // pin to blink led at each beat int fadePin = 5; // pin to do fancy classy fading blink at each beat int fadeRate = 0; // used to fade LED on with PWM on fadePin
// Volatile Variables, used in the interrupt service routine! volatile int BPM; // int that holds raw Analog in 0. updated every 2mS volatile int Signal; // holds the incoming raw data volatile int IBI = 600; // int that holds the time interval between beats volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat". volatile boolean QS = false; // becomes true when Arduoino finds a beat.
// Regards Serial OutPut static boolean serialVisual = false; // Set to 'false' by Default. Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse
long duration, distance, RightSensor, LeftSensor;
void setup(){ pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat! pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat! Serial.begin(115200); // we agree to talk fast! interruptSetup();
Serial.begin (9600); pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT);
void loop(){
serialOutput() ;
if (QS == true){ // A Heartbeat Was Found
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
fadeRate = 255; // Makes the LED Fade Effect Happen
// Set 'fadeRate' Variable to 255 to fade LED with pulse
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
ledFadeToBeat(); // Makes the LED Fade Effect Happen delay(20); // take a break
SonarSensor(trigPin1, echoPin1); RightSensor = distance; SonarSensor(trigPin2, echoPin2); LeftSensor = distance;
Serial.print(LeftSensor); Serial.print(" - "); Serial.print(RightSensor); Serial.print(" - ");
}
void SonarSensor(int trigPin,int echoPin) { digitalWrite(trigPin, LOW); delayMicroseconds(500); digitalWrite(trigPin, HIGH); delayMicroseconds(500); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; }
void ledFadeToBeat(){ fadeRate -= 15; // set LED fade value fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative numbers! analogWrite(fadePin,fadeRate); // fade LED }
+++
My processing code :
import processing.serial.*; import processing.sound.*;
Serial myPort;
SoundFile file;
int number_of_swallowed; int Sensor; // HOLDS PULSE SENSOR DATA FROM ARDUINO int IBI; // HOLDS TIME BETWEN HEARTBEATS FROM ARDUINO int BPM; // HOLDS HEART RATE VALUE FROM ARDUINO necessary?
int trigPin1; int echoPin1; int trigPin2; int echoPin2;
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0 int blinkPin = 13; // pin to blink led at each beat int fadePin = 5; // pin to do fancy classy fading blink at each beat int fadeRate = 0; // used to fade LED on with PWM on fadePin volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat". volatile boolean QS = false; // becomes true when Arduoino finds a beat.
boolean beat = false; // set when a heart beat is detected, then cleared when the BPM graph is advanced
// SERIAL PORT STUFF TO HELP YOU FIND THE CORRECT SERIAL PORT
/* String serialPort; String[] serialPorts = new String[Serial.list().length]; boolean serialPortFound = false; */
void setup() { size(400, 200);
myPort = new Serial(this, "COM6", 9600); myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line //myPort = new Serial(this, "COM6", 9600);// //myPort.buffer(1);//?
// Load a soundfile from the /data folder of the sketch and play it back file = new SoundFile(this, "whalesound.mp3"); file.play();
if (QS == true); { file = new SoundFile(this, "heartbeat.mp3"); file.play(); } }
void draw() { background(0); textSize(50); text(number_of_swallowed +" swallowed", 50, 100); }
void mouseReleased() { if (mouseButton == LEFT) { number_of_swallowed++; } else { number_of_swallowed = 0; } }