Hello all,
i am working on a project where I have an RGB LED that is steady green at powerup and when the button is released the RGB LED turns purple and starts rapidly flashing and over 5 seconds slows until it becomes steady purple. At the same time the RGB LED turns purple and starts flashing a continuous servo starts spinning and immediately starts decelerating until stopped with in 5 seconds, the servo speed is 20 RPS or less.
The issue i am having is the servo doesn't start decelerating until after the RGB LED stops flashing and goes steady purple. I am not sure of a way to adjust the script with proper breaks to allow the multiple task to start at the same time.
Any help would be appreciated. I am still learning and to be transparent i have been using Copilot to help lay this out but have found it was full of issue i had to resolve to get to this point. so the lesson have been good so far.
i also loaded a screen shot of the cosplay design to give you more of a visual of what this is. maybe it helps. (its still a work in progress.
#include <Servo.h>
// Define pin numbers
const int buttonPin = 2; // Button connected to digital pin 2
const int redPin = 9; // Red LED pin
const int greenPin = 10; // Green LED pin
const int bluePin = 11; // Blue LED pin
const int servoPin = 3; // Servo connected to digital pin 3
// Variables to store the button state
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
// Adjustable flash rate and duration (in milliseconds)
unsigned long initialFlashRate = 100; // Initial flash rate in milliseconds
unsigned long finalFlashRate = 300; // Final flash rate in milliseconds
unsigned long flashDuration = 5000; // Flash duration in milliseconds
// Servo control variables
unsigned long runTime = 5000; // Programmable run time in milliseconds
int initialServoSpeed = 20; // Programmable initial speed (0 to 180)
unsigned long slowDownTime = 1000; // Programmable slow down time in milliseconds
float decelerationRate = 0.9; // Programmable deceleration rate (1.0 for linear, >1.0 for faster, <1.0 for slower)
bool servoRunning = false;
unsigned long servoStartTime;
// LED flashing variables
unsigned long flashStartTime = 0;
bool flashing = false;
unsigned long lastFlashTime = 0;bool ledState = false;
// Variable to track if the sequence is active
bool sequenceActive = false;
Servo myServo;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
// Initialize the LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Initialize the button pin as an input
pinMode(buttonPin, INPUT);
// Attach the servo
myServo.attach(servoPin);
// Set the initial LED color to green
setColor(0, 255, 0);
}
void loop() {
// Read the state of the button
int reading = digitalRead(buttonPin);
// Check if the button state has changed
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// If the button is released and the sequence is not active
if (buttonState == LOW && !sequenceActive) {
Serial.println("Button released, starting servo and LED sequence.");
sequenceActive = true;
// Start the servo
servoStartTime = millis();
servoRunning = true;
myServo.write(initialServoSpeed); // Start the servo immediately
Serial.println("Servo started at speed: " + String(initialServoSpeed));
// Start the LED flashing sequence
flashStartTime = millis();
flashing = true;
lastFlashTime = millis();
}
// If the button is pressed and the sequence is active
if (buttonState == HIGH && sequenceActive) {
Serial.println("Button pressed, stopping servo and resetting LED.");
sequenceActive = false;
// Reset the LED to green
setColor(0, 255, 0);
}
}
}
lastButtonState = reading;
// Servo control
if (servoRunning) {
unsigned long currentTime = millis();
if (currentTime - servoStartTime < runTime) {
myServo.write(initialServoSpeed); // Run the servo at the set speed
Serial.println("Servo running at speed: " + String(initialServoSpeed));
} else {
unsigned long elapsed = currentTime - (servoStartTime + runTime);
if (elapsed < slowDownTime) {
// Proportional control for smooth deceleration
float proportion = pow((float)(slowDownTime - elapsed) / slowDownTime, decelerationRate);
int slowSpeed = 0 + (initialServoSpeed - 0) * proportion; // 90 is the stop position for continuous servos
myServo.write(slowSpeed);
Serial.println("Servo slowing down, speed: " + String(slowSpeed));
} else {
myServo.write(0); // Stop the servo
servoRunning = false;
Serial.println("Servo stopped.");
}
}
}
// LED flashing control
if (flashing) {
unsigned long currentTime = millis();
if (currentTime - flashStartTime < flashDuration) {
unsigned long flashInterval = initialFlashRate + (finalFlashRate - initialFlashRate) * (currentTime - flashStartTime) / flashDuration;
if (currentTime - lastFlashTime >= flashInterval) {
ledState = !ledState;
if (ledState) {
setColor(128, 0, 128); // Purple
} else {
setColor(0, 0, 0); // Turn off LED
}
lastFlashTime = currentTime;
}
} else {
flashing = false;
setColor(128, 0, 128); // Set to solid purple
}
}
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}


