Go Down

Topic: Serial Monitor needs to be open to for sketch to work properly (Read 820 times) previous topic - next topic

Marciokoko

I have this sketch which works fine, it sends data over serial ble from a Bluno Nano board to an iOS app.  However I have this issue reported here (https://arduino.stackexchange.com/questions/41717/arduino-uno-only-works-with-monitor-serial-opened) where data sent to the ios from the ble board will not show up until the serial monitor is opened up.

This of course is not a solution because the project is a standalone project and I cant keep the board connected to the computer.  So I followed the OP in that thread's recommendation which is to connect a 10uF cap between GND and RST pins.  Alas this didnt work.  Any ideas what might work?






terryking228

Hi,
Running the serial port  executes code in your Arduino that otherwise would not run.

This does change the internal timing of your sketch.

I have seen a problem like this but it was about 5 years ago ( Ancient Arduino History) and I don't remember the details.

What is time-critical in your code?  You might try adding small (1 to 5 ms) delays in a few places to see if that changes the problem..
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Marciokoko

Nothing critical...any particular place?  So like setup (run once) vs loop? 

Robin2

I have this sketch which works fine,
You have been around here long enough to know to post your program without having to be reminded.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Marciokoko

Robin,

Well it seems to be a Serial Monitor issue independent of the sketch, but here it is:

Code: [Select]

/*
Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev
*/
Measure the liquid/water flow rate using this code.
Connect Vcc and Gnd of sensor to arduino, and the
signal line to arduino digital pin 2.


byte statusLed    = 13;
byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

void setup(){
  Serial.begin(38400);
  
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop(){
   if((millis() - oldTime) > 5000) {   // Only process counters once per second
    detachInterrupt(sensorInterrupt);
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    
    totalMilliLitres += flowMilliLitres;
    unsigned int frac;
    frac = (flowRate - int(flowRate)) * 10;

    sendOverBLE();
    pulseCount = 0;
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

void pulseCounter(){
  pulseCount++;
}
 
void sendOverBLE() {
  //int mystring;
  //mystring = int(454);
    Serial.println(totalMilliLitres);
    Serial.println();   //print line feed character
}

pylon

Quote
I have this sketch which works fine, it sends data over serial ble from a Bluno Nano board to an iOS app.  However I have this issue reported here (https://arduino.stackexchange.com/questions/41717/arduino-uno-only-works-with-monitor-serial-opened) where data sent to the ios from the ble board will not show up until the serial monitor is opened up.
The problem discussed at stackexchange is something different. There the problem was just a timing issued. If the poster there had waited for about 1 second after opening the serial interface everything would have worked.

But after checking the board you're using (it's able to be programmed by the BTLE connection) I think you probably have the same problem. If you open a BTLE connection the Arduino is reset and needs some time to jump over the bootloader and get the data you're trying to read. This might be a to long time frame for the app that tries to read it.
If you have the Serial monitor on the USB connection open, the DTR line is already pulled low, so no new reset pulse reaches the processor and the sketch continues to run.

BTW: your pulseCounter() routine is never called. I guess you're missing an attachInterrupt() call or something similar.


Robin2

Well it seems to be a Serial Monitor issue independent of the sketch, but here it is:
Does that mean you have other programs that have the same problem?

By the way you have not posted a complete program. There is no call to the function pulseCounter(). Maybe the problem is in the stuff you have omitted.


Have you got the Bluetooth device connected to Pins 0 and 1?
And have you also got the USB cable connected between the Nano and the PC? - If so, try disconnecting the USB cable.

Alternatively use SoftwareSerial so you can put the Bluetooth device on some other pins.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pylon

Quote
Alternatively use SoftwareSerial so you can put the Bluetooth device on some other pins.
The Bluetooth device is onboard, so that may be hard to follow.

Marciokoko

Sorry about the sketch.  I posted a backup copy because I was at the office and. It at home and indeed it's missing the pulsecounter function which simply adds 1 to the pulse.  I'll update the code shortly.

The bluno nano board has the serial uart on pins 0,1 from what I understand.

So I'm still unclear how to solve the issue.  If u run the sketch from the bluno connected to the USB of my laptop, then open the iOS app, the data comes in perfectly.  But if I plug the bluno to a battery pack using the USB cable I get null in the iOS app.  If I then switch back to the laptop and open the serial monitor I get the data.

So right now I'm try a 1s delay before the sendOverBLE call and film everything and post back:

Ok I modified the sketch above to my actual sketch, except some comments i had.  Here is the video of how I run it at present:

https://youtu.be/VwdAZ_SA_ao

Robin2

The Bluetooth device is onboard, so that may be hard to follow.
I had not realized that until now (slapped own wrist). I don't know anything about the peculiarities of that board - I will retire from this discussion.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Marciokoko

Ok its obviously something that happens when the ble connection is made.  As pylon said, the arduino resets after a ble connection is made.  But what does that have to do with the serial getting null instead of my data?  Id like to understand that in order to see what I could do to fix it.

Because maybe I could modify my sketch so that it starts running the code I need after I send a certain character through serial that would jumpstart it out of sending (null) and back to sending the data it sends when SM opens.

pylon

Quote
Ok its obviously something that happens when the ble connection is made.  As pylon said, the arduino resets after a ble connection is made.  But what does that have to do with the serial getting null instead of my data?  Id like to understand that in order to see what I could do to fix it.
It's a question of timing. If you open the BTLE connection wait for one second before you send anything to the Bluno. Better: just keep the BTLE connection open all the time so the device does not reset.

Depending on the real code it might be an easy change but you probably have to change the smartphone app too to make it work.

Marciokoko

The iOS app is not sending to the bluno, the bluno is sending to the iOS app.  The smartphone app is the one receiving.

I've added a 1500 delay before sending out through the ble but it didn't do any good.

I could modify the iOS app but I'd have to know what it is I need to add to it.  What puzzles me is, if the problem is that the Arduino resets when the ble connection is made, why would that pose a problem?  I send data from the loop so even if it resets, wouldn't it run my send data code again?  Iow, what causes the null?

Marciokoko

I just tried it with a different app on Android and it happens similarly...


Coding Badly


Go Up