pleasedontcode

Vibration Pulse rev_120

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