I've made a coin bank with a slide potentionmeter and a photoresistor. So when the coin passes through the hole, it moves the potentiometer's slider so as to measure the coin's diameter. Also, in order to verify that the coin really falls into the coin bank, there is a photoresistor attached in the middle of the coin bank's body with a led on the other side. So when a coin gets in, the potentiometer value changes (and the peak value is saved in a variable, let's call it A) and also the photoresistor's value drops (and the minimum value is also saved in a variable, let's call it B). And if both the values of the two variables are acceptable, a led gets on (almost).
The problem is: if either the pot or the photoresistor's value changes independently and after much time the other's value changes also, the led does light up (since the peak values remain saved in the variables A and B).
The question is: can I write a simple code that clears these variables (A and B) after some seconds?
Please take in mind that I do not have much knowledge about programming, so, please, be clear.
and the peak value is saved in a variable, let's call it A
Why? What possible excuse is there for using such a meaningless name? Why NOT call it peakValue?
I can think of several reasons, one of them being that the OP's mother tounge is not English and he/she would rather not have variable names with English language meanings. The choice of A may just be for your benefit!
PaulS:
and the minimum value is also saved in a variable, let's call it B
Let's not.
Is it really too difficult for you to use A and B?
Mikos_Mikekos: The problem is: if either the pot or the photoresistor's value changes independently and after much time the other's value changes also, the led does light up (since the peak values remain saved in the variables A and B).
The question is: can I write a simple code that clears these variables (A and B) after some seconds?
Please take in mind that I do not have much knowledge about programming, so, please, be clear.
You would probably get this done with a state machine and a timer. The states: IDLE, POTSENSE, LIGHTSENSE, BOTHSENSE, TIMEOUT. Have a variable hold the current state. Let the default state be IDLE. The transitions would be
IDLE => POTSENSE when the A and B variables change to sense a coin. Start timer here!
IDLE => LIGHTSENSE when the photoresistor senses a coin. Start timer here!
POTSENSE => BOTHSENSE when the photoresistor senses a coin. Cancel timer here!
LIGHTSENSE => BOTHSENSE when the A and B change to sense a coin. Cancel timer here!
BOTHSENSE => IDLE always. Clear A and B here!
=> TIMEOUT when the timer fires. Clear A and B here!
TIMEOUT => IDLE always.
Yes but how that timer works in the end. Can you give me an example code. The problem is that a can't find an easy way to count the time from a given point.
Also, the reason why I chose A and B is so you could understand me more easily and the reason why I didn't show the full program because it is quite big and only a part of it is useful for you. And indeed, my first language is not English