# Led luminosity based on gyro movements

Hi to all, this is the first time I write on this forum, and I'm trying to accomplish my first real Arduino prototype; I hope to find some help; if I posted on the wrong section please move this thread.

I'm trying to control the luminosity of a led based on the movement of a gyro; here is the code I have until now; it works less or more but I think am sure I have a problem in the loop. My idea is:

when I start moving the gyro the luminosity of the LED grows (it works)
if the gyro stop moving the luminosity decrease (it works) and when it reach 0 another led (red) blinks
if you move the gyro for at least 30 seconds the led start blinking (you won the match)

the first problem I see is that if I don't move the gyro the LED starts the decrease, increase process.

Any idea?

``````int x_zero = 0;
int y_zero = 0;
int x_val = 0;
int y_val = 0;
int movement = 0;
int luminance = 0;
unsigned long int timestamp = 0;

const int LED =11;

void setup() {

pinMode (LED, OUTPUT);
Serial.begin(9600);
timestamp = millis();
}

void loop() {

if (abs(abs(x_val) - abs(y_val)) > 30) {
if  (abs(x_val) > abs(y_val))  {
movement++;
analogWrite(LED,luminance++);
}

else {
movement--;
analogWrite(LED,luminance--);

}

if (movement > 255) movement = 255;
}

else {
movement--;
analogWrite(LED,luminance--);
}

Serial.println(movement);
analogWrite(9,movement);
//analogWrite(10,movement);

//  if ((millis() - timestamp) > 500) {
//    timestamp = millis();
//    movement = movement - 10;
//    if (movement < 0) movement = 0;
//  }

if (luminance > 255) luminance = 255;

//  if ((millis() - timestamp) > 500) {
//    timestamp = millis();
//    luminance = luminance - 100;
//    if (luminance < 0) luminance = 0;
//  }

}

int val = 0;
for (int i = 0; i < 100; i++) {
``````int val = 0;