Hey Everyone!
I am completely new to this so please go easy on me!
I am trying to run a simple program where I can convert the Y readings on my MPU 6050 into analogue so I can adjust the brightness of the LED's depending on the Angle of the Y axis.
I seem to be getting somewhere with it but the program seems to stop and not loop like it should.
This is the code I have so far
#include <MPU6050_tockn.h>
#include <Wire.h>
//Four LED's to show MPU6050 movement
// updated so all pins are on PWM pins
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 9;
int led_value; // - Y AXIS
int led_value2; // + Y AXIS
int outputY;
int outputY2;
MPU6050 mpu6050(Wire);
void setup() {
pinMode( LED1, OUTPUT);//select pins as output
pinMode( LED2, OUTPUT);//select pins as output
pinMode( LED3, OUTPUT);//select pins as output
pinMode( LED4, OUTPUT);//select pins as output
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
//All LED's blink shows start getting data from MPU6050
digitalWrite(LED1, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
delay(1000);
}
void loop() {
mpu6050.update();
Serial.print("\tangleY : ");
Serial.print(mpu6050.getAngleY());
outputY = (mpu6050.getAngleY());
led_value = map (outputY,-1,-90, 0, 255);
outputY2 = (mpu6050.getAngleY());
led_value2 = map (outputY2,0, 90, 0, 255);
if ( ((mpu6050.getAngleY()) >=-1))
{
analogWrite(LED1, led_value);
analogWrite (LED3, led_value);
analogWrite (LED2, 0);
analogWrite (LED4, 0);
}
else ( ((mpu6050.getAngleY()) <=0));
{
analogWrite (LED2, led_value2);
analogWrite (LED4, led_value2);
analogWrite (LED1, 0);
analogWrite (LED3, 0);
return 0;
}
}
As I have mentioned before, I am new to this so apologies in advance for the shoddy layout etc. I'm sure there will be a simple fix for this but I can't seem to find it after hours of trying to figure it out!