Mpu6050 pattern converting to code - newbie

#include <EEPROM.h>
#include <FastLED.h>
// I2Cdev and MPU6050 must be installed as libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

#define NUM_STRIPS 2
#define NUM_LEDS_PER_STRIP 15
#define BRIGHTNESS  100
#define LED_TYPE    WS2811


CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];
unsigned long actTime = 0;
unsigned long remTime = 0;
const unsigned long period = 400;
bool is_red = true;
int IF_d = 0; // dito nareregister kung naka on na ba o hindi 

#define btnCalPin   4
#define btnBTOffPin 6
#define btnBTOffSig 5
#define buzPin      10

int sampleTimes = 1000;   //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int sampleOffset = 100;
int acel_deadzone = 8;   //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone = 1;   //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

const long btSpeed = 38400;
int x =0;


MPU6050 accelgyro(0x68); // <-- use for AD0 high

typedef struct gyroval {
  int16_t ax;
  int16_t ay;
  int16_t az;
  int16_t gx;
  int16_t gy;
  int16_t gz;
  int8_t  btoff;
  
} GyroVal;

GyroVal gv;
int16_t gyValUD, gyValLR;

const int8_t OFF_d   = 0;
const int8_t LEFT_d  = 1;
const int8_t RIGHT_d = 2;

const int8_t LAST_d = 3;
const int16_t MAX_d1     = 10000;
const int16_t MAX_d2    = -10000;

const int16_t MIN_d        = -6000; //15000 default
const int16_t MAX_d        = 6000;
const int16_t MAXINTTIME_d    = 1600; //dito yung whole time ng na magaaccept ng 2x swings
const int16_t MAXSIGTIME_d = 400; //ito yung time na 1 swing lang

int8_t ledState_d = OFF_d;
int8_t  ledState = OFF_d;
int8_t  oledState = OFF_d;
int8_t  led = OFF_d;

int8_t prevSignalLR = OFF_d;
int8_t stateLR  = 0;
int8_t numSigLR = 0;

int8_t signalUD = OFF_d;
int8_t numSigUD  = 0;

unsigned long startTime = millis();
unsigned long currTime = startTime;


long lastTime = 0;
unsigned long intTime;
unsigned long sigTime;
int addr = 0;
int btnCalState = 0;

int8_t blueToothOff = 0;

int XAxGForce;

void setup()
{
  FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);
      // tell FastLED there's 60 NEOPIXEL leds on pin 10
  FastLED.addLeds<NEOPIXEL, 8>(leds[0], NUM_LEDS_PER_STRIP);

  // tell FastLED there's 60 NEOPIXEL leds on pin 11
  FastLED.addLeds<NEOPIXEL, 7>(leds[1], NUM_LEDS_PER_STRIP);

      FastLED.setBrightness(BRIGHTNESS);
      
  Wire.begin();
  // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  Serial.begin(38400);
  //initialize device
  accelgyro.initialize();
  Serial.println("\nMPU6050 Connection...");
  delay(2000);
  if (accelgyro.testConnection()) {
    Serial.println("MPU6050 connection successful");
  }
  else {
    Serial.println("MPU6050 connection failed");
    while (1) ;
  }
  delay(1000);
  EEPROM.get(0, gv);
  if (gv.ax != 65535) accelgyro.setXAccelOffset(gv.ax);
  else accelgyro.setXAccelOffset(-10);
  if (gv.ay != 65535) accelgyro.setYAccelOffset(gv.ay);
  else accelgyro.setYAccelOffset(-1);
  if (gv.az != 65535) accelgyro.setZAccelOffset(gv.az);
  else accelgyro.setZAccelOffset(16385);
  if (gv.gx != 65535) accelgyro.setXGyroOffset(gv.gx);
  else accelgyro.setXGyroOffset(0);
  if (gv.gy != 65535) accelgyro.setYGyroOffset(gv.gy);
  else accelgyro.setYGyroOffset(0);
  if (gv.gz != 65535) accelgyro.setZGyroOffset(gv.gz);
  else accelgyro.setZGyroOffset(1);
  if (gv.btoff != 255) {
    digitalWrite(btnBTOffSig, HIGH);
    blueToothOff = 1;
    delay(1000);
  }
  else {
    digitalWrite(btnBTOffSig, LOW);
    blueToothOff = 0;
    delay(1000);
    Serial1.begin(btSpeed);
  }
  delay(1000);
  Serial.println("Start reading sensor...");
}

void loop() {
   actTime = millis();
  currTime = millis();
 

  if (btnCalState == HIGH) {
    tone(buzPin, 2500);
    delay (500);
    noTone(buzPin);
    delay (200);
    tone(buzPin, 2500);
    delay (500);
    noTone(buzPin);
    delay (2000);

    calibration();

    for (int i = 0; i < 3; i++) {
      tone(buzPin, 2500);
      delay (200);
      noTone(buzPin);
      delay (200);
    }
    tone(buzPin, 2500);
    delay (500);
    noTone(buzPin);
    
    delay(5000);
  }

  accelgyro.getMotion6(&gv.ax, &gv.ay, &gv.az, &gv.gx, &gv.gy, &gv.gz);
  //Serial.print(gv.ax, DEC);
  //Serial.print("\t");
 Serial.print(currTime, DEC);
  Serial.print("\t");
  Serial.print(gv.gy, DEC);
 Serial.print("\t");
  Serial.println(gv.gz, DEC);
  Serial.print("\t");
   
  
  if (blueToothOff == 0) {
    //Serial1.print(gv.ax, DEC);
   // Serial1.print("\t");
    //Serial1.print(currTime, DEC);
    //Serial1.print("\t");
   // Serial1.print(gv.gx, DEC);
    //Serial1.print("\t");
    Serial1.print(gv.gy, DEC);
    Serial1.print("\t");
    Serial1.print(gv.gz, DEC);
    Serial1.println("#");
    Serial1.println(XAxGForce, DEC);
    
    
    

  }
  gyValUD  = gv.gy;
  gyValLR = gv.gz;
  
    
  if( gyValLR >= MAX_d ){
        if( gyValLR <= MIN_d ){
            if( gyValLR >= MAX_d ){
                if( gyValLR >= MAX_d ){
                  ledState = LEFT_d;
                  osignal();
                
                }
            }
       }
    }
  
    else if( gyValLR <= MIN_d ){
      ledState = RIGHT_d;
      osignal();
      
     
    }
}

Hi Guys, I need your help to approach this type of coding. Im using a mpu6050 and using the gyroscope value, I wanted to create a code that will read this pattern in the plotter, please see image above. First If the program first detected -6000 then 6000 then -6000 then 6000 this will on the left led. And if the detected first the 6000 then -6000 then 6000 then -6000 it will on the right led.
Thank you for your suggestions

so you have alternating values (which seems weird from a MPU)

+6000…-6000…+6000…-6000…+6000…-6000…+6000…-6000…

Your need seems flawed as it depends when you start measuring with your arduino. if you started measuring at a +6000 you’ll have the right led on, but if you booted up your arduino at -6000 you’ll have the left LED on… but in both cases the signal was really the same

what’s the real need?

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