Go Down

Topic: Please Help(accelerometer/LED) (Read 546 times) previous topic - next topic

bailey2215

I've been working on a code for a adxl377 and one led. I want the LED to light up once the accelerometer reaches 90g's in any direction. I am very new to coding so I don't really have an idea what to do.

Beneath is the code. Feedback is very helpful. Thanks!


int scale = 3;
boolean micro_is_5V = false;
int x = 90;
boolean led = false;


void setup()
{
Serial.begin(115200);   
pinMode(LED_BUILTIN, OUTPUT);

}


void loop()
{
int rawX = analogRead(A0);   
int rawY = analogRead(A1);
int rawZ = analogRead(A2);

float scaledX, scaledY, scaledZ;
 if (micro_is_5V) // microcontroller runs off 5V
  {
    scaledX = mapf(rawX, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
    scaledY = mapf(rawY, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
    scaledZ = mapf(rawZ, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
  }
 else // microcontroller runs off 3.3V
  {
    scaledX = mapf(rawX, 0, 1023, -scale, scale);                               
    scaledY = mapf(rawY, 0, 1023, -scale, scale);
    scaledZ = mapf(rawZ, 0, 1023, -scale, scale);
  }
 
5 Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
 Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
 Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");


if (rawX > x) || (rawY > x) || (raw Z > x)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

PaulS

Is there some reason for calculating scaledX, scaledY, and scaledZ, and printing them?

Shouldn't you be checking that the absolute value of the (scaled) value is greater than 90?
The art of getting good answers lies in asking good questions.

MorganS

When you light the LED, do you want it to stay lit for the millisecond that the actual 90g condition existed? Your eye can't respond that fast. Usually you would want it to show for 500 milliseconds or more, so that it's possible to see the flash.

When the desired condition is achieved, start a timer. (If it occurs again, re-set the timer.) Then turn the LED off after the desired number of milliseconds.
"The problem is in the code you didn't post."

AWOL

Code: [Select]
5 Serial.print("X: "); Compiler problems there?

jimLee

After the LED hits 90Gs.. What tiny remaining bit of it are you going to try to find to see if it lit or not?

-jim lee
PNW Ardiuno & Maker club
1012 9Th Street, Anacortes, WA 98221 (Around the back of building)

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up