element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Model Rocket Flight Computer Altitude / Speed update
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 25 replies
  • Subscribers 387 subscribers
  • Views 5369 views
  • Users 0 members are here
  • altitude
  • mpu6050
  • bmp280
  • speed-measuring
  • I2C OLED Display
Related

Model Rocket Flight Computer Altitude / Speed update

Frausing
Frausing over 2 years ago

Hi all, I am new to Arduino

I am trying to make an Model Rocket Flight Computer

I Use an Arduino Nano, an BMP280 and MPU6050 to measure Altitude and Speed
It show it on an small I2C OLED Display

It should show the live data on the display for the first 10 minutes
And after 10 minutes it then should show the Max Altitude and the Max Speed.

I can't get the Altitude and Speed to update on the display, it just keep saying 0.00
and of course after the 10 min. it still say 0.00

BMP280, MPU6050, OLED Display
Is conected to:
VCC to 5V on Arduino Nano
GND to GND
SDA to A4
SCL to A5

The LED is on Pin 13

I hope that someone can help me 

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP280.h>
#include <Wire.h>
#include <MPU6050.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

Adafruit_BMP280 bmp;
MPU6050 mpu;

const int ledPin = 13;
float altitude;
float speed;
int maxAltitude = 0;
int maxSpeed = 0;
unsigned long startTime = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  bmp.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, HIGH);
  startTime = millis();
}

void loop() {
  unsigned long elapsedTime = millis() - startTime;
  int secondsElapsed = elapsedTime / 1000;

  if (secondsElapsed < 10) { // Display startup message for 10 seconds
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("0RocketFlightComputer");
    display.display();
  }
  else if (secondsElapsed < 600) { // Display current altitude and speed for first 10 minutes
    int16_t accelY = mpu.getAccelerationY();
    float accel = accelY / 16384.0 * 9.81; // convert to m/s^2
    altitude = bmp.readAltitude(1013.25);
    speed = accel * altitude;

    if (isnan(altitude) || isinf(altitude)) {
      altitude = 0;
    }

    if (isnan(speed) || isinf(speed)) {
      speed = 0;
    }

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("RocketFlightComputer1");
    display.setCursor(0,10);
    display.print("Altitude: " + String(altitude, 2));
    // display.println(altitude, 2);
    display.setCursor(0,20);
    display.print("Speed: " + String(speed, 2));
    // display.println(speed, 2);
    display.display();
  }
  else { // Display maximum altitude and speed after 10 minutes
    if (altitude > maxAltitude) {
      maxAltitude = altitude;
    }
    if (speed > maxSpeed) {
      maxSpeed = speed;
    }
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("RocketFlightComputer2");
    display.setCursor(0,10);
    display.println("Max Altitude: " + String(maxAltitude) + " m");
    display.setCursor(0,20);
    display.println("Max Speed: " + String(maxSpeed) + " m/s");
    display.display();
  }
  delay(100);
}


Test setup

image
image

  • Sign in to reply
  • Cancel

Top Replies

  • Gough Lui
    Gough Lui over 2 years ago +4
    Your code never checks if the sensors were initialised correctly anyway - check return values for mpu.initialize() and bmp.begin(). I suspect you have issues with your I2C connectivity - run a I2C scanner…
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm +3
    Vars don't belong in a header file. I advise arduino users to stick to the setup() and loop() mechanisms. It works well and it 'll be easier to get assistance when you use a common approach.
  • Gough Lui
    Gough Lui over 2 years ago in reply to shabaz +2
    I wonder if it's because people who have not configured a name (rather than having just username) get blanked? shabaz - I'll test it with your name and see what happens. Or perhaps there's some more…
Parents
  • shabaz
    shabaz over 2 years ago

    Hi,

    The code isn't readable. It is possible to use the Insert->Code menu option in the blog post to correct the formatting.

    To save a bit of time for anyone wishing to read the code:

    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <Adafruit_BMP280.h>
    #include <Wire.h>
    #include <MPU6050.h>
    
    #define OLED_RESET 4
    Adafruit_SSD1306 display(OLED_RESET);
    
    Adafruit_BMP280 bmp;
    MPU6050 mpu;
    
    const int ledPin = 13;
    float altitude;
    float speed;
    int maxAltitude = 0;
    int maxSpeed = 0;
    unsigned long startTime = 0;
    
    void setup()
    {
        Serial.begin(9600);
        Wire.begin();
        mpu.initialize();
        bmp.begin();
        display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
        display.clearDisplay();
        pinMode(ledPin, OUTPUT);
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, HIGH);
        startTime = millis();
    }
    
    void loop()
    {
        unsigned long elapsedTime = millis() - startTime;
        int secondsElapsed = elapsedTime / 1000;
    
        if (secondsElapsed < 10)
        { // Display startup message for 10 seconds
            display.clearDisplay();
            display.setCursor(0, 0);
            display.println("0RocketFlightComputer");
            display.display();
        }
        else if (secondsElapsed < 600)
        { // Display current altitude and speed for first 10 minutes
            int16_t accelY = mpu.getAccelerationY();
            float accel = accelY / 16384.0 * 9.81; // convert to m/s^2
            altitude = bmp.readAltitude(1013.25);
            speed = accel * altitude;
    
            if (isnan(altitude) || isinf(altitude))
            {
                altitude = 0;
            }
    
            if (isnan(speed) || isinf(speed))
            {
                speed = 0;
            }
    
            display.clearDisplay();
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0, 0);
            display.println("RocketFlightComputer1");
            display.setCursor(0, 10);
            display.print("Altitude: " + String(altitude, 2));
            // display.println(altitude, 2);
            display.setCursor(0, 20);
            display.print("Speed: " + String(speed, 2));
            // display.println(speed, 2);
            display.display();
        }
        else
        { // Display maximum altitude and speed after 10 minutes
            if (altitude > maxAltitude)
            {
                maxAltitude = altitude;
            }
            if (speed > maxSpeed)
            {
                maxSpeed = speed;
            }
            display.clearDisplay();
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0, 0);
            display.println("RocketFlightComputer2");
            display.setCursor(0, 10);
            display.println("Max Altitude: " + String(maxAltitude) + " m");
            display.setCursor(0, 20);
            display.println("Max Speed: " + String(maxSpeed) + " m/s");
            display.display();
        }
        delay(100);
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Frausing
    Frausing over 2 years ago in reply to shabaz

    Thank you so much :-)
    I have corrected my post

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Frausing
    Frausing over 2 years ago in reply to shabaz

    Thank you so much :-)
    I have corrected my post

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube