MPU 6050, Arduino Code Question!

Hello!
I want it to work smoothly every second without blinking.
PLease help!

The link below is the video of using Arduino.

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

int RED = 11;
int GREEN = 10;
int BLUE = 6;
int WHITE = 9;

const int rgbLedPin = 13;
const int whiteLedPin = 2;
const int batteryVoltagePin = A0;

int currentFunction = 0;
unsigned long functionStartTime = 0;
const unsigned long warmupDuration = 3600000UL; // Milliseconds, make sure to use UL for unsigned long

#define RAD_TO_DEG 180.0 / PI

void setup() {
Wire.begin();
Serial.begin(9600);
mpu.initialize();

pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(WHITE, OUTPUT);
pinMode(rgbLedPin, OUTPUT);
pinMode(whiteLedPin, OUTPUT);
pinMode(batteryVoltagePin, INPUT);
}

void loop() {
int16_t accelX, accelY, accelZ;
mpu.getAcceleration(&accelX, &accelY, &accelZ);

Serial.print("Acceleration: X = ");
Serial.print(accelX);
Serial.print(", Y = ");
Serial.print(accelY);
Serial.print(", Z = ");
Serial.println(accelZ);

int batteryValue = analogRead(batteryVoltagePin);
float voltage = batteryValue * (5.0 / 1023.0);
Serial.print("Battery Voltage: ");
Serial.println(voltage);

if (voltage < 3.6) {
analogWrite(rgbLedPin, 26);
analogWrite(RED, LOW);
analogWrite(GREEN, LOW);
analogWrite(BLUE, LOW);
digitalWrite(whiteLedPin, LOW);
Serial.println("Low battery voltage");
} else {
analogWrite(rgbLedPin, 255);
Serial.println("Normal battery voltage");
}

if (accelZ > 15000 && accelY > -2000 && accelY < 2000 && accelX > -2000 && accelX < 2000) {
// Case 1
currentFunction = 0;
} else if (accelX < -15000 && accelY > -2000 && accelY < 2000 && accelZ > -2000 && accelZ < 2000) {
// Case 2
currentFunction = 1;
} else if (accelY > 15000 && accelX > -2000 && accelX < 2000 && accelZ > -2000 && accelZ < 2000) {
// Case 3
currentFunction = 2;
} else if (accelX > 15000 && accelY > -2000 && accelY < 2000 && accelZ > -2000 && accelZ < 2000) {
// Case 4
currentFunction = 3;
} else if (accelY < -15000 && accelX > -2000 && accelX < 2000 && accelZ > -2000 && accelZ < 2000) {
// Case 5
currentFunction = 4;
} else if (accelZ < -15000 && accelX > -2000 && accelX < 2000 && accelY > -2000 && accelY < 2000) {
// Case 6
currentFunction = 5;
}

switch (currentFunction) {
case 0:
digitalWrite(rgbLedPin, LOW);
analogWrite(RED, LOW);
analogWrite(GREEN, LOW);
analogWrite(BLUE, LOW);
digitalWrite(whiteLedPin, HIGH);
Serial.println("LED OFF");
break;
case 1:
digitalWrite(rgbLedPin, HIGH);
analogWrite(RED, 0);
analogWrite(GREEN, 255);
analogWrite(BLUE, 255);
digitalWrite(whiteLedPin, HIGH);
Serial.println("Case 2: RED LED ON");
break;
case 2:
digitalWrite(rgbLedPin, HIGH);
analogWrite(RED, 255);
analogWrite(GREEN, 0);
analogWrite(BLUE, 255);
digitalWrite(whiteLedPin, HIGH);
Serial.println("Case 3: GREEN LED ON");
break;
case 3:
digitalWrite(rgbLedPin, HIGH);
analogWrite(RED, 255);
analogWrite(GREEN, 255);
analogWrite(BLUE, 0);
digitalWrite(whiteLedPin, HIGH);
Serial.println("Case 4: BLUE LED ON");
break;
case 4:
digitalWrite(rgbLedPin, HIGH);
analogWrite(RED, 0);
analogWrite(GREEN, 3);
analogWrite(BLUE, 15);
digitalWrite(whiteLedPin, LOW);
Serial.println("Case 5: White LED ON");
break;
case 5:
if (millis() - functionStartTime >= warmupDuration) {
currentFunction = 0;
digitalWrite(WHITE, LOW);
analogWrite(RED, LOW);
analogWrite(GREEN, LOW);
analogWrite(BLUE, LOW);
Serial.println("Case 6: White LED OFF");
break;
}
}

if (currentFunction != 5) {
functionStartTime = millis();
digitalWrite(WHITE, LOW);
}

delay(1000); // 1초 딜레이
}

Your code does what you have coded

  if (voltage < 3.6) {
    analogWrite(rgbLedPin, 26);
    analogWrite(RED, LOW);
    analogWrite(GREEN, LOW);
    analogWrite(BLUE, LOW);
    digitalWrite(whiteLedPin, LOW);
    Serial.println("Low battery voltage");
  } 
  else {
    analogWrite(rgbLedPin, 255);
    Serial.println("Normal battery voltage");
  }

if this is not causing the flashing you will have to describe very precise which IO-pin switches what

Just dropping the code and an external video is not enough information

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.