Advertisement
pleasedontcode

GearCarIndicator rev_02

Sep 17th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.45 KB | Software | 0 0
  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: GearCarIndicator
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-09-17 15:54:59
  15.     - Source Code generated by: James
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /********* User code review feedback **********
  20. #### Feedback 1 ####
  21. - static uint8_t gear shall be global.
  22. ********* User code review feedback **********/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Wire.h>
  27. #include <EasyButton.h>
  28. #include <Adafruit_SSD1306.h>
  29.  
  30. /****** SYSTEM REQUIREMENT 1 *****/
  31. /* The display shows the indication of the gear from */
  32. /* 1 to 6. At the startup, the system shall show "N" */
  33. /* as Neutral. */
  34. /****** SYSTEM REQUIREMENT 2 *****/
  35. /* If the gear up switch is released, so the gear */
  36. /* value shall be increased by 1. If the gear down */
  37. /* switch is released, so the gear value shall be */
  38. /* decreased by 1. */
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t GearUp_PushButton_PIN_D2 = 2;
  46. const uint8_t GearDown_PushButton_PIN_D3 = 3;
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
  52.  
  53. // Instantiate EasyButton objects for gear up and gear down buttons
  54. EasyButton gearUpButton(GearUp_PushButton_PIN_D2);
  55. EasyButton gearDownButton(GearDown_PushButton_PIN_D3);
  56.  
  57. // Instantiate SSD1306 display object
  58. Adafruit_SSD1306 display(display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  59.  
  60. // Global variable for gear value
  61. static uint8_t gear = 0;
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.  
  67.     pinMode(GearUp_PushButton_PIN_D2, INPUT_PULLUP);
  68.     pinMode(GearDown_PushButton_PIN_D3, INPUT_PULLUP);
  69.  
  70.     // Initialize the SSD1306 display
  71.     display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  72.     display.clearDisplay();
  73.     display.setTextColor(WHITE);
  74.     display.setTextSize(2);
  75.     display.setCursor(0, 0);
  76.     display.println("N");
  77.     display.display();
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.  
  84.     // Check if the gear up button is released
  85.     if (gearUpButton.isReleased())
  86.     {
  87.         // Increase the gear value by 1
  88.         gear++;
  89.         if (gear > 6)
  90.         {
  91.             gear = 6;
  92.         }
  93.  
  94.         // Update the display with the new gear value
  95.         display.clearDisplay();
  96.         display.setCursor(0, 0);
  97.         display.println(gear);
  98.         display.display();
  99.     }
  100.  
  101.     // Check if the gear down button is released
  102.     if (gearDownButton.isReleased())
  103.     {
  104.         // Decrease the gear value by 1
  105.         if (gear > 0)
  106.         {
  107.             gear--;
  108.         }
  109.  
  110.         // Update the display with the new gear value
  111.         display.clearDisplay();
  112.         display.setCursor(0, 0);
  113.         display.println(gear);
  114.         display.display();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement