Show Posts
Pages: 1 ... 33 34 [35] 36 37 ... 294
511  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 09:27:33 am
This is getting really weird... as I add and remove debug prints, the problem changes. That original string 1234567890123/4 needs to be shortened as I add debugs elsewhere. That makes me wonder if it's not something to do with the total data sent as Serial.print.

So gents thanks for your input: I've tried all the suggestions to no avail. What I'll do now is change the thread title to "on hold" until I can experiment fully with debugs here, there and everywhere and try to narrow it down.

I'm travelling out of town next week so I "might" take Arduino with me for evening entertainment...

All I feel confident to say now is that when I had that string 1234567890123 I had a problem, when I added the 4, it went away.

Thanks for the assistance: I'll revert when I test further.
512  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 08:57:31 am
Can you post the code you had when you removed the debounce stuff?



Code:
// declarations and includes
volatile bool Locked = true; //*****
volatile bool Coin = false;
volatile bool Pass = false; //*****

byte alarmLed = 2;
byte thanksLed =3;
byte passPin = 4;
byte passPinState;
byte lastPassPinState = 1;
byte magnetPin = 5;  //actually base of npn via resistor, but it's for the magnet
byte coinPin = 6; //mech switch so needs debounce
byte coinPinState;       
byte lastCoinPinState = 1;

/*
#include <Bounce2.h>
 Bounce dbCoinPin = Bounce();
 */

void setup()
{

  Serial.begin(9600);
  //char* myString = "abcdefghijklmn";
  Serial.println("12345678901234"); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

  Serial.print(Locked); //XXXXX
  Serial.println(Pass); //XXXXX
  Serial.println(F("Locked and awaiting coin...."));


  pinMode(alarmLed, OUTPUT);
  digitalWrite(alarmLed, LOW);

  pinMode(thanksLed, OUTPUT);
  digitalWrite(thanksLed, LOW);

  pinMode(passPin, INPUT_PULLUP);

  pinMode(coinPin, INPUT_PULLUP);

  pinMode(magnetPin, OUTPUT);
  digitalWrite(magnetPin, HIGH);  //starts locked

  // After setting up the buttons, setup debouncers
  /*
  dbCoinPin.attach(coinPin);
   dbCoinPin.interval(50);
   */


  Serial.println("End of setup");
  Serial.println();

}


void loop()
{
  // see if either event (Coin or Pass) has occured
  checkCoin();
  checkPass();

  // then do the transitions based on the events

    doTransitions();

}

void checkCoin()
{
  //set Coin if coin has been deposited
  /*
  dbCoinPin.update();
   coinPinState = dbCoinPin.read();
   */

  coinPinState = digitalRead(coinPin);

  if ((coinPinState != lastCoinPinState) && coinPinState == LOW) //changed and low means just got pressed
  {
    Coin = true;
  }
  lastCoinPinState = coinPinState;
}


void checkPass()
{
  //set Pass if boom has been opened and closed
  //Serial.print(Pass);
  //Serial.print(passPinState);
  //Serial.print(lastPassPinState);
  passPinState = digitalRead(passPin);
  if ((passPinState != lastPassPinState) && passPinState == HIGH) //changed and HIGH means just got interrupted
  {
    Pass = true;
  }
  lastPassPinState = passPinState;
  //Serial.println(Pass);
  delay(1000);

}



void doTransitions()
{
  if (Locked && Coin)  // Unlock
  {
    //unlock, reset coin
    Serial.println(F("Unlocking, come in"));
    digitalWrite(magnetPin, LOW);
    Locked = false;
    Coin = false;
  }

  if (!Locked && Coin) //ie it was already unlocked, so Thanks
  {
    //thankyou, reset coin
    Serial.println(F("  You paid already"));
    digitalWrite(thanksLed, HIGH);
    delay(250);
    digitalWrite(thanksLed, LOW);
    Coin = false;
  }

  if (Locked && Pass)  //Alarm
  {
    //alarm, reset Pass
    Serial.println(F("Hey you didn't pay"));
    Serial.print(Locked); //XXXXX
    Serial.println(Pass); //XXXXX
    digitalWrite(alarmLed, HIGH);
    delay(500);
    digitalWrite(alarmLed, LOW);
    Pass = false;
  }

  if (!Locked && Pass) //ie it was already unlocked, Lock
  {
    //lock, reset Pass
    Serial.println(F("Welcome. Locked again"));
    digitalWrite(magnetPin, HIGH);
    Pass = false;
    Locked= true;
  }
}















513  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 08:40:08 am
How do you know that function checkPass( ) isn't setting Pass to true ?

Have you tried putting debugging code in checkPass( )  to see if that is happening ?

Good point: I tried that and yes checkPass IS setting Pass to true.

But that just moves the problem to a different variable, and I'll check further to see what's causing the "if" in checkPass to fire with one string in setup() and not fire with the other, longer one. That goes to the variables passPinState and lastPassPinstate
514  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 08:31:31 am
perhaps one more thing to try

Code:
char* myString = "12345678901234";
Serial.println(myString);

Nope, no joy with that
515  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 08:24:24 am
Is  Serial.print( bool )  perhaps the culprit ?    It is a bit unusual to do that.

You mean as in Serial.print(Pass)? That was part of my debugging when the problem showed up, to check that the variable had changed, to verify that was the cause of the "if" firing. But I'll take it out....
516  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 08:04:31 am
Yeah the reason I put that 123... in was that the problem manifest with my original de-bug strings, printing text in appropriate places to see where the code was taking me. When I had narrowed down the problem, I put the 123.... in so I could easily see at which character it fell over.

So the numbers aren't the problem I'm sure, but wth, I'll try ABCs

EDIT... same hassle with 13 letters, goes away with 14th
517  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:57:17 am
By the way, I can live with this, this is an academic exercise, so if the problem is gone with a "4" on the end of the print then so be it. BOTOH I hate not understanding what's going on......
518  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:55:16 am
Code:
volatile bool Coin = false;

Thanks for the suggestion: tried that and no joy  smiley-yell
519  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:43:54 am
Changed all prints to Serial.print(F(..... , no change.

edit: no change in behaviour I mean; it reduced the sketch from just over 4000 to just under 3900
520  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:40:42 am
The only time I have seen fails like this is due to interrupts or lack of SRAM, but I don't see where you are using that much SRAM in your sketch.

Have you tried the F() function as a test? I would try it with all your static strings to see if that helps.
Code:
 Serial.println(F("1234567890123")); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Sorry Tim, I should have said I tried the "F" way too, but I'll check again.

What I find really odd, is that the shorter string gives the problem, add a character and it's back to good behaviour.
521  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:31:59 am
Nope it's not the bounce2 library. Removed all references to it, changed to read the switch with a normal digitalRead. (Which now reads 2 for 1 so it really needs a debounce  smiley-cool )

Still the same issue.
522  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:24:40 am
Where did you get the Bounce2 library? Does it use interrupts? The Serial functions use interrupts. Have you tried your code without the Bounce2 library?


Ah, now that's a point. I'm pretty (90%) sure it was linked from the playground.

I'll try without it right away. It would be a pity if I had to remove that, it 's really so simple to use.

I'll be back very soon with news on that test ST.
523  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:19:52 am
Don't print non alpha numerical characters. It's a known bug

    Serial.println("*** Hey you didn't pay ***");

Ok I'll try that but:

1) I think I added the **** after the problem occured and
2) The codse shouldn't be going there in the first place. It's the spurious change in the Pass bool from false to tru that sends the code there to print that line.

edit.... still the same, see pic
524  Using Arduino / Programming Questions / Re: How can Serial.print() affect my sketch's logic? on: July 13, 2014, 07:04:27 am
If you posted your entire sketch as you are asked to do, we might have a chance
of figuring out the problem.  As it is we have nothing to go on.

It's attached to the post... I said so in the post. Just gave a few snippets for ease of reference.
525  Using Arduino / Programming Questions / ON HOLD How can Serial.print() affect my sketch's logic? on: July 13, 2014, 06:59:42 am
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.

Code:
// declarations and includes
bool Locked = true; //*****
bool Coin = false;
bool Pass = false; //*****


Code:
void setup()
{

  Serial.begin(9600);
  Serial.println("12345678901234"); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

  Serial.print(Locked); //XXXXX
  Serial.println(Pass); //XXXXX
  Serial.println("Locked and awaiting coin....");

Code:
if (Locked && Pass)  //Alarm
  {
    //alarm, reset Pass
    Serial.println("*** Hey you didn't pay ***");
    Serial.print(Locked); //XXXXX
    Serial.println(Pass); //XXXXX
    digitalWrite(alarmLed, HIGH);
    delay(500);
    digitalWrite(alarmLed, LOW);
    Pass = false;
  }

Circuit description:
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.

Jim

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.
Pages: 1 ... 33 34 [35] 36 37 ... 294