Advertisement
pleasedontcode

Servo Fireworks rev_01

Aug 24th, 2024
175
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: Servo Fireworks
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-24 07:40:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize the EasyButton library to */
  21.     /* detect button presses on digital pins D2 and D4, */
  22.     /* triggering specific actions in the Pong game.   If */
  23.     /* the player has 5 points, play a fireworks */
  24.     /* animation and turn the servo 90 degrees for 5 */
  25.     /* seconds. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <Servo.h>  // https://github.com/arduino-libraries/Servo
  31. #include <Adafruit_GFX.h>
  32. #include <Adafruit_SSD1306.h>   // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  33. #include <EasyButton.h> // Include the EasyButton library
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs();
  39. void onButton1Pressed(); // Callback for button D2
  40. void onButton2Pressed(); // Callback for button D4
  41. void playFireworks(); // Function to play fireworks animation
  42. void moveServo(); // Function to move the servo
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t Button_PushButton_PIN_D2      = 2;
  46. const uint8_t LButton_PushButton_PIN_D4     = 4;
  47.  
  48. /***** DEFINITION OF PWM OUTPUT PINS *****/
  49. const uint8_t Servos_Servomotor_PWMSignal_PIN_D3        = 3;
  50.  
  51. /***** DEFINITION OF I2C PINS *****/
  52. const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SDA_A4     = A4;
  53. const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SCL_A5     = A5;
  54. const uint8_t Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS      = 0x3C; // Default I2C address for SSD1306
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. uint8_t Servos_Servomotor_PWMSignal_PIN_D3_rawData      = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. float   Servos_Servomotor_PWMSignal_PIN_D3_phyData      = 0.0;
  61.  
  62. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  63. // Create a Servo object to control the servo motor
  64. Servo myservo; // Instance of the Servo class
  65.  
  66. // Create an instance of the Adafruit_SSD1306 class
  67. #define OLED_RESET -1 // Define reset pin (not used in I2C)
  68. Adafruit_SSD1306 display(OLED_RESET); // Create display object
  69.  
  70. // Create instances of the EasyButton class for each button
  71. EasyButton button1(Button_PushButton_PIN_D2); // Button on D2
  72. EasyButton button2(LButton_PushButton_PIN_D4); // Button on D4
  73.  
  74. // Variable to keep track of player points
  75. int playerPoints = 0;
  76.  
  77. void setup(void)
  78. {
  79.     // put your setup code here, to run once:
  80.  
  81.     pinMode(Servos_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  82.  
  83.     // Attach the servo control to the PWM output pin
  84.     myservo.attach(Servos_Servomotor_PWMSignal_PIN_D3); // Attach the servo to pin D3
  85.  
  86.     // Initialize the display
  87.     display.begin(SSD1306_SWITCHCAPVCC, Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
  88.     display.clearDisplay(); // Clear the display buffer
  89.     display.display(); // Show the buffer on the display
  90.  
  91.     // Initialize buttons
  92.     button1.begin(); // Initialize button D2
  93.     button2.begin(); // Initialize button D4
  94.     button1.onPressed(onButton1Pressed); // Set callback for button D2
  95.     button2.onPressed(onButton2Pressed); // Set callback for button D4
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     // put your main code here, to run repeatedly:
  101.     updateOutputs(); // Refresh output data
  102.  
  103.     // Continuously read the status of the buttons
  104.     button1.read();
  105.     button2.read();
  106. }
  107.  
  108. void updateOutputs()
  109. {
  110.     analogWrite(Servos_Servomotor_PWMSignal_PIN_D3, Servos_Servomotor_PWMSignal_PIN_D3_rawData);
  111. }
  112.  
  113. void onButton1Pressed()
  114. {
  115.     // Increment player points when button D2 is pressed
  116.     playerPoints++;
  117.     if (playerPoints >= 5) {
  118.         playFireworks(); // Play fireworks animation
  119.         moveServo(); // Move the servo
  120.         playerPoints = 0; // Reset points after action
  121.     }
  122. }
  123.  
  124. void onButton2Pressed()
  125. {
  126.     // You can add functionality for button D4 here
  127. }
  128.  
  129. void playFireworks()
  130. {
  131.     // Placeholder for fireworks animation
  132.     display.clearDisplay();
  133.     display.setTextSize(2);
  134.     display.setTextColor(WHITE);
  135.     display.setCursor(0, 0);
  136.     display.println("Fireworks!");
  137.     display.display();
  138.     delay(2000);
  139.     display.clearDisplay();
  140. }
  141.  
  142. void moveServo()
  143. {
  144.     myservo.write(90); // Move servo to 90 degrees
  145.     delay(5000); // Hold for 5 seconds
  146.     myservo.write(0); // Move servo back to 0 degrees
  147. }
  148.  
  149. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement