I'm trying to measure vibrations in frequency with a Wii Nunchuk accelerometer, i have found the acceleration of the the Nunchuk in the x-axis. However, converting these values to frequency have been an issue, since i do not know what type of units the Wii Nunchuk measures in. In addition, I'm trying to open a solenoid valve to allow air to inflate a balloon and stop pumping air at a certain time shortly after. I have a threshold value where this will be executed at above a certain frequency. Also, i have a darlington transistor from the Arduino to the solenoid.
/*
Created By: Alberto Lopez
Date: 03/23/14
accel sketch
simple sketch to output average values of the x-axes
detects if acceleration goes above threshold
and turns on Solenoid if it goes above threshold
To Do's
-
Convert acceleration to Frequencies of an epileptic patient.
-
Stop the balloon from inflating after a couple of seconds.
*/
const int xPin = 4; // analog input pins
const int THRESHOLD = 1000; //threshold
#define Solenoid 7 //solenoid pin
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup()
{
Serial.begin(9600); // note the higher than usual serial speed
pinMode(Solenoid, OUTPUT); //solenoid set as an ouput
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(xPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.print("X_avg = "); //print
Serial.println(average);
delay(1000); // delay in between reads for stability
if (average >= THRESHOLD) //if vibrations are greater than
{
digitalWrite(Solenoid, HIGH); //turn on solenoid
delay(100);
}
else
digitalWrite(Solenoid, LOW); //turn off solenoid
}