Advertisement
pleasedontcode

**Button Control** rev_01

Mar 31st, 2025
572
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: **Button Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-03-31 23:37:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When button one is pressed move ArmsServo1, */
  21.     /* ArmServo2 and ArmServo3 by 45 degrees */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  29. #include <FastLED.h> // Added FastLED library for LED control
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Button1_PushButton_PIN_D2     = 2;
  37. const uint8_t Button2_PushButton_PIN_D4     = 4;
  38.  
  39. /***** DEFINITION OF PWM OUTPUT PINS *****/
  40. const uint8_t ArmServo1_Servomotor_PWMSignal_PIN_D3     = 3; // This pin is used for Button2
  41. const uint8_t ArmServo2_Servomotor_PWMSignal_PIN_D5     = 5;
  42. const uint8_t ArmServo3_Servomotor_PWMSignal_PIN_D6     = 6;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. uint8_t ArmServo1_Servomotor_PWMSignal_PIN_D3_rawData       = 0;
  46. uint8_t ArmServo2_Servomotor_PWMSignal_PIN_D5_rawData       = 0;
  47. uint8_t ArmServo3_Servomotor_PWMSignal_PIN_D6_rawData       = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. float   ArmServo1_Servomotor_PWMSignal_PIN_D3_phyData       = 0.0;
  51. float   ArmServo2_Servomotor_PWMSignal_PIN_D5_phyData       = 0.0;
  52. float   ArmServo3_Servomotor_PWMSignal_PIN_D6_phyData       = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. // LED setup
  56. #define NUM_LEDS 30
  57. #define LED_PIN 6
  58. CRGB leds[NUM_LEDS];
  59.  
  60. // Servo setup
  61. Servo arm1;
  62. Servo arm2;
  63. Servo arm3;
  64.  
  65. // Button states
  66. bool button1State = false;
  67. bool button2State = false;
  68.  
  69. // Servo movement angles
  70. int arm_rest_angle = 0; // Set resting angle for all servos
  71. int arm_move_angle = 45; // Set angle to move servos when button is pressed
  72.  
  73. void setup(void)
  74. {
  75.     // put your setup code here, to run once:
  76.  
  77.     pinMode(Button1_PushButton_PIN_D2,  INPUT_PULLUP);
  78.     pinMode(Button2_PushButton_PIN_D4,  INPUT_PULLUP);
  79.  
  80.     pinMode(ArmServo1_Servomotor_PWMSignal_PIN_D3,   OUTPUT);
  81.     pinMode(ArmServo2_Servomotor_PWMSignal_PIN_D5,   OUTPUT);
  82.     pinMode(ArmServo3_Servomotor_PWMSignal_PIN_D6,   OUTPUT);
  83.  
  84.     // Set up the servos
  85.     arm1.attach(ArmServo1_Servomotor_PWMSignal_PIN_D3); // Updated to use defined servo pins
  86.     arm2.attach(ArmServo2_Servomotor_PWMSignal_PIN_D5);
  87.     arm3.attach(ArmServo3_Servomotor_PWMSignal_PIN_D6);
  88.  
  89.     // Set up LED strip
  90.     FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  91.     FastLED.setBrightness(255);
  92.  
  93.     // Start with LEDs off and solid orange
  94.     fill_solid(leds, NUM_LEDS, CRGB::Orange);
  95.     FastLED.show();
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     // put your main code here, to run repeatedly:
  101.  
  102.     // Read button states
  103.     button1State = digitalRead(Button1_PushButton_PIN_D2) == LOW;
  104.     button2State = digitalRead(Button2_PushButton_PIN_D4) == LOW;
  105.  
  106.     if (button1State && !button2State) {
  107.         MoveArms(); // Call the function to move the servos by 45 degrees
  108.     } else if (button2State && !button1State) {
  109.         HoldLoop();
  110.     } else if (button1State && button2State) {
  111.         FlashAndReset();
  112.     } else {
  113.         ResetServosAndLEDs();
  114.     }
  115.  
  116.     // Handle LED effects (optional)
  117.     HandleLEDs();
  118. }
  119.  
  120. // Function to move servos by 45 degrees
  121. void MoveArms() {
  122.     arm1.write(arm_move_angle);
  123.     arm2.write(arm_move_angle);
  124.     arm3.write(arm_move_angle);
  125.     delay(500); // Delay to allow servos to reach the position
  126.     arm1.write(arm_rest_angle);
  127.     arm2.write(arm_rest_angle);
  128.     arm3.write(arm_rest_angle);
  129. }
  130.  
  131. void updateOutputs()
  132. {
  133.     analogWrite(ArmServo1_Servomotor_PWMSignal_PIN_D3, ArmServo1_Servomotor_PWMSignal_PIN_D3_rawData);
  134.     analogWrite(ArmServo2_Servomotor_PWMSignal_PIN_D5, ArmServo2_Servomotor_PWMSignal_PIN_D5_rawData);
  135.     analogWrite(ArmServo3_Servomotor_PWMSignal_PIN_D6, ArmServo3_Servomotor_PWMSignal_PIN_D6_rawData);
  136. }
  137.  
  138. // HoldLoop - Move servos to 90 degrees and flicker the LEDs
  139. void HoldLoop() {
  140.     arm1.write(90);
  141.     arm2.write(90);
  142.     arm3.write(90);
  143.  
  144.     // Flicker effect on LEDs (orange and deep orange)
  145.     static unsigned long lastFlickerTime = 0;
  146.     static bool flickerOn = false;
  147.     if (millis() - lastFlickerTime > 100) {
  148.         lastFlickerTime = millis();
  149.         flickerOn = !flickerOn;
  150.         if (flickerOn) {
  151.             fill_solid(leds, NUM_LEDS, CRGB::Orange);
  152.         } else {
  153.             fill_solid(leds, NUM_LEDS, CRGB::DarkOrange);
  154.         }
  155.         FastLED.show();
  156.     }
  157. }
  158.  
  159. // FlashAndReset - Flash LEDs to 100% brightness and then reset everything
  160. void FlashAndReset() {
  161.     // Flash LEDs to full brightness for a moment
  162.     fill_solid(leds, NUM_LEDS, CRGB::Yellow);
  163.     FastLED.show();
  164.     delay(200);
  165.  
  166.     // Reset LEDs and servos to normal
  167.     ResetServosAndLEDs();
  168. }
  169.  
  170. // Reset the servos and LEDs to their resting positions and state
  171. void ResetServosAndLEDs() {
  172.     arm1.write(arm_rest_angle);
  173.     arm2.write(arm_rest_angle);
  174.     arm3.write(arm_rest_angle);
  175.  
  176.     // Set LEDs to a dim orange (normal state)
  177.     fill_solid(leds, NUM_LEDS, CRGB::Orange);
  178.     FastLED.show();
  179. }
  180.  
  181. // Handle LED effects (simple fading and flickering)
  182. void HandleLEDs() {
  183.     static unsigned long lastFadeTime = 0;
  184.     static bool fadeIn = true;
  185.     static int fadeBrightness = 0;
  186.  
  187.     // Simple fade-in and fade-out effect on LEDs
  188.     if (millis() - lastFadeTime > 100) {
  189.         lastFadeTime = millis();
  190.         if (fadeIn) {
  191.             fadeBrightness += 5;
  192.             if (fadeBrightness >= 255) {
  193.                 fadeIn = false;
  194.             }
  195.         } else {
  196.             fadeBrightness -= 5;
  197.             if (fadeBrightness <= 0) {
  198.                 fadeIn = true;
  199.             }
  200.         }
  201.         FastLED.setBrightness(fadeBrightness);
  202.         FastLED.show();
  203.     }
  204. }
  205.  
  206. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement