Edit: I am currently unable to upload a picture of my breadboard connections do to the fact that I joined this community/forum in the last 24 hours. I will continue to try over the next few days to see what the time limit is. I appreciate the quick responses and look forward to learning more in this journey.
Currently following Paul McWhorter's videos on YouTube. I am having trouble understanding why my setup is not working. Keep in mind, I have tested to make sure I do not have an faulty LED's and everything is good. I am not sure if there is a coding issue since the videos are about 5 years old. I can't image that is the case.
int lightPin=A0;
int lightVal;
int dv=300;
int redPin=7;
int greenPin=8;
void setup() {
// put your setup code here, to run once:
pinMode(lightPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
lightVal=analogRead(lightPin);
Serial.println(lightVal);
delay(dv);
if (lightVal>250) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
};
if (lightVal<250) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
Purpose of this is to have the photocell switch between red of green depending on the input value. The problem is the green LED is always on no matter what. The board is connected properly. I have switched the red and green LED pins with each other and then the red is on permanently. I am pulling out my hair trying to figure out what I am missing and I am hoping someone can point me in the right direction.
Your code works. You might have a wiring problem, but probably the ambient light is pushing the LDR over 250. The LEDs should have their Cathode leg tied to ground and their Anode leg tied to the pin you designated. The analog input of the LDR probably ranges from 0 to 1023, so "250" might be too low to be anything but "on"... so change "250" to "512".
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.