Code writing problem

how do i write the code, i want to tell arduino if the value of acc(measured from mpu6050) after 1 sec is less than the value now then go in a specific mode pls help me out

The Arduino will need to be program to convert voice commands into computer code. Voice recognition on an Arduino will be difficult. For voice commands I'd use TensorFlow and tap into the TensorFLow repositories for voice command Tensors or make your own Tensor. With a Tensor set you can then tell the Arduino things. Arduino makes 32 bit MCU's and the ESP32 can do TensorFLow by using the TensorFlow Lite library.

1 Like

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please tell us your electronics, programming, arduino, hardware experience?

Is this a school/college/university project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:
PS to forum members. Is it me or is there an increasing number of threads bout the MPU6050 in the past week or so?

I am making a rocket flight computer and for detecting apogee I want to code that if acc after 1 sec is less than last measured acceleration then it is apogee for that I don't underdtant what statement should I use after if(??) Pls reply

Start with a Google search
"model rocket detecting apogee mpu6050 arduino"

the more information, the less speculation.

since you posted no code, we can only make wild guesses.
here are some ideas. not real code. but it gives you some sort of idea of sequences.

apogee = /* get reading from device /
acceleratonNOW = /
get reading from device*/
if ( timerTHENflag = 0 ) {
if ( accelerationNOW < accelerationTHEN) {
timerTHEN = millis();
timerTHENflag = 1; // locks out changing after the timer started
}
}

if ( timerTHENflag = 1 ) {
if (millis()-timer >=1000) {
apogee = apogeeMAX ;
}
}

// all the way at the bottom, before end loop
accelerationTHEN = accelerationNOW; // saves last accleration
} // ========== END LOOP =====

1 Like

here is the code:

#include <SD.h>
#include <SPI.h>
#include <Wire.h>



int buzzer = 2;
int redl = 3;
int greenl = 4;
int bluel = 5;
File myFile;
int pinCS=53;



long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;

long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;

void setup(){
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
pinMode(redl, OUTPUT);
pinMode(greenl, OUTPUT);
pinMode(bluel, OUTPUT);
pinMode(buzzer,OUTPUT);
startup();
if(gForceZ>=8.6) {
      Ascending();
    }
    else{
      pad();
    }


}

void loop()
{
    }

void gyro()
{
    Wire.begin();
    setupMPU();
    recordAccelRegisters();
    recordGyroRegisters();
    printData();
    delay(10);
}

void setupMPU()
{
    Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
    Wire.write(0x6B);                  //Accessing the register 6B - Power Management (Sec. 4.28)
    Wire.write(0b00000000);            //Setting SLEEP register to 0. (Required; see Note on p. 9)
    Wire.endTransmission();
    Wire.beginTransmission(0b1101000); //I2C address of the MPU
    Wire.write(0x1B);                  //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
    Wire.write(0x00000000);            //Setting the gyro to full scale +/- 250deg./s
    Wire.endTransmission();
    Wire.beginTransmission(0b1101000); //I2C address of the MPU
    Wire.write(0x1C);                  //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
    Wire.write(0b00000000);            //Setting the accel to +/- 2g
    Wire.endTransmission();
}

void recordAccelRegisters()
{
    Wire.beginTransmission(0b1101000); //I2C address of the MPU
    Wire.write(0x3B);                  //Starting register for Accel Readings
    Wire.endTransmission();
    Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
    while (Wire.available() < 6)
        ;
    accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
    accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
    accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
    processAccelData();
}

void processAccelData()
{
    gForceX = accelX*9.8 / 16384.0;
    gForceY = accelY*9.8 / 16384.0;
    gForceZ = accelZ*9.8 / 16384.0;
}

void recordGyroRegisters()
{
    Wire.beginTransmission(0b1101000); //I2C address of the MPU
    Wire.write(0x43);                  //Starting register for Gyro Readings
    Wire.endTransmission();
    Wire.requestFrom(0b1101000, 6); //Request Gyro Registers (43 - 48)
    while (Wire.available() < 6)
        ;
    gyroX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
    gyroY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
    gyroZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
    processGyroData();
}

void processGyroData()
{
    rotX = gyroX / 131.0;
    rotY = gyroY / 131.0;
    rotZ = gyroZ / 131.0;
}

void printData()
{
    Serial.print("Gyro (deg)");
    Serial.print(" X=");
    Serial.print(rotX);
    Serial.print(" Y=");
    Serial.print(rotY);
    Serial.print(" Z=");
    Serial.print(rotZ);
    Serial.print(" Accel (m/s^2)");
    Serial.print(" X=");
    Serial.print(gForceX);
    Serial.print(" Y=");
    Serial.print(gForceY);
    Serial.print(" Z=");
    Serial.println(gForceZ);
    
}

