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 5370 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
  • phoenixcomm
    phoenixcomm over 2 years ago

     contrary, to what folks think, I NEVER USE THE VOID LOOP() Ie you don't need it. 

    try this:

    /// NO SEMI AT the end of a define
    #define GO 1
    #define FAIL -1
    
    void setup(){
    *** set up stuff only. ie address, and that ilk.
    }
    
    int main(int argc, char *argv[]) { *** No Need for void loop!
    if ( initalize() == Go) { main body of your code is hear }
    else return FAIL; 
    }
    
    int initalize(){
    abort = ;
    launch = 0; // for GO
    *** test you commponents. if any of them fail set abort = -1;
    
    return(abort);
     

    Oh yes! the first line of code should be #include <programname.h> // this is where you can hold stuff like your vars etc. This is a flat file.

    I also have a pension for getting rid of the void setup do it like this it will readability. 

    void setup() {
    #include setup.h again this is a FLAT FILE with the rest of the trash.
    }
    
    now the setup.h
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <Adafruit_BMP280.h>
    #include <Wire.h>
    #include <MPU6050.h>
    
    fins hope you enjoy. 

    • Cancel
    • Vote Up -1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm

    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.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm

    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.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Children
  • shabaz
    shabaz over 2 years ago in reply to Jan Cumps

    I've never checked, but there is a chance some compilers/linkers might even throw an error if there are variables in header files. It might work more by accident, but it can't be guaranteed, since code and variables have to be in source files for C.    one compatible approach (compatible for all compilers/linkers) is to (instead of a .h file), create a "globals.c" file, and place all the global vars in there, if that meets peoples needs. This used to be a typical way of doing things, having a single dedicated globals.c file, but I think it's fallen out of favor nowadays.

    Regarding the other point for Arduino, from my perspective I agree with Jan Cumps  that it would be better if complete beginners follow what is in the Arduino tutorials, save us having to go into multiple files when we are debugging their issues. Granted that in non-Arduino code they would typically be starting with the main(void) and organize their code in multiple files.

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

    Weird bug.. every time I type at-sign phoenixcomm, it disappears in the message above. Worked fine for at-sign jancumps. 

    image

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

    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 gremlins in the system ...

    But yes, Arduino is C-like, but does not adhere strictly to C-conventions. If you don't want to use loop, then leave it there and have it empty, can even throw all the code into setup as some people do. But that's really a style thing - most of the time functionality comes first.

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to Gough Lui

    Hi Gough,

    Interesting, it worked for shabaz .

    I think it used to work for at phoenixcomm in the past so it may be a bug. I'll try again here:

      phoenixcomm 

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

    Indeed. It worked for you, but not phoenixcomm ...

    I just tried to tag them above ... if it's blank, then it's an issue that's global.

    - Gough

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Gough Lui
    Gough Lui over 2 years ago in reply to Gough Lui

    Hey, that's strange shabaz  - I could tag pheonixcomm just fine it seems.

    Have you tried logging out/in, force-refresh?

    - Gough

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

    Unfortunately I'm currently mobile (the message 14 mins ago from me was using mobile chrome and I tried at shabaz and at phoenixcomm and they both appeared as plain text so that's a bug too). However my earlier messages were all using desktop Chrome.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to Jan Cumps

    Jan, why don't you look at the code? In the Arduino IDE loop is basically a micro that encompasses the main() IE You don't need it. and to make matters worse setup is a lousy way to initialize something? Yes you need a setup( to include the libraries, BUT that's it!) 

    Generally, after this, you need a function that tests the functionality of the unit.  So how do you know if it FAILS that right you DONT unless you TEST for it. This test should return FAIL or PASS as defined in the program-name.h file as shown below:

    #define PASS 1

    #define FAIL -1

    case in point I have an ethernet device programmed on your board now how do you test it? you can't use echo as its internal to the driver. You will need to have connected to another device and then you need two tests. 1 test for 'link' which is hardware-based, then I want to send a data packet to the other device and see if it will see the traffic. If it does it should send the packet back to your board and If you see the traffic then and only then does it pass testing. you can return other -negative values as well as error codes. Since this board is the heart of my protocol converter it has to work or you have to figure out what went wrong. ie whos on first, LOL. BTW my protocol converter changes ethernet packets to CANaerospace using 2 wires but I always have a third wire and that's the ground wire. 

    AND FOR S&Gigles: You can put vars in a header file (no rule barring it) and I don't know what compiler you're using but if you can't it's BROKE. I use GNU tools and this works fine. In my book, EVERY FILE needs a header file you define the variables. In real life, there is no difference between a .c file and a .h file.  There are some things that the standard library does not such as how do you square a number you could a*a but why a header file is the place for the same goes for a value of pi.

    • Cancel
    • Vote Up -1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm

    We seem to have different views on the language Slight smile. When the language or libs have a functionality, I prefer to use it instead of rolling my own.

    The arduino already handles the main + setup +  loop. I prefer using that, instead of overriding their main() and bypassing their setup() and loop().

    c++ has a solution for boolean, I prefer using that instead of a PASS and FAIL. I think the c++ boolean is a better solution than the custom one you wrote, and  easier to use in expressions than your +1 / -1 pair. 

    > In real life, there is no difference between a .c file and a .h file.

    That is true, but a .h is intended to be included in other files. To define interfaces, APIs, constants - likely to be reused at other places. Declaring a variable in a .h will surprise anyone that wants to use your code. 

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

    Hi Cris,

    Putting variables in header files limits including the header file to just one source file (.c). The linker will throw this error (this is with GCC):

    image

    If the variable was in a .c file, there's no problem. Every file that needs to use the global variable can simply have the extern declaration of the variable.

    I'm not saying code should use variables in this way, but there's definitely a lot more flexibility if the variables are in a .c file. With a .h file containing variables, it is not possible to include that header file anywhere, except in just one .c file. I might be missing something though, this is just at first thought.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
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