Go Down

Topic: Hi I am trying to get four pressure sensors to trigger four different samples. (Read 451 times) previous topic - next topic

Zuna

Hi, I am trying to get four pressure sensors to trigger four different samples. At the moment the sensors all trigger the same audio file. Any idea what I could do? I am using Arduino and Processing and am totally new to both. The sensors are going to the Analog inputs and to the 5Vpin on the Arduino board.

The issue is that either they all play the same file or all the files play together. I need each sensor to play on of the files only. Thanks in advance for the help!!

Below is my code in Arduino and Processing.

Grumpy_Mike

In the processing code you have:-
Code: [Select]
if (0 < myPort.available ()) {
Which is very odd.
It is more convention to write:-
Code: [Select]
if (myPort.available () > 0) {

You are sending data from every sensor all the time. This is a bit of a waste as you should only send data when it is significant, so apply the threshold at the Arduino end. Then you only need send the sensor number and not bother with the data value.

At the processing end you just receive the data and play the file associated with that sensor number, and not bother with the threshold.

Zuna

Thank you very much for your help. I was able to send the values from my Arduino code:

ARDUINO CODE
Code: [Select]

int sensorValue1;
int sensorValue2;
int sensorValue3;
int sensorValue4;
int inputPin = A0;
int inputPin1 = A1;
int inputPin2 = A2;
int inputPin3 = A3;

void setup() {
 Serial.begin(2400);  // Start serial communication at 2400 bps
}

void loop() {
 // Read the analog input pin, and set the value in the range 0-255
 // dividing by 4
 sensorValue1 = analogRead(inputPin)/4;
 Serial.write(sensorValue1);  // Send the value
 if(sensorValue1 < 10){
 Serial.write("THRESH");}
 delay(100);  // Wait 300ms for next reading
 
   sensorValue2 = analogRead(inputPin1)/4;
 Serial.write(sensorValue2);  // Send the value
     if(sensorValue2 < 100){
 Serial.write("THRESH");}
 delay(100);  // Wait 100ms for next reading
 
    sensorValue3 = analogRead(inputPin2)/4;
 Serial.write(sensorValue3);  // Send the value
     if(sensorValue3 > 1000){
 Serial.write("THRESH");}
 delay(100);  // Wait 100ms for next reading
 
    sensorValue4 = analogRead(inputPin3)/4;
 Serial.write(sensorValue4);  // Send the value
      if(sensorValue4 > 1000){
 Serial.write("THRESH");}
 delay(100);  // Wait 100ms for next reading
};



However in my processing code I can't seem to understand how to receive the data and how to associate the sensor number to the file.
Here's my attempt:

PROCESSING CODE

Code: [Select]
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer song1;
AudioPlayer song2;
AudioPlayer song3;
AudioPlayer song4;
FFT fft;

Serial myPort;  // The serial port
int sensorValue1;
int sensorValue2;
int sensorValue3;
int sensorValue4;


void setup()
{
 size(300, 300);
 noStroke();
myPort = new Serial(this, Serial.list()[4], 2400);
 minim = new Minim(this);

 // this loads mysong.wav from the data folder
 song1 = minim.loadFile("C2C Superstion Intro.wav");
 song2 = minim.loadFile("C2C Superstion Verse.wav");
 song3 = minim.loadFile("C2C Superstion Chorus.wav");
 song4 = minim.loadFile("C2C Superstion Outro.wav");
}

void draw()
{
if (myPort.available () > 0) {
 sensorValue1 = myPort.read();
 println(sensorValue1);
}
song1.play(); {

}

if (myPort.available () > 0) {
sensorValue2 = myPort.read();
println(sensorValue2);
}
song2.play(); {

}

if (myPort.available () > 0) {
 sensorValue3 = myPort.read();
 println(sensorValue3);
}
song3.play(); {
 
}

if (myPort.available () > 0) {
 sensorValue4 = myPort.read();
 println(sensorValue4);
}
song4.play(); {

}}




Thanks a lot for the help!!!

Grumpy_Mike

Read the how to use the forum sticky and get that code posted correctly.

That is not what I told you to do at the Arduino end. You are just sending the same text message for each sensor therefore you can't tell what sensor was triggered at the processing end.

Send the single digit sensor number from the Arduino end. Then at the processing end you know what sample to trigger depending on the number you receive. You only need one serial read not four. You use an if statement on what you received to select the correct sample to play.

Zuna

Thanks and sorry for posting the code wrong.
I understand the issue and what you mean however I think my coding skills are not good enough as I am not sure how I can send just the sensor number and then receive that in processing and for the if statement as well I have researched all day and havn't found somewhere where it is explained in detail.

Nevertheless, thanks and I will keep trying different solutions and hopefully get my head around it.

Grumpy_Mike

Quote
Thanks and sorry for posting the code wrong.
So go back and modify that post.

Quote
I think my coding skills are not good enough as I am not sure how I can send just the sensor number
Replace:-
Code: [Select]
sensorValue1 = analogRead(inputPin)/4;
  Serial.write(sensorValue1);  // Send the value
  if(sensorValue1 < 10){
  Serial.write("THRESH");}
  delay(100);  // Wait 300ms for next reading

with
Code: [Select]
sensorValue1 = analogRead(inputPin)/4;
  Serial.write(sensorValue1);  // Send the value
  if(sensorValue1 < 10){
  Serial.print('1');}
 


Quote
then receive that in processing and for the if statement
Code: [Select]
if (myPort.available () > 0) {
  sensor = myPort.read();

  if(sensor=='1') song1.play();
  if(sensor=='2') song2.play();
  if(sensor=='3') song3.play();
  if(sensor=='4') song4.play();
}
 


Quote
for the if statement as well I have researched all day and havn't found somewhere where it is explained in detail.
:o

Zuna

Thanks!! On the processing end where it says:

Code: [Select]
if (myPort.available () > 0) {
  sensor = myPort.read();

  if(sensor=='1') song1.play();
  if(sensor=='2') song2.play();
  if(sensor=='3') song3.play();
  if(sensor=='4') song4.play();
}


What sensor do I equal to myPort.read? Should I list all of them?
And as well in the if statement should I name each sensor?

Zuna

Is there also anyway where I can make it so that the files can never play together, only one at a time?

Grumpy_Mike

Quote
What sensor do I equal to myPort.read?
Not at all sure what that means. That code was all you need in your draw function.

Quote
Is there also anyway where I can make it so that the files can never play together
Yes you can stop one before you start another.

The if statement in that example just does the one instruction as a conditional instruction after the the if condition.
You can do as many statements as you like if you enclose the statements in braces. You have to know the code to stop a file which I don't know off hand and you have to have a variable that holds the playing sample's name so you can stop it. Something along these lines:-
Code: [Select]

if(sensor=='1') {
// stop the sample in the variable songPlaying maybe songPlaying(stop) or something like that
song1.play();
songPlaying = song1
}

Go Up