I've made the following sketch to measure the 'Rotational Speed' with the ADXL335. I'm getting odd results. Sometime when I run the sketch it works well and the rotational speed returns 0 when stationary and slowly increases as it rotates. Other times the values are jumpy when the sensor is still. Is it a calibration problem?
Cheers
Nick
Code: Select all
int zPin = A1; //accelerometer z-axis on pin A0
int yPin = A2; //accelerometer y-axis on pin A1
int xPin = A3; //accelerometer x-axis on pin A2
int z; //z angle
int y; //y angle
int x; //x angle
int q; //delay time
float rotY; //y rotational speed
int minVal = 390;
int maxVal = 620;
#include <Adafruit_NeoPixel.h> //allows recognition of codes from the adafruit NeoPixel library
#define pix 20
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(pix, 6, NEO_GRB + NEO_KHZ800); //left
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(pix, 7, NEO_GRB + NEO_KHZ800); //right*
const int numReadings = 5;
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 averageY = 0; // the average
void setup()
{
strip2.begin();
strip2.show();// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
int yReadOld = analogRead(yPin);
// Serial.print("old y: ");
// Serial.println(yReadOld);
delay(1);
int yReadNew = analogRead(yPin);
// Serial.print("New y: ");
// Serial.println(yReadNew);
int rotY = abs(yReadNew - yReadOld);
// Serial.print("rotY=: ");
// Serial.println(rotY);
total= total - readings[index];
// read from the sensor:
readings[index] = rotY;
// 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:
averageY = total / numReadings; //average rotational speed of y
// send it to the computer as ASCII digits
Serial.println(averageY);
//delay(1); // delay in between reads for stability
if (averageY >=5&&averageY<30)
{
theaterChase(strip2.Color(255, 255, 255), 1);
}
else
theaterChase(strip2.Color(0, 0, 10), 1);
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip2.numPixels(); i++) {
strip2.setPixelColor(i, c);
strip2.show();
delay(wait);
}
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<1; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip2.numPixels(); i=i+3) {
strip2.setPixelColor(i+q, c); //turn every third pixel on
}
strip2.show();
delay(wait);
for (int i=0; i < strip2.numPixels(); i=i+3) {
strip2.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}