Go Down

Topic: TRYING TO MEASURE CHANGES IN MOVEMENT USING ACCELEROMETER (Read 2616 times) previous topic - next topic

EnricaF

Aug 04, 2014, 06:22 pm Last Edit: Aug 04, 2014, 06:46 pm by CrossRoads Reason: 1
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 =)

Code: [Select]

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)

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Add some code to note the time when the change you want is noticed, and keep checking the level as you watch some time go by. When enough time has passed, call the song to play.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

EnricaF

The change I want the song to respond to is a large change in values (read by the accelerometer), thus indicating motion. Therefore rather then reading:
if 520 > sensorValue (for example)
digitalWrite (5, HIGH)

The code would be something like:

if (changes in value >20) > sensorValue
digitalWrite (5, HIGH)

I'm just not sure what this code should look like.
I anticipate that it won't be as simple as this, (that I'll have to potentially use some sort of Boolean switch?) but once again, I just don't know what it will have to look like.


ScottC

Have a look at my accelerometer project here:
http://arduinobasics.blogspot.com.au/2012/08/arduino-basics-3-axis-accelerometer.html

It might help you in your endeavors.
My project uses an accelerometer to detect movement - and will buzz when movement is detected.

You could adapt my code to suit your needs, i.e to play a song instead of buzzing a buzzer.

Gaur

try error values
cheak out this
http://crackeconcept.blogspot.in/2014/11/using-accelerometer-with-ardiuno-with.html

Go Up