# Force-Sensitive-Sensors and Piezo to create sounds

Hey guys, just have a short question regarding writing code. I want to build a circuit in which two force-sensitive-sensors generate sound via a piezo, while being pushed.

Now I thought about different states of the piezo.

State 1: No sensor is pushed, piezo produces one tone, delay, same tone again, delay and so on

State 2: sensor1 is pushed: the delay in state 1 gets smaller - is there a quicker way of doing that, instead of writing the same code (e.g. delay(5000), tone(...), delay(4000), tone(...)) ?

State 3: if sensor2 is pushed, I want to create an arpeggio sound, which goes on repeatedly.

State 4: If both sensors are being pushed, I want to create the arpeggio sound and want to speed it up.

Basically I guess I know what I have to do, but I am not sure about the "speed-up-the-tones-thing".. So if there is any hack here, to get this speed up pretty fast it would be nice to know :)

Another question I have:

Those force-sensitive-sensors have different states of resistance, depending on how much I push them. Those signals have to be analog inputs (right?), therefore I have to use digitalread-functions, to get the different values (right?). So if I understand it correctly, I can programm different functions, to different values. Or can declare certain "value-limits" for example every resistance-value below 15 triggers sth. and so on ?

Thanks in advance, if you have any further questions please let me know!

Cheers,

Simon

Those signals have to be analog inputs (right?), therefore I have to use digitalread-functions, to get the different values (right?).

If the sensors produce an analogue output and you want to do different things based on the value then you must use analogRead() rather than digitalRead(). analogRead() will give you a value between 0 and 1023 depending on the output from the sensor and you can map() that to a different range if that is what you need. digitalRead() will give you HIGH or LOW depending on whether the value is below or above the threshold.

I already worked on the code and the circuit, but as of now I am not receiving any values while pressing the sensor. The serial monitor shows nothing …

This is the code:

int PressurePin = A0;
int piezoPin = 12;
int length = 15;
char notes = "ccggaagffeeddc ";
int beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 200;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(piezoPin, HIGH);
delayMicroseconds(tone);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’ };
int tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_
}_
_
}_
_
}_
void setup() {
_
pinMode(piezoPin, OUTPUT);_
_
for (int i = 0; i < length; i++) { //plays activation sound*_
_ if (notes == ’ ') {
delay(beats * tempo); // rest
* } else {_
playNote(notes, beats _ tempo);
* }
// pause between notes*

* delay(tempo / 2);
Serial.begin(9600);
pinMode(PressurePin,INPUT);
}}
void loop()
{
Serial.println(PressurePin);
if (PressurePin > 15)
{
tone (12, 800, 200);
delay (200);
tone (12, 1200, 200);
delay (200);
tone (12, 800, 200);
delay (200);
tone (12, 1200, 200);
delay (200);
}
else {
noTone(12);
}
}*

So after starting my Arduino, It makes an “Activation sound”, then it should make no sound and after I push the sensor some tones should start playing.
But nothing happens, the Arduino just keeps skipping my “if-function” and proceeds playing the tones without me pushing the sensor… I have no idea where my mistake is…
Maybe anyone got an idea …
cheers,
Simon_

``````int PressurePin = A0;
int piezoPin = 12;
int length = 15;
char notes[] = "ccggaagffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 200;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(piezoPin, HIGH);
delayMicroseconds(tone);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(piezoPin, OUTPUT);
for (int i = 0; i < length; i++) { //plays activation sound
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

// pause between notes
delay(tempo / 2);
Serial.begin(9600);
pinMode(PressurePin,INPUT);
}}

void loop()
{
Serial.println(PressurePin);

if (PressurePin > 15)
{
tone (12, 800, 200);
delay (200);
tone (12, 1200, 200);
delay (200);
tone (12, 800, 200);
delay (200);
tone (12, 1200, 200);
delay (200);

}

else {

noTone(12);
}
}
``````

the Arduino just keeps skipping my "if-function"

In my experience when if statements do not do what you want them to do the first thing to do is to print the values being tested to see whether they are what you expect.