On hold, pending more testing, see reply #24
This one has me stumped.
I have a sketch and hardware which is a model of a turnstyle or turnstile if you prefer. I thought it was working perfectly and started tidying the code up to post in the gallery part of the forum. Changing some Serial.print statements in the process, caused a change to the logic.
I noticed that an LED came on: that particular LED represents an alarm state, where the style has been passed but it's still locked. When testing it earlier, if I did indeed force the style open against its electromagnet, the LED flashed as expected. Now it comes on when the style has not been breached. It comes on when, and only when, Locked and Pass are both true.
I've tracked this down to the Serial.print somehow changing the value of a variable, however odd that may sound.
In the code attached, two variables Locked and Pass are initialised as true and false resepectively; see the lines in the code marked //*****. The lines marked //XXXXX print the values to the monitor in setup. The screenshot named "good" shows that they are 1 (locked) and 0 (no pass has occured) respectively; so does the screenshot named "bad". Initialisation is correct.
In the "good" case, you'll also see the "12345678901234".
In the "bad" case, the "1234567890123" is one digit shorter, and although the variables are initialised correctly (the "10"), the Pass variable mysteriously got changed to a 1, as shown by the "11" lower down, which is a breach. The changed variable caused an "if" to fire which blinks the LED and gives the no payment message.
I literally change 12345678901234 (see line in code marked //<<<<<<<<<<<<<) to 1234567890123 and compile: the alarm led flashes and the "you didn't pay" message appears without my touching my hardware. Add the 4 to the 1234567890123, recompile, and the monitor shows correct behaviour.
Code and circuit attached.
For ease of viewing the code, parts are shown below, but the whole code is attached.
// declarations and includes
bool Locked = true; //*****
bool Coin = false;
bool Pass = false; //*****
Serial.println("Locked and awaiting coin....");
if (Locked && Pass) //Alarm
//alarm, reset Pass
Serial.println("*** Hey you didn't pay ***");
Pass = false;
Pin2 output to an led with resistor
Pin3 output to an led with resistor
Pin4 input (internal pullup) from the opto-int which sees the turnstyle is home
Pin5 output thru a resistor to transistor base; transistor has electromagnet on the collector, emitter to ground; magnet has flyback diode back to front and indicator led the right way round
Pin6 input (internal pullup) from click switch which simulates a coin deposit.
This one has had me tearing out what little hair I have....
Any ideas, even pure speculation, would be good.Please bear in mind
when considering this problem, that all I do is take one character out of a Serial.print and the problem occurs; put it back, problem goes away.
Program size, btw, is in the 4k out of 30+k range.
edit: changed circuit pic size to more managable.UPDATE to save you reading all the replies:
Took the **** out of the message
Took the bounce 2 library out
Changed prints to Serial.print(F(.... style
Changed bools to volatile bool
Changed 123... to abc.., fails at 13 letters, good with 14
Still the same, alas.