const int outputPin1 = 9; // choose the output pin number for PWM output 1
const int outputPin2 = 10; // choose the output pin number for PWM output 2
const int pwmSteps = 255; // set the number of PWM steps for full range
const int pwmIncrement = 5; // set the PWM increment for each step
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
void setup() {
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
}
void loop() {
rampUpDownOutput1(outputPin1, 10); // ramp up and down PWM output 1 with a time interval of 10 milliseconds
pulseOutput2(outputPin2, 500); // pulse PWM output 2 with a time interval of 500 milliseconds
}
void rampUpDownOutput1(int outputPin, unsigned long interval) {
static int pwmValue = 0;
static bool rampUp = true;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis1 >= interval) {
previousMillis1 = currentMillis;
if (rampUp) {
pwmValue += pwmIncrement;
if (pwmValue >= pwmSteps) {
pwmValue = pwmSteps;
rampUp = false;
}
} else {
pwmValue -= pwmIncrement;
if (pwmValue <= 0) {
pwmValue = 0;
rampUp = true;
}
}
analogWrite(outputPin, pwmValue);
}
}
void pulseOutput2(int outputPin, unsigned long interval) {
static bool pulseState = false;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= interval) {
previousMillis2 = currentMillis;
pulseState = !pulseState;
int pulseValue = pulseState ? pwmSteps : 0;
analogWrite(outputPin, pulseValue);
}
}