void sdcard()
{
    if(SD.begin())
{
  Serial.println("SD CARD READY");
}else{
  Serial.println("SD card failed");
  return;
}
 myFile = SD.open("new-6.txt", FILE_WRITE);

 if(myFile) {
   myFile.print("Gyro (deg)");
    myFile.print(" X=");
    myFile.print(rotX);
    myFile.print(" Y=");
    myFile.print(rotY);
    myFile.print(" Z=");
    myFile.print(rotZ);
    myFile.print(" Accel (g)");
    myFile.print(" X=");
    myFile.print(gForceX);
    myFile.print(" Y=");
    myFile.print(gForceY);
    myFile.print(" Z=");
    myFile.println(gForceZ);
    myFile.println(millis()/1000);
    myFile.close();
    Serial.println("DONE.");
  
 }
else{
  Serial.println("error");
}
}

void startup(){
  Serial.println("hey i am ursa flight computer");
  delay(1000);
  Serial.println("lets check if everything is working well......");
  delay(1000);
  Serial.println("checking accelerometer and gyroscopes");
  delay(1000);
  gyro();
  delay(1000);
  Serial.println("cool");
  delay(1000);
  Serial.println("lets see if sd card is there");
  delay(1000);
   if(SD.begin())
{
  Serial.println("SD CARD is READY to go!");
}else{
  Serial.println("INSERT SD card");
}
delay(1000);
Serial.println("checking leds and buzzers");
digitalWrite(redl,HIGH);
digitalWrite(buzzer,HIGH);
digitalWrite(greenl,HIGH);
delay(1000);
digitalWrite(redl,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(greenl,0);
Serial.println("everything is go for launch!");
delay(1000);
Serial.println("starting launch pad mode..");
delay(100);

}

void Ascending(){
  gyro();
  sdcard();
  digitalWrite(redl,1);
  if( // i dont understand what to write// ){
    Descending();
    
  }

  
  
}

void Descending(){
  digitalWrite(bluel,1);
  delay(4000);
  return 0;
}

void pad()
{
  digitalWrite(greenl,1);
}

hey, thanks but i dont understand what is timerTHENflag

we live in a continuous series of now.
when you say now that is one thing, but now, you said it way back then. so you have now and you have then,

When you try to measure a minute, you need a now and a then.
the difference between then and now is duration.

As flag tells us something.
If the motor is spinning, you don't need to start it.

the problem it solves is
if the nowAcceleration is slower than the thenAcceleration

so, if nowAcceleration is slower than thenAcceleration
start the timer.

so, as long at now is slower than then...
you start the timer at 0, over and over, about 1 million times a second.

so, if it already started never try to re-start it.

if (timer is not started) // aka timerTHENflag = 0
and if ( thenAcceleration <= nowAcceleration)
start the timer
set a flag to not start the timer again // aka timerTHENflag = 1

oh, and this bit
timerTHENflag = 0 goes in setup.
so it can recycle on power recycle.

Thanks a lot!

The acceleration will become negative as soon as the rocket motor stops producing thrust on the way up. I think you need to use something to measure altitude and not just acceleration.

Actually not exactly apogee i want to measure if its descending to deploy shoots btw thnks for replying

You can just buy a rocket altimeter, unless this is a class project. I have flown many a high power rocket using them.

Statements like "for that I don't underdtant what statement should I use after if(??) Pls reply" doesn't give a lot of confidence you can design and code a reliable apogee detector. I'm not being condescending but there are safety concerns here. The last thing you want is a big rocket coming in ballistic due to a deployment failure.

I think it will be hard to measure that with an accelerometer. As soon as the motor burns out the rocket will be in free-fall except for air resistance. Acceleration will be fairly low from burn-out to landing. Detecting when the rocket has coasted to a stop in the air will be tricky.

Hi,
Have you Googled;

arduino rocket altimeter

Tom... :smiley: :+1: :coffee: :australia:

actually i am flying a small rocket and its tvc controlled so i need to built flight computer and altimeter for controlling

yes, but trying is worth it and i am following bps.space and he actually explained this method to detect descending and deploying shoots.

Hi,

Good, you will find most of his stuff is open source, have you looked at it.

Tom... :smiley: :+1: :coffee: :australia:

yes i have looked at it and i am doing avionics on my own as its not open source,thnks for replying

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