I've written this code that turns on a "song" represented in the Void scale, when the Accelerometer value is less than 520. However, I'd like the song to turn on when the Accelerometer detects changes in movement, i.e. when the Serial Monitor values are changing a certain amount, over a certain period of time. When the Accelerometer values remaining within a small Range (i.e. very little movement), I'd like the song to turn off. This is the code I've written (it's pretty basic it's my first time writing my own!). Any suggestions?
Thanks =)
int Accelerometer = A2;
int speakerPin = 5;
int sensorValue;
void setup() {
pinMode(A2, INPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
pinMode(A2, INPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A2);
Serial.println(sensorValue);
if (520 > sensorValue) {
digitalWrite(5, LOW);
delay(1000);
}
else{
digitalWrite(5, HIGH);
scale();
delay(1000);
}
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) // the sound producing function
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(5,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(5,LOW);
delayMicroseconds(delayAmount);
}
}
void scale ()
{
beep(5,330,600);
beep(5,392,500);
beep(5,659,500);
beep(5,587,1000);
beep(5,523,500);
beep(5,330,500);
beep(5,392,500);
beep(5,523,500);
beep(5,494,1000);
beep(5,349,600);
beep(5,392,500);
beep(5,698,500);
beep(5,659,500);
beep(5,587,500);
beep(5,387,500);
beep(5,523,500);
beep(5,440,500);
beep(5,392,700);
beep(5,330,600);
beep(5,392,500);
beep(5,659,500);
beep(5,587,1000);
beep(5,523,500);
beep(5,330,500);
beep(5,392,500);
beep(5,523,500);
beep(5,494,700);
beep(5,440,500);
beep(5,392,500);
beep(5,523,500);
beep(5,698,1000);
beep(5,659,500);
beep(5,523,500);
beep(5,587,500);
beep(5,523,500);
beep(5,440,700);
beep(5,494,1000);
beep(5,523,1000);
}
(code tags added by moderator)