Hello everybody,
I'm an industrial designer and I'm new to Arduino.
I'm working on a project for cyclists that uses an accelerometer (ADXL335) in order to control some LEDs used to signal when the the bike is braking.
Basically I calibrated the accelerometer in order to measure acceleration/deceleration in +/- values, and then I set up a value "20" that is like the threshold.
Then I wrote a code, that should work like that: "if you feel the bike is decelerating (analogRead > 20) then switch on the LEDs for 2 seconds, else LEDs off".
I have two problems: the first one is that everytime I switch on the prototype (or I move it for a while) it seem that I'm able to get different values from the accelerometer that don't match with my calibration anymore. I'm not able to understand what is the correct value for deceleration.
The second one is that the code is not working (the part "if you read this value then do that action") I must have messed up with the code but I'm not able to understand what.
What could be the problem and how can I solve those issues?
Thank you
Here's the code:
const int Z = A4;
const int LEDstop = 9;
long lastDebounceDx = 0;
long DebounceDelayDx = 20;
long previousMillisdx = 0;
long previoustimerdx = 0;
long intervaltimerdx = 2000;
int ledStatedx = HIGH;
int statedx = 0;
void loop()
{
int readingAcc = analogRead(Z);
int zeroG = 515;
if (((readingAcc - zeroG) < 20) && ((millis() - lastDebounceDx) > DebounceDelayDx))) {
statedx = 1 - statedx;
lastDebounceDx = millis();
previoustimerdx = millis();
}
if (statedx == 1) {
long timerdx = millis();
digitalWrite(LEDstop, ledStatedx);
}
if(timerdx - previoustimerdx > intervaltimerdx) {
digitalWrite(LEDstop, LOW);
oldvaldx = LOW;
}
}
else {digitalWrite(LEDstop, LOW);
}
}