I'm trying to write code for a school project, and we are trying to make a circuit that detects acceleration from an accelerometer, and if the acceleration is large enough, triggers blinking LEDs. I was just wondering if the code I have been working on would suffice or if I need to add anything else, thanks in advance for any help!
int x = 0;
int y = 0;
int z = 0;
int gforce = 0;
int redLed;
int greenLed;
int blueLed;
int xpin;
int ypin;
int zpin;
int xval;
int yval;
int zval;
// declare everything else here (make sure to assign each to a pin)
void setup() {
Serial.begin(9600);
pinMode(redLed,OUTPUT);
pinMode(blueLed,OUTPUT);
pinMode(greenLed,OUTPUT);
}
void alertBlinking(){
digitalWrite(redLed,HIGH);
delay(50);
digitalWrite(redLed,LOW);
delay(100);
digitalWrite(blueLed,HIGH);
delay(50);
digitalWrite(blueLed,LOW);
delay(100);
}
void loop() {
xval = analogRead(xpin);
yval = analogRead(ypin);
zval = analogRead(zpin);
x = sq(xval);
y = sq(yval);
z = sq(zval);
gforce = sqrt(x + y + z);
Serial.println(gforce);
if (gforce > 2) {
alertBlinking();
}
delay(1000000);
}