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