/* ============== Altimeter ======================== * Pressure is measured with the BMP180 board. * Altitude will be displayed on the 4-digit display * * JDu - dec 2017 ==================================================== Connections for BMP180: GND - GND VCC - 3.3V (!!) SDA - ANALOG pin 4 SCL - ANALOG pin 5 Connections for 4-digit TM1637 display: GND - GND VCC - 5V CLK - pin 6 DIO - pin 8 Led voor negative values - Down-hill: Pin 2 Button for reset baseline pressure: Pin 4 ========================================================================================== */ #include // Comes with Arduino IDE #include #include #define CLK 6 // CLK pin for TM1637 display #define DIO 8 // DIO pin for TM1637 display SFE_BMP180 pressure; // You will need to create an SFE_BMP180 object, here called "pressure": TM1637Display display(CLK, DIO); bool lz = false; // Show decimal numbers with leading zeros on TM1637 display bool col = false; // Sets the colon indicator mode on or off int bn = 15; // Default brightness of the time 4-digit display double baseline; // baseline pressure int TM1637pres; // Pressure display value int TM1637alt; // Altimtude display value char status; double presMes,presSum,presAv,T,P,p0,a,t; int led = 2; // Led pin negative values (down-hill) int button = 4; // Button pin for reset Baseline int val = LOW; int count = 9; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); pinMode(button, INPUT); uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; // Settings for the 4-digit display display.setBrightness(bn); // Brightness for display display.setColon(col); // Colon indicator on/off pressure.begin(); // initialize the sensor (it is important to get calibration values stored on the device). baseline = getPressure(); // Get the baseline pressure presSum = baseline * 9; // For sum of 10 pressures to calculate the average value TM1637pres = baseline,0; resetBaseline(); // Display baseline pressure during startup } void loop(){ presMes = getPressure(); // Get a new pressure reading: presSum = presSum + presMes; // Sum of 10 pressures to calculate the average value count += 1; if (count == 10) { presAv = presSum / 10; // To calculate the average of the 10 pressures presSum = 0.0; count = 0; } if (presAv <= baseline) // Going up-hill { a = pressure.altitude(presAv,baseline); // The relative altitude difference between the new reading and the baseline reading digitalWrite(led, LOW); // Led is off when going up-hill so positive height } else // Going down-hill { a = pressure.altitude(baseline,presAv); if(a>=1)digitalWrite(led, HIGH); // Led is on when going down-hill so negative height } TM1637pres = presAv,0; TM1637alt = a,0; display.showNumberDec(TM1637alt, lz); // Display the altitude delay(200); val = digitalRead(button); if (val == HIGH) { resetBaseline(); // Reset Baseline } } void resetBaseline() { baseline = getPressure(); // Reset the baseline pressure display.showNumberDec(TM1637pres, lz); // Display the pressure on 4-dig display Serial.print("Reset Baseline pressure to "); // Display the pressure on serial Serial.print(TM1637pres); Serial.println(" hPa"); delay(2000); } double getPressure() { char status; // You must first get a temperature measurement to perform a pressure reading. // Start a temperature measurement: // If request is successful, the number of ms to wait is returned. // If request is unsuccessful, 0 is returned. status = pressure.startTemperature(); if (status != 0) { // Wait for the measurement to complete: delay(status); // Retrieve the completed temperature measurement: // Note that the measurement is stored in the variable T. // Use '&T' to provide the address of T to the function. // Function returns 1 if successful, 0 if failure. status = pressure.getTemperature(T); if (status != 0) { // Start a pressure measurement: // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait). // If request is successful, the number of ms to wait is returned. // If request is unsuccessful, 0 is returned. status = pressure.startPressure(3); if (status != 0) { // Wait for the measurement to complete: delay(status); // Retrieve the completed pressure measurement: // Note that the measurement is stored in the variable P. // Use '&P' to provide the address of P. // Note also that the function requires the previous temperature measurement (T). // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.) // Function returns 1 if successful, 0 if failure. status = pressure.getPressure(P,T); if (status != 0) { return(P); } else Serial.println("error retrieving pressure measurement\n"); } else Serial.println("error starting pressure measurement\n"); } else Serial.println("error retrieving temperature measurement\n"); } else Serial.println("error starting temperature measurement\n"); }