# Help in making a interactive lamp using an MPU6050 accelerometer/gyro

Hello!

So I’m trying to make a Lamp that turns on when it is turned upside down, and while it is upside down if turned to left on its own axis it lowers intensity and when turned to the right it increases intensity.

However I’ve been having some problems with the code, I’m using Jeff Rowberg’s library for the MPU6050 accelerometer/gryroscope, so I’m using his Readable_Quaternation to turn the lamp on when it is upside down, and Readable_Accelgryro (the Raw values of acceleration) for trying to make it dim according to the direction. I’m using Arduino Nano.

This is the part that I wrote trying to make it word properly (this is a section of the void loop, the entire code is attached on the post). I’m using a simple LED connected to digital 3 (for PWM) to test the circuit that will be applied on a LED stripe once it’s finished.

``````if(abs(q.x) >0.50 || abs(q.y) > 0.50){    //q.x and q.y are the values for the Quaternate positions
on = true;                          // I put as abs(q.x) because when the sensor completes a full turn makes the values negative
for (a=0; a<200; a++) {            //The LED (on Digital Pin 3) turns on slowly
analogWrite(3, a);

delay(10);
}

if(on == true ){

analogWrite(3,a);
while((gz)>100){          // while Z angular acceleration is positive increase the light
a++;

}
while((gz)<-50){        hile Z angular acceleration is negative decrease the light
a--;

}
}

}

else analogWrite(3, 0);             // if it is not upside down -> turn off
``````

But it didn’t. In first place I had problems with setting the delay for the LED to turn on, when I opened Serial monitor and turned it upside down the values stopped to come, and some time the “FIFO Overload!” message appeared, removing the delay dismissed the problem but the light turned on much more quickly than what I had imagined. Also when it did turn on it dimmed from 0 to 200 and when it hit 200 it came back to 0 and cycled like that. And dimming the light with the angular acceleration didn’t work at all, probably because of the problems with the first part of the code.

So I don’t know how to proceed, thus I came here to see if I can finish this project with your help.

Thanks for the help!

Lamp_Project.ino (17.1 KB)

A loop like

``````               while((gz)>100){          // while Z angular acceleration is positive increase the light
a++;

}
``````

will never stop, once gz is > 100.

Better check your conditions once every few millis, and adjust the brightness by 1 for each successful comparison.

I would use the raw accelerometer to detect right-side-up and upside-down. Just look at the sign of the Z axis.

When you say "turned to left on its own axis" did you mean a vertical axis or tilting on a horizontal axis?

For turning on a vertical axis I would use the raw rate gyroscope to detect a turn. If the rate is below a lower threshold, turn the brightness down. If it is above a higher threshold, turn the brightness up. The thresholds will be near zero.

For tilting on a horizontal axis I would use the accelerometer to detect roll. Leave a good dead band around vertical so the light won't change brightness when tilted only slightly.