need help with photo cell code

can anyone help me to edit my code so that the photocell detects the changes constantly,
but not having a set time for the next action. (so I can avoid copy&paste the same code over-and-over again)
Thanks!! :slight_smile:

int greenPin = 2; 
int greenPintwo = 3; 
int yellowPin = 4; 
int yellowPintwo = 5; 
int servoPin = 7; 
int timer = 0; 
int timer2 = 0; 
int photoCell = 0; 
 
void setup () {  
beginSerial (9600); 
pinMode (greenPin, OUTPUT); 
pinMode (greenPintwo, OUTPUT); 
pinMode (yellowPin, OUTPUT); 
pinMode (greenPintwo, OUTPUT); 
pinMode (servoPin, OUTPUT); 
}  
 
void loop (){ 
photoCell = analogRead (0); 
while (photoCell < 600) { 
   
      while (timer < 6000) { 
      digitalWrite (greenPin, HIGH); 
      digitalWrite (greenPintwo, HIGH); 
      timer +=10; 
      } 
      photoCell = analogRead (0); 
      while (timer2 < 6000) {  
      digitalWrite (yellowPin, HIGH); 
      digitalWrite (yellowPintwo, HIGH); 
      timer2 +=10; 
      } 
/* 
want to have the photocell constantly checking but not counting for the time 
*/  
      photoCell = analogRead (0); 
      digitalWrite (greenPin, LOW); 
      digitalWrite (greenPintwo, LOW);    
      }      
 
      while (photoCell > 600) { 
      digitalWrite (yellowPin, LOW); 
      digitalWrite (yellowPintwo, LOW); 
      } 
   
}

edit: I have edit some mistake in the code.

I posted a piece of my "educational code" in the Wiring forum last year. It's about building very basic "multitasking capabilities", avoiding delays and while loops. Is this what you're looking for?

http://wiring.org.co/discourse/yabb/YaBB.cgi?board=Interfacing;action=display;num=1131209589;start=1#1

Thanks for your reply :D However, I don't understand what your code means (sorry I'm new to do coding) If possible, I would like to specify how I want to do with the code.

I have a photocell that checks for light turning on/off, during a stage of time, it allows different LED light to shine in a fixed time.

But at the part where I put the comment on, I like to have the photocell checking for the changes constantly, then decide what kind of light is going to fade out (instantly).

hope this clearify more. Thanks~ :-[

I like to have the photocell checking for the changes constantly, then decide what kind of light is going to fade out (instantly).

I guess I got your problem right. The Wiring program I provided doesn't really do funky things code-wise, it's just an almost empty framework that might give you a clue about how to design your code structure.

When doing time-based stuff like LED fading, a simple approach is to do it in a loop. But as the necessity comes in to perform "real-time" sensor checks simultaneously, such a loop takes way too much time. Nothing is going to happen before you're done with that particular loop. To solve this problem, granulate all the time based processes you have in your program. From the main program loop, call methods that each perform tiny steps of longer processes.

In your case, a pseudo-code description goes like this:

loop
{
checkSensors
manageLights
}

checkSensors
{
storeCurrentValueReadFromPhotoCell
}

manageLights
{
setNewStateAccordingToStoredSensorData
...
(yeah... fill in all your conditional statements)

seeWhatTimeIsIs
performAnotherStep
incrementACounterOrSetATimeStamp
}

Within your animation management methods, keep track of time using incrementing counters or time-stamps. With that said, please look at my example again. I'm pretty sure you're going to understand what it's for now.

Thanks for your reply! Just wonder where should I insert your pseudo-code in? : )

Thanks for your reply! Just wonder where should I insert your pseudo-code in? : )

Ahem... Thought you might be better off figuring some things out on your own (future-wise and such). Try re-structuring your code according to the suggestions above... if you run into problems, post your new cleaned-up code and I'll be back in in a couple of hours. Okay?

oops sorry I forgot to update the forum, Thank you very much for all the help! I have figure out how the program works :)