Combing to sketches

Hi
i have tried to combine 2 sketches. Fist one is from Spark Fun. It is there demo sketch of there 4 digit 7 segment display which is a counting sketch. 1 to 9999. Second is an LDR sketch, lazer trip wire. I would like to put the lazer across a door and every time the beam is broken the 7 seg display will count up 1. I have both sketches running at the same time on the same ATmega328, it is counting up and i can break the beam and read the LDR value in the serial monitor. I just haven't been successful at making the LDR sketch do the counting. One zip file is labeled People Counter and the other is labeled LDR as a switch. Also if i haven't given enough info please ask i will include what ever is needed.

If anybody would be able to guide me to combine these 2 i would be greatfull and i will buy you lunch next time you are in my neighborhood.

Peoplr_Counter2_2.zip (2.56 KB)

LDR_as_Switch1.zip (587 Bytes)

I have both sketches running at the same time on the same ATmega328

That is not possible. Only one sketch at a time can be running on an Arduino.

I just haven't been successful at making the LDR sketch do the counting.

Where is your attempt at combining the two sketches?

I'll throw in my 2 cents.

Not sure what you mean here:

I have both sketches running at the same time on the same ATmega328

As for how to combine the two sketches, I can see the people counter sketch reads an analog value and sends it to the PC, plus it turns on or off a led, while the display part just display current millis()/1000.

IMHO you have to add two things: a counter variable and a "previous state" variable. In rough pseudocode:

if (analogRead() > threshold) then
    currState = HIGH
else
    currState = LOW
endif
if currState != prevState and currState == LOW then // LOW or HIGH depending on the circuit
    counter++
    prevState = currState
endif

Finally, pass the counter value to the displayNumber function.

You might have to decouple the timing of analogRead() from that of displayNumber(). For this, have a look at blink without delay example.

Thanks for the reply!

Yes i did't say that quite right. Not 2 sketches but 2 "operations". It is counting up the display and also the LDR is reading, monitored on the serial monitor and #13 LED blinking. But they are not "interacting". I need to remove the mills and replace it with advance by 1 from the LDR, i guess.

I will try the suggestion from tuxdiuno. I should put that code inplace of the mill?

I will try the suggestion from tuxdiuno. I should put that code inplace of the mill?

Now you have this:

displayNumber(millis()/1000);

I think you should have this instead:

displayNumber(counter);

Of course you have to declare counter and increment its value when you detect a change in the analogRead() value.

It's also very important that you understand how to get rid of delay()s in the code. Hint: blink without delay example.

OK Thanks for your help. I will try.