Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: **Button Control**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-03-31 23:37:08
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* When button one is pressed move ArmsServo1, */
- /* ArmServo2 and ArmServo3 by 45 degrees */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> //https://github.com/arduino-libraries/Servo
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- #include <FastLED.h> // Added FastLED library for LED control
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Button1_PushButton_PIN_D2 = 2;
- const uint8_t Button2_PushButton_PIN_D4 = 4;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t ArmServo1_Servomotor_PWMSignal_PIN_D3 = 3; // This pin is used for Button2
- const uint8_t ArmServo2_Servomotor_PWMSignal_PIN_D5 = 5;
- const uint8_t ArmServo3_Servomotor_PWMSignal_PIN_D6 = 6;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- uint8_t ArmServo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
- uint8_t ArmServo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
- uint8_t ArmServo3_Servomotor_PWMSignal_PIN_D6_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float ArmServo1_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
- float ArmServo2_Servomotor_PWMSignal_PIN_D5_phyData = 0.0;
- float ArmServo3_Servomotor_PWMSignal_PIN_D6_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // LED setup
- #define NUM_LEDS 30
- #define LED_PIN 6
- CRGB leds[NUM_LEDS];
- // Servo setup
- Servo arm1;
- Servo arm2;
- Servo arm3;
- // Button states
- bool button1State = false;
- bool button2State = false;
- // Servo movement angles
- int arm_rest_angle = 0; // Set resting angle for all servos
- int arm_move_angle = 45; // Set angle to move servos when button is pressed
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(Button1_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(Button2_PushButton_PIN_D4, INPUT_PULLUP);
- pinMode(ArmServo1_Servomotor_PWMSignal_PIN_D3, OUTPUT);
- pinMode(ArmServo2_Servomotor_PWMSignal_PIN_D5, OUTPUT);
- pinMode(ArmServo3_Servomotor_PWMSignal_PIN_D6, OUTPUT);
- // Set up the servos
- arm1.attach(ArmServo1_Servomotor_PWMSignal_PIN_D3); // Updated to use defined servo pins
- arm2.attach(ArmServo2_Servomotor_PWMSignal_PIN_D5);
- arm3.attach(ArmServo3_Servomotor_PWMSignal_PIN_D6);
- // Set up LED strip
- FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
- FastLED.setBrightness(255);
- // Start with LEDs off and solid orange
- fill_solid(leds, NUM_LEDS, CRGB::Orange);
- FastLED.show();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- // Read button states
- button1State = digitalRead(Button1_PushButton_PIN_D2) == LOW;
- button2State = digitalRead(Button2_PushButton_PIN_D4) == LOW;
- if (button1State && !button2State) {
- MoveArms(); // Call the function to move the servos by 45 degrees
- } else if (button2State && !button1State) {
- HoldLoop();
- } else if (button1State && button2State) {
- FlashAndReset();
- } else {
- ResetServosAndLEDs();
- }
- // Handle LED effects (optional)
- HandleLEDs();
- }
- // Function to move servos by 45 degrees
- void MoveArms() {
- arm1.write(arm_move_angle);
- arm2.write(arm_move_angle);
- arm3.write(arm_move_angle);
- delay(500); // Delay to allow servos to reach the position
- arm1.write(arm_rest_angle);
- arm2.write(arm_rest_angle);
- arm3.write(arm_rest_angle);
- }
- void updateOutputs()
- {
- analogWrite(ArmServo1_Servomotor_PWMSignal_PIN_D3, ArmServo1_Servomotor_PWMSignal_PIN_D3_rawData);
- analogWrite(ArmServo2_Servomotor_PWMSignal_PIN_D5, ArmServo2_Servomotor_PWMSignal_PIN_D5_rawData);
- analogWrite(ArmServo3_Servomotor_PWMSignal_PIN_D6, ArmServo3_Servomotor_PWMSignal_PIN_D6_rawData);
- }
- // HoldLoop - Move servos to 90 degrees and flicker the LEDs
- void HoldLoop() {
- arm1.write(90);
- arm2.write(90);
- arm3.write(90);
- // Flicker effect on LEDs (orange and deep orange)
- static unsigned long lastFlickerTime = 0;
- static bool flickerOn = false;
- if (millis() - lastFlickerTime > 100) {
- lastFlickerTime = millis();
- flickerOn = !flickerOn;
- if (flickerOn) {
- fill_solid(leds, NUM_LEDS, CRGB::Orange);
- } else {
- fill_solid(leds, NUM_LEDS, CRGB::DarkOrange);
- }
- FastLED.show();
- }
- }
- // FlashAndReset - Flash LEDs to 100% brightness and then reset everything
- void FlashAndReset() {
- // Flash LEDs to full brightness for a moment
- fill_solid(leds, NUM_LEDS, CRGB::Yellow);
- FastLED.show();
- delay(200);
- // Reset LEDs and servos to normal
- ResetServosAndLEDs();
- }
- // Reset the servos and LEDs to their resting positions and state
- void ResetServosAndLEDs() {
- arm1.write(arm_rest_angle);
- arm2.write(arm_rest_angle);
- arm3.write(arm_rest_angle);
- // Set LEDs to a dim orange (normal state)
- fill_solid(leds, NUM_LEDS, CRGB::Orange);
- FastLED.show();
- }
- // Handle LED effects (simple fading and flickering)
- void HandleLEDs() {
- static unsigned long lastFadeTime = 0;
- static bool fadeIn = true;
- static int fadeBrightness = 0;
- // Simple fade-in and fade-out effect on LEDs
- if (millis() - lastFadeTime > 100) {
- lastFadeTime = millis();
- if (fadeIn) {
- fadeBrightness += 5;
- if (fadeBrightness >= 255) {
- fadeIn = false;
- }
- } else {
- fadeBrightness -= 5;
- if (fadeBrightness <= 0) {
- fadeIn = true;
- }
- }
- FastLED.setBrightness(fadeBrightness);
- FastLED.show();
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement