int pirPin = 7; // Input for HC-S501
#define BLUE 3
#define GREEN 5
#define RED 6
int pirValue; // Place to store read PIR Value
void setup() {
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(pirPin, INPUT);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
}
void loop() {
pirValue = digitalRead(pirPin);
if (pirValue == HIGH); {
loop(); {
digitalWrite(RED, HIGH);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
delay(100);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
delay(100);
}
}
}
This code is supposed to trigger a red/blue strobe in the RGB LEDs when motion is detected. I have tried coding it several different ways, but it would either go off when the board was turned on, or do nothing at all. I am using an Arduino Mega from the Elegoo Most Complete Starter Kit. If anyone has ideas to make it work, I would appreciate it!
With any new sensor, a good first step is just doing something even simpler than your sketch to just see what the sensor is (or isn't) doing for you.
void loop() {
pirValue = digitalRead(pirPin);
digitalWrite(RED, pirValue);
delay(50); // so we can see on and off distinctly
}
PIR sensors can be a bit finicky, and if you've paid almost nothing for one then that is what you will get.
Any PIR sensor will report nonsense for a brief period after startup, so place your sensor in something like the circumstances in which you intend to use it, power it up and wait maybe even a few minutes, then play with giving it somethig you think it should sense and watch the RED LED.
And of course as @LarryD says, mind the punctuation. It may not have mattered in High School, but programming is ruthlessly dumb and everything counts.
The error you made is common, and hard to spot. You may want to go to the IDE preferences and turn up all warnings and verbosity - I'm pretty sure your mistake would have invoked a warning in red ink… a warning because what you did is entirety legal although only very rarely what was intended.
And… don't
loop(); {
call loop(), ever. It gets called for you automatically and repeatedly.