Im having trouble with a simple arduino code to make a tone. I am using three sensors together with the tone.library to make a tone. I was hoping that anyone could help me out. My goal is to create a tone, influenced by three sensors.
This is the setup:
Lets say we have three analog sensors.
sensorValue1 gives a value between 0-1024 (activates the tone if value > 0)
sensorValue2 gives a value between 0-1024 (sensorValue2 range from 0-1024 and defines the frequency of the note)
SensorValue3 gives a value between 0-1024 (sensorValue3 defines the duration of the note)
What I want to achieve is:
When sensorValue1 gives a value > 0. it should active a loop for an ''x'' period of time. This 'x' period is dependend from the values of sensorValue3. The frequency of the tone is dependend from sensorValue2 and is able to change during 'x' period.
the problem:
I can't change the frequency of the note after it has been activated by sensor1 and the duration of sensor3.
The tricky part here is (and here is where I am stuck) When sensorValue1 gives a value > 0, this can happen for just 1 milisecond: I want to active a note for (e.g.) 5 seconds (dependend on sensorValue3). However in this 5 seconds I want to be able to change the frequency of the note (by using sensor2).
Now, correct me if I am wrong. In order to change the note during 'x' period, the loop has to start over again? But if the loop starts over again, sensorValue1 has become 'zero' again. Therefore there is no trigger for the note.
#include <Tone.h>
Tone tone1;
void setup () {
Serial.begin(9600);
tone1.begin(13);
}
void loop() {
int piezoValue = analogRead(A0);
int softpotValue = map(analogRead(A1),0,1024,200,800);
int flexSensor = map(analogRead(A2),310,600,30,1000);
if(piezoValue > 20){
int startTime = millis();
while(millis() - startTime < 1000) {
tone1.play(softpotValue);
}
}
}
I have experienced with millis() and start time, however I can't managed to do it.
Now, correct me if I am wrong. In order to change the note during 'x' period, the loop has to start over again? But if the loop starts over again, sensorValue1 has become 'zero' again. Therefore there is no trigger for the note.
Obviously you are wrong. There is nothing in that code called sensorValue1.
the problem:
I can't change the frequency of the note after it has been activated by sensor1 and the duration of sensor3.
Of course not. You need to stop the note and start it again at the new frequency. To do that, you want to get rid of the while loop.
The loop() function is called over and over. On each pass, you need to see if it is time to stop playing the note, because the time has expired or because the frequency has changed.
If the time has expired, just stop playing the note. If the frequency has changed, stop and restart the note, but leave the time alone.
One final note. The start time should be a global variable. And, millis() doesn't return an int.
Give this code a try - not compiled or tested, but it uses other names for the variables and the datatypes are corrected e.g. Millis() does not return an int
#include <Tone.h>
Tone tone1;
void setup ()
{
Serial.begin(115200);
Serial.println("start");
tone1.begin(13);
}
void loop()
{
int duration = analogRead(A0);
int frequency = map(analogRead(A1),0,1024,200,800);
Serial.print("duration : ");
Serial.println(duration );
Serial.print("frequency : ");
Serial.println(frequency);
unsigned long now = millis();
while (millis() - now < duration)
{
tone1.play(softpotValue);
}
}