int ledPins[] = {13, 14, 27}; // choose the pins for the LEDs
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
float phaseShift = 0; // phase shift for the wave effect
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT); // declare LEDs as outputs
}
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
for (int i = 0; i < 3; i++) {
int brightness = 127 + 127 * sin(phaseShift + i * PI / 3); // calculate brightness based on sine wave
analogWrite(ledPins[i], brightness); // set the LED brightness
}
if (pirState == LOW) {
// we have just detected motion
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
for (int i = 0; i < 3; i++) {
analogWrite(ledPins[i], 0); // turn off the LEDs
}
if (pirState == HIGH) {
// motion has ended
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
phaseShift += 0.1; // increment phase shift for next iteration
delay(50); // adjust the delay for the speed of the wave effect
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
led1:A
led1:C
pir1:VCC
pir1:OUT
pir1:GND
led2:A
led2:C
led3:A
led3:C