const int motorPin = 9; // Pin connected to the base of the NPN transistor
const int maxSpeed = 255; // Maximum speed for the motor
const int fadeTime = 3000; // Time to fade from 0 to max speed (in milliseconds)
void setup() {
pinMode(motorPin, OUTPUT); // Set the motor pin as an output
}
void loop() {
// Fade in from 0 to max speed
for (int speed = 0; speed <= maxSpeed; speed++) {
analogWrite(motorPin, speed); // Set the motor speed
delay(fadeTime / maxSpeed); // Wait for a short period to create a smooth transition
}
// Wait at maximum speed for a moment
delay(1000); // Wait for 1 second at maximum speed
// Fade out from max speed to 0
for (int speed = maxSpeed; speed >= 0; speed--) {
analogWrite(motorPin, speed); // Set the motor speed
delay(fadeTime / maxSpeed); // Wait for a short period to create a smooth transition
}
// Wait for a moment before restarting the cycle
delay(1000); // Wait for 1 second before restarting the cycle
}