Advertisement
learnelectronics

Arduino Spirit Level V2

Jul 24th, 2023
1,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Digital Spirit Level v2
  3.  *
  4.  * learnelectronics
  5.  * 4 JUL 2023
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9. //---------------------------------------------------------------------------------------------------------------
  10. //                                                 LIBRARIES
  11. //---------------------------------------------------------------------------------------------------------------
  12.  
  13. #include <Wire.h>                                 //wire library for oled
  14. #include <Adafruit_SSD1306.h>                     //oled driver
  15.  
  16. //---------------------------------------------------------------------------------------------------------------
  17. //                                                  DEFINES
  18. //---------------------------------------------------------------------------------------------------------------
  19.  
  20. #define OLED_RESET 4                              //used by adafruit library
  21.  
  22. //---------------------------------------------------------------------------------------------------------------
  23. //                                              LIBRARY OBJECTS
  24. //---------------------------------------------------------------------------------------------------------------
  25.  
  26. Adafruit_SSD1306 display(OLED_RESET);             //create instance of oled called display
  27.  
  28. //---------------------------------------------------------------------------------------------------------------
  29. //                                                  VARIABLES
  30. //---------------------------------------------------------------------------------------------------------------
  31.  
  32. float x0;                                         //initial reading of x (0 point)
  33. float y0;                                         //initial reading of y (0 point)
  34. float xnow;                                       //current reading of x
  35. float ynow;                                       //current reading of y
  36. int xplot;                                        //difference from center x axis
  37. int yplot;                                        //difference from center y axis
  38.  
  39.  
  40. //---------------------------------------------------------------------------------------------------------------
  41. //                                                    SETUP
  42. //---------------------------------------------------------------------------------------------------------------
  43.  
  44. void setup()   {                
  45.  
  46.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);      //start oled @ hex addy 0x3C
  47.   display.display();                              //clear display
  48.   display.clearDisplay();                         //clear display
  49.   delay(1000);                                    //wait 1 sec to get 0 reading
  50.   x0 = analogRead(A0);                            //get 0 reading of x
  51.   y0 = analogRead(A1);                            //get 0 reading of y
  52.   delay(1000);                                    //wait 1 sec before beginning
  53.  
  54. }
  55.  
  56. //---------------------------------------------------------------------------------------------------------------
  57. //                                                  MAIN LOOP
  58. //---------------------------------------------------------------------------------------------------------------
  59.  
  60. void loop() {
  61.  
  62.   xnow = analogRead(A0);                          //get current value of x
  63.   ynow = analogRead(A1);                          //get current value of y
  64.  
  65.   display.drawCircle(64, 32, 10, WHITE);          //draw the bullseye that marks center (xcenter,ycenter,radius,color)
  66.  
  67.   if(xnow>x0+5){                                  //if tilted left (5 unit buffer zone)
  68.     xplot = (xnow-x0)+64;                         //difference is current-0 offset by center point
  69.   }
  70.  
  71.   else if(xnow<x0-5){                             //if tilted right (5 unit buffer zone)
  72.     xplot = 64-(x0-xnow);                         //difference is center point offset by 0-current
  73.   }
  74.  
  75.   else{                                           //otherwise x axis is centered
  76.     xplot = 64;                                   // center point
  77.   }
  78.  
  79.  
  80. if(ynow>y0+5){
  81.     yplot = (ynow-y0)+32;
  82.   }
  83.  
  84.   else if(ynow<y0-5){
  85.     yplot = 32-(y0-ynow);
  86.   }
  87.  
  88.   else{
  89.     yplot = 32;
  90.   }
  91.  
  92.   display.fillCircle(xplot, yplot, 8, WHITE);     //draw filled dot (xcenter,ycenter,radius,color)
  93.  
  94.   delay(500);                                     //wait 1/2 sec to make it more stable
  95.   display.display();                              //show whats in the buffer
  96.   display.clearDisplay();                         //clear screen & buffer
  97. }
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement