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: Vibration Pulse
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-08-14 23:52:31
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* if vibrationSignal == 1, so transform activateSong */
- /* in output and activate it LOW for 500ms and then */
- /* retransform activateSong in digital input with */
- /* pull up again. Then move servo to 180°, maintain */
- /* it for 30 seconds and then go back again to 0°. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* if vibrationSignal == 1, so transform activateSong */
- /* in output and activate it LOW for 500ms and then */
- /* retransform activateSong in digital input with */
- /* pull up again. Then move servo to 180°, maintain */
- /* it for 30 seconds and then go back again to 0°. */
- /****** END SYSTEM REQUIREMENTS *****
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> //https://github.com/arduino-libraries/Servo
- // Create Servo object
- Servo myservo;
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t vibrationSignal_PIN_D2 = 2;
- const uint8_t activateSong_PIN_D4 = 4;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t myActiveBuzzer_ActiveBuzzer_output_PIN_D5 = 5;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t myServo_Servomotor_PWMSignal_PIN_D3 = 3;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(vibrationSignal_PIN_D2, INPUT);
- pinMode(activateSong_PIN_D4, INPUT_PULLUP);
- pinMode(myActiveBuzzer_ActiveBuzzer_output_PIN_D5, OUTPUT);
- // Attach servo to PWM pin
- myservo.attach(myServo_Servomotor_PWMSignal_PIN_D3);
- // Initialize servo position
- myservo.write(0);
- }
- // State machine for non-blocking operation
- enum State { IDLE, ACTIVATION, SERVO_HOLD_STATE, DONE };
- State currentState = IDLE;
- unsigned long activationStartMillis = 0;
- unsigned long servoHoldStartMillis = 0;
- uint8_t lastVibrationState = LOW;
- void loop(void)
- {
- // Read current vibration input
- uint8_t currentVibration = digitalRead(vibrationSignal_PIN_D2);
- // Rising edge detection to start the sequence only when idle
- if (currentState == IDLE && currentVibration == HIGH && lastVibrationState == LOW)
- {
- // Begin activation by driving activation pin LOW for 500ms
- pinMode(activateSong_PIN_D4, OUTPUT);
- digitalWrite(activateSong_PIN_D4, LOW);
- activationStartMillis = millis();
- currentState = ACTIVATION;
- }
- // Update last vibration state
- lastVibrationState = currentVibration;
- // Non-blocking state machine
- switch (currentState)
- {
- case ACTIVATION:
- if (millis() - activationStartMillis >= 500)
- {
- // Revert activation pin to INPUT_PULLUP
- pinMode(activateSong_PIN_D4, INPUT_PULLUP);
- // Move servo to 180 degrees and start hold timer
- myservo.write(180);
- servoHoldStartMillis = millis();
- currentState = SERVO_HOLD_STATE;
- }
- break;
- case SERVO_HOLD_STATE:
- if (millis() - servoHoldStartMillis >= 30000)
- {
- // Return servo to 0 degrees
- myservo.write(0);
- currentState = DONE;
- }
- break;
- case DONE:
- // Wait for input to go LOW to reset to IDLE
- if (currentVibration == LOW)
- {
- currentState = IDLE;
- }
- break;
- case IDLE:
- default:
- // Remain idle
- break;
- }
- // Optional small buzzer activity or other tasks could go here
- // Avoid blocking delays
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment