Hello!
I'm getting weird data from my MPU6050, but for the first time since I connect it to power supply. Everything starts working correctly when I press reset button. Working on arduino UNO. I'm trying to use it with RF24 to send that data. Normally I should get about running average value about 8, and I do, but only if I had used reset button before.
Does anyone know what's the problem?
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
bool X_used = 0;
bool Y_used = 0;
bool Z_used = 0;
const int numReadings = 50; //liczba odczytów
int readings[numReadings]; //tablica przechowująca odczyty
int readIndex = 0; //numer aktualnego odcztu
int total = 0; //suma
int average = 0; //srednia
boolean buttonstate = 0;
boolean reading = 0;
int o = 0;
Vector normAccel;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00002
radio.openReadingPipe(1, addresses[1]); // 00001
radio.setPALevel(RF24_PA_MIN);
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_8G)); //inicjalizacja MPU6050 i czekanie na zakończenie tego procesu
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0;
//wpisanie zer do tablicy wyników
}
void loop()
{
if (buttonstate == 0){
radio.startListening();
if ( radio.available()) {
while (radio.available()) {
radio.read(&reading, sizeof(reading));
buttonstate = reading;
Serial.println("buttonstate = 1");
}
}
}
if (buttonstate == 1){
int srednia = averageZ();
Serial.println(srednia);
if(srednia > 10){
for(int i=0; i < 80; i++) srednia = averageZ(); //czekaj ale sprawdzaj srednia
if(srednia > 10){
o++;
radio.stopListening();
radio.write(&o, sizeof(o));
}
}
}
}
int averageZ()
{
if(X_used||Y_used) //czyszczenie tablicy jesli jakas srednia byla juz obliczana
{
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0;
X_used = 0;
Y_used = 0;
readIndex = 0;
total = 0;
average = 0;
}
Z_used = 1; //ustawianie zmiennej informujacej, ze w tablicy sa juz wyniki i przed obliczeniami w innej osi nalezy ja wyczyscic
Vector normAccel = mpu.readNormalizeAccel(); //odczytanie wektora z MPU6050
total = total - readings[readIndex]; //odjęcie poprzedniego pomiaru z sumy
readings[readIndex] = normAccel.ZAxis; //wpisanie na to miejsce nowego pomiaru
total = total + readings[readIndex]; //obliczenie sumy
readIndex = readIndex + 1; //inkrementacja indeksu pomiaru
if (readIndex >= numReadings) //jesli jest to koniec tablicy to ustawiamy z powrotem poczatek
{
readIndex = 0;
}
average = total / numReadings; //obliczanie sredniej
return average; //wypisywanie wyniku
}