CCCPaste

Servo Fireworks rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Servo Fireworks
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-08-24 07:40:24

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system shall utilize the EasyButton library to */
	/* detect button presses on digital pins D2 and D4, */
	/* triggering specific actions in the Pong game.   If */
	/* the player has 5 points, play a fireworks */
	/* animation and turn the servo 90 degrees for 5 */
	/* seconds. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	// https://github.com/arduino-libraries/Servo
#include 
#include 	// https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
#include  // Include the EasyButton library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void onButton1Pressed(); // Callback for button D2
void onButton2Pressed(); // Callback for button D4
void playFireworks(); // Function to play fireworks animation
void moveServo(); // Function to move the servo

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Button_PushButton_PIN_D2		= 2;
const uint8_t LButton_PushButton_PIN_D4		= 4;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Servos_Servomotor_PWMSignal_PIN_D3		= 3;

/***** DEFINITION OF I2C PINS *****/
const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SDA_A4		= A4;
const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SCL_A5		= A5;
const uint8_t Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS		= 0x3C; // Default I2C address for SSD1306

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
uint8_t	Servos_Servomotor_PWMSignal_PIN_D3_rawData		= 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float	Servos_Servomotor_PWMSignal_PIN_D3_phyData		= 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create a Servo object to control the servo motor
Servo myservo; // Instance of the Servo class

// Create an instance of the Adafruit_SSD1306 class
#define OLED_RESET -1 // Define reset pin (not used in I2C)
Adafruit_SSD1306 display(OLED_RESET); // Create display object

// Create instances of the EasyButton class for each button
EasyButton button1(Button_PushButton_PIN_D2); // Button on D2
EasyButton button2(LButton_PushButton_PIN_D4); // Button on D4

// Variable to keep track of player points
int playerPoints = 0;

void setup(void)
{
	// put your setup code here, to run once:

	pinMode(Servos_Servomotor_PWMSignal_PIN_D3, OUTPUT);

	// Attach the servo control to the PWM output pin
	myservo.attach(Servos_Servomotor_PWMSignal_PIN_D3); // Attach the servo to pin D3

	// Initialize the display
	display.begin(SSD1306_SWITCHCAPVCC, Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
	display.clearDisplay(); // Clear the display buffer
	display.display(); // Show the buffer on the display

	// Initialize buttons
	button1.begin(); // Initialize button D2
	button2.begin(); // Initialize button D4
	button1.onPressed(onButton1Pressed); // Set callback for button D2
	button2.onPressed(onButton2Pressed); // Set callback for button D4
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	updateOutputs(); // Refresh output data

	// Continuously read the status of the buttons
	button1.read();
	button2.read();
}

void updateOutputs()
{
	analogWrite(Servos_Servomotor_PWMSignal_PIN_D3, Servos_Servomotor_PWMSignal_PIN_D3_rawData);
}

void onButton1Pressed()
{
	// Increment player points when button D2 is pressed
	playerPoints++;
	if (playerPoints >= 5) {
		playFireworks(); // Play fireworks animation
		moveServo(); // Move the servo
		playerPoints = 0; // Reset points after action
	}
}

void onButton2Pressed()
{
	// You can add functionality for button D4 here
}

void playFireworks()
{
	// Placeholder for fireworks animation
	display.clearDisplay();
	display.setTextSize(2);
	display.setTextColor(WHITE);
	display.setCursor(0, 0);
	display.println("Fireworks!");
	display.display();
	delay(2000);
	display.clearDisplay();
}

void moveServo()
{
	myservo.write(90); // Move servo to 90 degrees
	delay(5000); // Hold for 5 seconds
	myservo.write(0); // Move servo back to 0 degrees
}

/* END CODE */