Possible Coding issue

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.

I'll go first. Show an as-built schematic (what is in front of you, not a screenshot/copy from somewhere else). Hand-drawn is ok.

You could maybe try out your code on WOKWI and see if it works there.

1 Like

Not enough information. Please post a wiring diagram, with component types and values indicated. Hand drawn is fine.

Please use code tags when posting code. There is no good reason to avoid linking to source material, i.e. the youtube video.

1 Like

What is connected to pin A0 and how is it connected?

1 Like

Please edit your opening post, select all code and click the <CODE/> button; next save your post.

This will apply so-called code tags which make the code easier to read and copy and the forum software will display it correctly. See https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems

1 Like
1 Like

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".

Here is a code-version that prints to the serial monitor in a special way.

Only in case the variable has changed its value
one single line is printed
that looks like this

lightVal changed from 0 to 347

in case the value has really changed.
otherwise nothing is printed.
This means all changes appear but as long as nothing changes nothing is printed

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

int lightPin = A0;
int lightVal;
int dv = 300;
const byte redPin   = 7;
const byte greenPin = 8;

void setup() {

  Serial.begin(9600);
  Serial.println("setup-Start");
  pinMode(lightPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {

  lightVal = analogRead(lightPin);
  //Serial.println(lightVal);

  // only print ONE time 
  // "lightVal changed from 'old-value' to 'actual-value'
  // in case the value has CHANGED since last printing
  // effect: print only ONCE if value has changed 
  dbgc("LDR",lightVal);
  delay(dv);
  
  if (lightVal > 250) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
  };

  if (lightVal < 250) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
  }
}

Hi, @addesigns
Welcome to the forum.

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.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia: