Photocell Issue

Good Evening Arduino Gurus! Below is my question which I hope someone can solve. I have several LEDs connected via 3 shift registers and 2 photo resistors attached. My code works as coded the LEDs change as expected however when the second photo resistor changes the LEDs the first photo resistor is uncovered and one of the LEDs continues to flicker. Is there a way in my code to fix this or do I have to add some more hardware? Thanks in advance!

int photocellPin       = 19; // Connected to Arduino
int photocellPin2      = 18; // Connected to Aruino

int lowThreshold  = 580;
int lowThreshold2 = 570;

void loop(){

int sensorValue  = analogRead(photocellPin);
int sensorValue2 = analogRead(photocellPin2);
Serial.println(sensorValue);
Serial.println(sensorValue2);
delay(200);

if (sensorValue < lowThreshold){
setRegisterPin(14, LOW);
setRegisterPin(12, HIGH);
writeRegisters();      // MUST BE CALLED TO DISPLAY CHANGES
  // Only call once after the values are set how you need. 
} else {
setRegisterPin(12, LOW);
setRegisterPin(14, HIGH);
writeRegisters();      // MUST BE CALLED TO DISPLAY CHANGES
  // Only call once after the values are set how you need. 
}

if (sensorValue2 < lowThreshold2){
setRegisterPin(13, HIGH);
setRegisterPin(14, LOW);
setRegisterPin(11, LOW);
setRegisterPin(9, HIGH);
writeRegisters();      // MUST BE CALLED TO DISPLAY CHANGES
  // Only call once after the values are set how you need. 
} else {
setRegisterPin(9, LOW);
setRegisterPin(13, LOW);
setRegisterPin(11, HIGH);
writeRegisters();      // MUST BE CALLED TO DISPLAY CHANGES
  // Only call once after the values are set how you need. 
}

}

however when the second photo resistor changes the LEDs the first photo resistor is uncovered and one of the LEDs continues to flicker.

Nowhere in your code is there anything call "second photo resistor". Using names from your code makes more sense than handwaving.

It does not make sense that turning some LEDS on will "uncover the first photo resistor". Please explain.

Which one of the LEDs continues to flicker? PM me, and I'll send you an address, so you can ship me your Arduino and other hardware, so I can see which one it is.

Ok here is a bit more explanation:

PhotocellPin2 sets Register Pins 14 & 11 LOW and 9 & 13 HIGH however 14 still flickers on and off when in the LOW state as it is still being controlled by PhotocellPin...I need to know how to keep that from happening. Thanks.

PilotinControl:
Ok here is a bit more explanation:

PhotocellPin2 sets Register Pins 14 & 11 LOW and 9 & 13 HIGH however 14 still flickers on and off when in the LOW state as it is still being controlled by PhotocellPin...I need to know how to keep that from happening. Thanks.

Why are some LEDs controlled by two photocells?

If that is really necessary, you need three blocks of code, to deal with with LEDs controlled by one sensor, to deal with the LEDs controlled by the other sensor, and to deal with the LEDs controlled by both sensors.

Yes it is necessary....Model Railroad Signalling....when a train passes over one photocell it changes the aspects of the signals in other blocks on the track. Signal aspects are Red Yellow Green....all LEDs are set to Green by default. When the train passes over the photocell it changes the Green to Red....when it passes the second photocell it changes that signal to Red and the previous signal to Yellow....however the Green flickers....so yes I need code to handle that....and thats where I'm stuck. Thanks

Are your photocells reacting to ambient light? Have you tried lowering the thresholds in small steps? You might do better using magnetic sensors and sensing the wheels.

Hi,
If you stop the train over the LDRs does the problem stop?
Is it caused by the flickering of light between the carriages as they pass over the LDR?

Tom.... :slight_smile:

LED 14 flickers low intensity rapidly when photocell2 is covered and returns to HIGH state when photocell2 is uncovered

In loop(), you have independent if statements. Either one can turn on the LED. So if one tests true and the other false, it will continually blink on and off.

Hi,
Are you using LDRs or photocells?
How have you got them connected to the arduino?
Have you got bypass caps on each one and how long are the wires fro the LDR I think you have and the controller?

Tom.... :slight_smile:

Thanks Aarg for pointing that out. Yes each IF Statement controls 3 LEDS and LED 14 is still being controlled by Photocell while Photocell2 is trying to control it. I need to add some conditional code...thats where I am stuck. Are you able to show me Aarg?

Now to answer Tom: I am using photoresistors....did not know there was a difference between LDRs...they both look the same to me. They are connected using the digital outputs on the arduino I do have 10k ohm resistors attached and the wires are the short jumpers from the cell to the arduino.

Hi,
Okay, you are using photoresistors, photocells are small PV cells that produce their own current when in light, solarcell.
Have you got bypass caps on the inputs to minimise electrical noise?

Tom... :slight_smile:

Hello Tom,

Actually no I do not have bypass capacitors connected. How many should I be using? 1 for each photoresistor and where should I connect it to? In all my Arduino projects to date I haven't had a need for such a thing. I guess I do now. This would eliminate the constant flickering? If not it will help for sure. I still think it is because photocell and photocell2 is trying to control the output at the same time to LED 14 and cannot as Aarg stated....I need to figure that out too. Thanks

Hi,
0.1uF between arduino input and gnd.
It may help..
Tom... :slight_smile:

So that would be a total of 2 capacitors...1 for each input.

Hi,
Yes one cap for each input.
I have attached a diagram of the logic that I hope is what you are trying to accomplish.
I think you should be looking for input transitions, not input states, to trigger your signal outputs.
What pins do you have connected to which signals and colours?
Feel free to edit it, its in jpg format.

Tom.... :slight_smile:

Pins 12 (Red), 13 (Yellow), 14 (Green) are connected to LDR 1 and Pins 11 (Green), 10 (Yellow), 9 (Red) are connected to LDR 2.

Pins 14 and 11 are defaulted to Green. When LDR 1 is covered Green (14) changes to Red (12) and when LDR 2 is covered Green (11) changes to Red (9) and Green (14) should change to Yellow (13), which it does, however Green (14) flickers rapidly and it should be off. However since LDR 1 is still trying to change Green (14) back to its original state. That is why I am assuming that is where the flickering is coming from...one IF statement is telling Green (14) to be HIGH while another IF statement is telling Green (14) to be LOW. They are contradicting each other.

I believe you have a State problem.
Unrelated code snipped out for clarity...

if (sensorValue < lowThreshold){

} else {
setRegisterPin(14, HIGH);

;

then...

if (sensorValue2 < lowThreshold2){

setRegisterPin(14, LOW);

Resulting in 200mS flicker when sensorValue2 is dark.

My suggestion, start over.
Using a ye-olde pencil and paper layout boxes that defines the state of each LED and what causes LEDs to change.
Also give the register pins and their state names...

Instead of...
setRegisterPin(14, LOW);

Try this...
#define ON LOW // Maybe the way you wired these LOW is OFF. If so reverse these.
#define OFF HIGH

const int GreenLED = 14;
setRegisterPin(GreenLED, ON); // So easy to tell what will happen!

States: (Example)

  1. Powerup
    a. Entry into State: // Create a function to toggle all LEDs. Only performed once
    All_LEDs(ON)
    Delay 1 second to verify all LEDs are working.
    All_LEDs(OFF)

GreenLED ON

b. Change conditions: sensorValue < lowThreshold
Chage to state: 2

  1. First sensor tripped // Give it a meaningful name
    a. Entry into state: All_LEDs(OFF) // Only performed once
    RedLED ON

b. Change conditions: ?

  1. Another state
    a.
    b.

Once you have all the States and change conditions worked out then it is far less troublesome towrite the code. I highly recommend the State Machine library.

PS I rarely visit the site during the Summer, so this message is a guiding light rather that a collaborative effort. Have fun!

TomS...no the capacitors did not help...although George maybe onto something...however instead of rewriting the code...isnt there a workaround I can do with the IF statements?