pleasedontcode

Vibration Controller rev_121

Aug 14th, 2025
381
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: Vibration Controller
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2025-08-14 23:55:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if vibrationSignal == 1, so transform activateSong */
  21.     /* in output and activate it LOW for 500ms and then */
  22.     /* retransform activateSong in digital input with */
  23.     /* pull up again. Then move servo to 180°, maintain */
  24.     /* it for 30 seconds and then go back again to 0°. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28.  
  29.  
  30. /* START CODE */
  31.  
  32. /****** SYSTEM REQUIREMENTS *****/
  33. /****** SYSTEM REQUIREMENT 1 *****/
  34.     /* if vibrationSignal == 1, so transform activateSong */
  35.     /* in output and activate it LOW for 500ms and then */
  36.     /* retransform activateSong in digital input with */
  37.     /* pull up again. Then move servo to 180°, maintain */
  38.     /* it for 30 seconds and then go back again to 0°. */
  39. /****** END SYSTEM REQUIREMENTS *****
  40.  
  41. /****** DEFINITION OF LIBRARIES *****/
  42. #include <Servo.h> //https://github.com/arduino-libraries/Servo
  43.  
  44. // Create Servo object
  45. Servo myservo;
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50.  
  51. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  52. const uint8_t vibrationSignal_PIN_D2        = 2;
  53. const uint8_t activateSong_PIN_D4       = 4;
  54.  
  55. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  56. const uint8_t myActiveBuzzer_ActiveBuzzer_output_PIN_D5     = 5;
  57.  
  58. /***** DEFINITION OF PWM OUTPUT PINS *****/
  59. const uint8_t myServo_Servomotor_PWMSignal_PIN_D3       = 3;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.  
  67.     pinMode(vibrationSignal_PIN_D2, INPUT);
  68.     pinMode(activateSong_PIN_D4,    INPUT_PULLUP);
  69.  
  70.     pinMode(myActiveBuzzer_ActiveBuzzer_output_PIN_D5,   OUTPUT);
  71.     // Attach servo to PWM pin
  72.     myservo.attach(myServo_Servomotor_PWMSignal_PIN_D3);
  73.  
  74.     // Initialize servo position
  75.     myservo.write(0);
  76. }
  77.  
  78.  
  79. // State machine for non-blocking operation
  80. enum State { IDLE, ACTIVATION, SERVO_HOLD_STATE, DONE };
  81. State currentState = IDLE;
  82.  
  83. unsigned long activationStartMillis = 0;
  84. unsigned long servoHoldStartMillis = 0;
  85. uint8_t lastVibrationState = LOW;
  86.  
  87. void loop(void)
  88. {
  89.     // Read current vibration input
  90.     uint8_t currentVibration = digitalRead(vibrationSignal_PIN_D2);
  91.  
  92.     // Rising edge detection to start the sequence only when idle
  93.     if (currentState == IDLE && currentVibration == HIGH && lastVibrationState == LOW)
  94.     {
  95.         // Begin activation by driving activation pin LOW for 500ms
  96.         pinMode(activateSong_PIN_D4, OUTPUT);
  97.         digitalWrite(activateSong_PIN_D4, LOW);
  98.         activationStartMillis = millis();
  99.         currentState = ACTIVATION;
  100.     }
  101.  
  102.     // Update last vibration state
  103.     lastVibrationState = currentVibration;
  104.  
  105.     // Non-blocking state machine
  106.     switch (currentState)
  107.     {
  108.         case ACTIVATION:
  109.             if (millis() - activationStartMillis >= 500)
  110.             {
  111.                 // Revert activation pin to INPUT_PULLUP
  112.                 pinMode(activateSong_PIN_D4, INPUT_PULLUP);
  113.                 // Move servo to 180 degrees and start hold timer
  114.                 myservo.write(180);
  115.                 servoHoldStartMillis = millis();
  116.                 currentState = SERVO_HOLD_STATE;
  117.             }
  118.             break;
  119.  
  120.         case SERVO_HOLD_STATE:
  121.             if (millis() - servoHoldStartMillis >= 30000)
  122.             {
  123.                 // Return servo to 0 degrees
  124.                 myservo.write(0);
  125.                 currentState = DONE;
  126.             }
  127.             break;
  128.  
  129.         case DONE:
  130.             // Wait for input to go LOW to reset to IDLE
  131.             if (currentVibration == LOW)
  132.             {
  133.                 currentState = IDLE;
  134.             }
  135.             break;
  136.  
  137.         case IDLE:
  138.         default:
  139.             // Remain idle
  140.             break;
  141.     }
  142.  
  143.     // Optional small buzzer activity or other tasks could go here
  144.     // Avoid blocking delays
  145. }
  146.  
  147. /* END CODE */
  148.  
Advertisement
Add Comment
Please, Sign In to add comment