Advertisement
pleasedontcode

"Speedometer Interface" rev_01

Feb 18th, 2024
69
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: "Speedometer Interface"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-18 08:37:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* map the pot value in the oled to show the */
  21.     /* speedometer as of a car ,on the oled , and toggle */
  22.     /* between 3 interfeces of the speedo meter using the */
  23.     /* push button */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  28. #include <Adafruit_GFX.h>
  29. #include <Adafruit_SSD1306.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void displaySpeedometer(int speed);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t button_PushButton_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t button1_Potentiometer_Vout_PIN_A0 = A0;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  43. EasyButton button(button_PushButton_PIN_D2);
  44. Adafruit_SSD1306 display(128, 64, &Wire, -1);
  45.  
  46. int speed = 0;
  47. int prevSpeed = 0;
  48. int interface = 1;
  49.  
  50. void setup(void)
  51. {
  52.   // Put your setup code here, to run once:
  53.   Serial.begin(115200);
  54.  
  55.   button.begin();
  56.  
  57.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  58.   display.clearDisplay();
  59.   display.setTextColor(SSD1306_WHITE);
  60.   display.setTextSize(2);
  61.   display.setCursor(0, 0);
  62.   display.println("Speedometer");
  63.   display.display();
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   // Put your main code here, to run repeatedly:
  69.   button.read();
  70.  
  71.   if (button.wasPressed())
  72.   {
  73.     interface++;
  74.     if (interface > 3)
  75.       interface = 1;
  76.      
  77.     display.clearDisplay();
  78.     display.setCursor(0, 0);
  79.    
  80.     switch (interface)
  81.     {
  82.       case 1:
  83.         display.println("Interface 1");
  84.         break;
  85.       case 2:
  86.         display.println("Interface 2");
  87.         break;
  88.       case 3:
  89.         display.println("Interface 3");
  90.         break;
  91.     }
  92.    
  93.     display.display();
  94.     delay(500);
  95.   }
  96.  
  97.   // Read speed from potentiometer
  98.   int potValue = analogRead(button1_Potentiometer_Vout_PIN_A0);
  99.   speed = map(potValue, 0, 1023, 0, 200);
  100.  
  101.   // Only update display if speed has changed
  102.   if (speed != prevSpeed)
  103.   {
  104.     displaySpeedometer(speed);
  105.     prevSpeed = speed;
  106.   }
  107. }
  108.  
  109. void displaySpeedometer(int speed)
  110. {
  111.   display.clearDisplay();
  112.   display.setTextSize(2);
  113.   display.setCursor(0, 0);
  114.   display.print("Speed: ");
  115.   display.print(speed);
  116.   display.print(" km/h");
  117.   display.display();
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement