pleasedontcode

BMX280 Readings rev_01

Oct 12th, 2025
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: BMX280 Readings
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-12 12:59:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 1) Use D2 with INPUT_PULLUP and EasyButton to emit */
  21.     /* a debounced pressed event to Serial. 2) On press, */
  22.     /* read BMX280 via forcedBMX280 and print */
  23.     /* pressure/temp. 3) Rely only on */
  24.     /* button1_PushButton_PIN_D2; no extra pins/libs */
  25.     /* beyond those listed. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EasyButton.h>
  33. #include <forcedBMX280.h>   // https://github.com/soylentOrange/Forced-BMX280
  34. #include <Wire.h>
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void onButtonPressed(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t button1_PushButton_PIN_D2 = 2; // D2
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45. ForcedBMX280 climateSensor = ForcedBMX280();
  46. EasyButton button1 = EasyButton(button1_PushButton_PIN_D2);
  47.  
  48. void setup(void)
  49. {
  50.     // Initialize serial port for debugging and output
  51.     Serial.begin(115200);
  52.     while (!Serial) {
  53.         ; // wait for serial port to connect. Needed for native USB devices
  54.     }
  55.  
  56.     // Initialize I2C bus
  57.     Wire.begin();
  58.  
  59.     // Initialize BMX280 sensor in forced mode (no continuous sampling)
  60.     climateSensor.begin();
  61.  
  62.     // Initialize button with debounced callback on D2 (INPUT_PULLUP by EasyButton)
  63.     button1.begin();
  64.     button1.onPressed(onButtonPressed);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // Update button state to generate debounced events
  70.     button1.update();
  71. }
  72.  
  73. // Debounced button press handler
  74. void onButtonPressed(void)
  75. {
  76.     Serial.println("Button pressed");
  77.     // Take a forced measurement to update data
  78.     climateSensor.takeForcedMeasurement();
  79.  
  80.     // Read temperature in hundredths of degrees Celsius
  81.     int32_t temperatureC100 = climateSensor.getTemperatureCelsius(true);
  82.     Serial.print("Temperature: ");
  83.     Serial.print(temperatureC100/100);
  84.     Serial.print(".");
  85.     int32_t frac = temperatureC100 % 100;
  86.     if (frac < 0) frac = -frac;
  87.     if (frac < 10) Serial.print("0");
  88.     Serial.println(frac);
  89.     Serial.println(" C");
  90.  
  91.     // Read pressure (hPa with two decimals). If not available on the used class, this call
  92.     // will either be ignored by the library or may return a valid value depending on the build.
  93.     uint32_t pressurePa100 = climateSensor.getPressure(true);
  94.     Serial.print("Pressure: ");
  95.     Serial.print(pressurePa100/100);
  96.     Serial.print(".");
  97.     uint32_t pDec = pressurePa100 % 100;
  98.     if (pDec < 10) Serial.print("0");
  99.     Serial.println(pDec);
  100.     Serial.println(" hPa");
  101.     Serial.println("");
  102. }
  103.  
  104. /* END CODE */
  105.  
Advertisement
Add Comment
Please, Sign In to add comment