Advertisement
safwan092

Untitled

Feb 22nd, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. // Include the U8g2 library
  2. #include <Arduino.h>
  3. #include <U8g2lib.h>
  4. #include <QMC5883LCompass.h>
  5.  
  6. #ifdef U8X8_HAVE_HW_SPI
  7. #include <SPI.h>
  8. #endif
  9. #ifdef U8X8_HAVE_HW_I2C
  10. #include <Wire.h>
  11. #endif
  12.  
  13. // Initialize a U8g2 display object for a 128x64 OLED display with I2C interface
  14. //U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  15. //U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  16. //U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /reset=/ U8X8_PIN_NONE);
  17. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /*clock=*/ 9, /*data=*/ 10, /*reset=*/ U8X8_PIN_NONE);
  18.  
  19. QMC5883LCompass compass;
  20.  
  21. int angle = 0;
  22. // The setup function runs once when the program starts
  23. void setup() {
  24. Serial.begin(9600);
  25. u8g2.begin(); // Initialize the display
  26. u8g2.setContrast(255); // Set display contrast to maximum
  27. u8g2.setFont(u8g2_font_ncenB08_tr); // Set the font to use for displaying text
  28. compass.init();
  29. compass.read();
  30. //{
  31. // u8g2.clearBuffer(); // Clear the display buffer
  32. // u8g2.setCursor(60 , 40); // Set the cursor position for text
  33. // u8g2.print(counter); // Print the current count value to the display
  34. // u8g2.sendBuffer(); // Send the buffer to the display to update the display
  35. // }
  36. }
  37.  
  38. // The loop function runs repeatedly while the program is running
  39. void loop() {
  40. angle = compass.getAzimuth();
  41. Serial.println(angle);
  42. // Read compass values
  43. compass.read();
  44. //Serial.println(angle);
  45. u8g2.clearBuffer(); // Clear the display buffer
  46. u8g2.setFont(u8g2_font_ncenB08_tr);
  47. // Set the cursor position for text
  48. //aa = String(angle);
  49. if (angle >= 190 && angle <= 210) {
  50. u8g2.drawStr(1, 10, " OK");
  51. //u8g2.setCursor(30 , 40);
  52. //u8g2.print(angle); // Print the current count value to the display
  53. }
  54.  
  55. else {
  56. u8g2.drawStr(1, 10, "Error"); // Print the current count value to the display
  57. //u8g2.setCursor(30 , 40);
  58. //u8g2.print(angle); // Print the current count value to the display
  59. }
  60.  
  61. u8g2.sendBuffer();
  62. delay(100);
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement