Advertisement
pleasedontcode

prova2 rev_04

Oct 18th, 2023
91
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: prova2
  13.     - Source Code compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2023-10-18 18:52:55
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /********* User code review feedback **********
  20. #### Feedback 1 ####
  21. - If I don't press two times the button within 0.5s, so the button
  22. PressCount will reach in any case the number 2, but I cannot ret
  23. ry again because buttonPressCount will be always > 2.
  24. ********* User code review feedback **********/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <Wire.h>
  29. #include <Adafruit_SSD1306.h>
  30.  
  31. /****** SYSTEM REQUIREMENT 1 *****/
  32. /* if button is pressed two times within 0.5 seconds, */
  33. /* so display "hello" which scroll from left to right */
  34. /* and then activate the LED for 2 seconds. */
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void displayHello(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t myButton_PushButton_PIN_D3 = 3;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t blueLed_LED_PIN_D2 = 2;
  46.  
  47. /***** DEFINITION OF I2C PINS *****/
  48. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  49. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  50. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
  51.  
  52. // Global variables
  53. unsigned long buttonPressTime = 0;
  54. int buttonPressCount = 0;
  55.  
  56. Adafruit_SSD1306 display(display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  57.  
  58. void setup(void)
  59. {
  60.   // put your setup code here, to run once:
  61.  
  62.   pinMode(myButton_PushButton_PIN_D3, INPUT_PULLUP);
  63.   pinMode(blueLed_LED_PIN_D2, OUTPUT);
  64.  
  65.   display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_PIN_SDA_A4, display_SSD1306OledDisplay_I2C_PIN_SCL_A5);
  66.   display.clearDisplay();
  67.   display.setTextSize(2);
  68.   display.setTextColor(WHITE);
  69. }
  70.  
  71. void loop(void)
  72. {
  73.   // put your main code here, to run repeatedly:
  74.  
  75.   // Check if button is pressed
  76.   if (digitalRead(myButton_PushButton_PIN_D3) == LOW)
  77.   {
  78.     // Record the time of button press
  79.     if (buttonPressCount == 0)
  80.     {
  81.       buttonPressTime = millis();
  82.     }
  83.  
  84.     // Increment button press count
  85.     buttonPressCount++;
  86.  
  87.     // Check if button was pressed two times within 0.5 seconds
  88.     if (buttonPressCount >= 2 && millis() - buttonPressTime <= 500)
  89.     {
  90.       // Display "hello" scrolling from left to right
  91.       displayHello();
  92.  
  93.       // Activate the LED for 2 seconds
  94.       digitalWrite(blueLed_LED_PIN_D2, HIGH);
  95.       delay(2000);
  96.       digitalWrite(blueLed_LED_PIN_D2, LOW);
  97.  
  98.       // Reset button press count
  99.       buttonPressCount = 0;
  100.     }
  101.     else if (millis() - buttonPressTime > 500)
  102.     {
  103.       // Reset button press count if more than 0.5 seconds have passed
  104.       buttonPressCount = 0;
  105.     }
  106.   }
  107. }
  108.  
  109. void displayHello(void)
  110. {
  111.   display.clearDisplay();
  112.   display.setCursor(0, 0);
  113.   display.println("hello");
  114.   display.display();
  115.  
  116.   int16_t scrollPos = display.width();
  117.   while (scrollPos >= -display.width())
  118.   {
  119.     display.clearDisplay();
  120.     display.setCursor(scrollPos, 0);
  121.     display.println("hello");
  122.     display.display();
  123.     delay(100);
  124.     scrollPos--;
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement