Arduino Script for Triggering Stops after ~50 sec. on Nicla Sense ME

Hi All,

I just purchased the Arduino Nicla Sense ME, which I (also) want to use for triggering.
For this I use the script below. The triggering should be 100 ms at ~3.3 V and then 100 ms at 0 V.

The script seems to work in the beginning, however, after ~50 seconds the triggering stops. This happens for both the GPIO0 and the GPIO3-pin. No error messages appear in the output-screen. I used both Arduino 1.8.15 and Arduino 2.0.0 for the uploading: both show this issue.

Has anyone of you seen issue this before? Can someone tell me how this can be fixed?

Thanks in advance.

Regards,
Harald

</>int n = 0;
float t_shutter = 100.0, t_off = 100.0;
bool state;
float currentTime, startTime;

void setup() {
pinMode(GPIO3, OUTPUT);
state = false;
currentTime = 0.0;
startTime = 0.0;
}

void loop() {
currentTime = millis();
if (state == false && (currentTime - startTime > t_shutter + t_off)){
state = true;
digitalWrite(GPIO3, HIGH);
startTime = millis();
}

if ((state) && (currentTime - startTime > t_shutter)){
state = false;
digitalWrite(GPIO3, LOW);
n = n+1;
}

delay(10);

}</>

float currentTime, startTime;
currentTime = millis();

millis() returns an unsigned long, not a float

delay(10);

Why are you mixing millis() timing and blocking code ?

Hi UKHeliBob,

Thanks for the reply.
I changed the currentTime and startTime to 'unsigned long' and I deleted the millis() from the code. However, the triggering still stops after ~50 seconds. Do you think it is in the code or might this be a problem with the Arduino?

Regards,
Harald

Most likely the code

Please post your sketch as it is now

Here it is (the triggering starts after 10 seconds (t_start_up) :

"int n = 0;
unsigned long t_shutter = 100, t_start_up = 10000, t_off = 100;
bool state;
unsigned long currentTime, startTime;

void setup() {
Serial.begin(115200);
pinMode(GPIO3, OUTPUT);
state = false;
}

void loop() {
if (currentTime > t_start_up){
if (state == false && (currentTime - startTime > t_shutter + t_off)){
state = true;
digitalWrite(GPIO3, HIGH);
startTime = millis();
}
}
else{
state = false;
digitalWrite(GPIO3, LOW);
}

currentTime = millis();
if ((state) && (currentTime - startTime > t_shutter)){
state = false;
digitalWrite(GPIO3, LOW);
n = n+1;
}

Serial.print(currentTime);
Serial.print(" ;");
Serial.print(state);
Serial.print(" ;");
Serial.print(n);
Serial.println(" ;");

}"

Thanks, but :

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Sorry:

int n = 0;
unsigned long t_shutter = 100, t_start_up = 10000, t_off = 100;
bool state;
unsigned long currentTime, startTime;

void setup() {
Serial.begin(115200);
pinMode(GPIO3, OUTPUT);
state = false;
}

void loop() {
if (currentTime > t_start_up){
if (state == false && (currentTime - startTime > t_shutter + t_off)){
state = true;
digitalWrite(GPIO3, HIGH);
startTime = millis();
}
}
else{
state = false;
digitalWrite(GPIO3, LOW);
}

currentTime = millis();
if ((state) && (currentTime - startTime > t_shutter)){
state = false;
digitalWrite(GPIO3, LOW);
n = n+1;
}

Serial.print(currentTime);
Serial.print(" ;");
Serial.print(state);
Serial.print(" ;");
Serial.print(n);
Serial.println(" ;");


Your code is missing the closing }

Leaving that aside, I have tried your code on a Nano and it appears to work OK and not stop after 50 seconds. However, you seem to have written a complicated sketch to do something simple.

If I understand correctly you want to turn an LED on for 100 milliseconds then off for the same period, and to repeat this. If so, then you have described the BlinkWithourDelay example. Even simpler, you do not need a state variable, you can just invert the state of the LED every 100 milliseconds

haralde, I have the same problem. It's actually 48 seconds. All GPIO pins, whether configured as inputs (with or without pullup) or outputs, stop functioning after 48 seconds. Did you find a solution?

Hi Gjennings,

Arduino fixed the issue themselves by updating their library.
With Arduino_BHY2 version 1.0.4 and Arduino_BHY2Host version 1.0.6 the problem was solved.

Regards,
Harald

I'm using Arduino_BHY2 v1.0.4 but still having the problem. Did your problem really go away?

#include "Arduino_BHY2.h"

void setup() {
  pinMode(GPIO0,INPUT);
  pinMode(GPIO1,OUTPUT);
  pinMode(GPIO2,OUTPUT);
  pinMode(GPIO3,OUTPUT);
  digitalWrite(GPIO1,LOW);
}

void loop() {
  digitalWrite(GPIO1,HIGH);
  digitalWrite(GPIO2,HIGH);
  digitalWrite(GPIO3,HIGH);
  delay(2000);
  digitalWrite(GPIO1,LOW);
  digitalWrite(GPIO2,LOW);
  digitalWrite(GPIO3,LOW);
  delay(2000);
}

Yes it did go away.

I used the following script:

#include "Nicla_System.h"
using namespace nicla;

unsigned long t_shutter = 10000, t_start_up = 0, t_off = 5000;
bool state;
unsigned long currentTime, startTime; 

void setup() {
  nicla::begin();
  Serial.begin(115200);
  pinMode(GPIO3, OUTPUT);
  digitalWrite(GPIO3, LOW);  
  state = false;
  currentTime = 0;
  startTime = 0;
}

void loop() {
  if (currentTime > t_start_up){  
    if (state == false && (currentTime - startTime > t_shutter + t_off)){
      state = true;
      digitalWrite(GPIO3, HIGH); 
      startTime = micros();
    }   
  }      
  else{
    state = false;
    digitalWrite(GPIO3, LOW);
  }
  
  currentTime = micros();
  if ((state) && (currentTime - startTime > t_shutter)){
    state = false;
    digitalWrite(GPIO3, LOW);
  }
  
  Serial.print(currentTime);
  Serial.print(" ;");    
  Serial.print(state);
  Serial.print(" ;");           
  Serial.print(n);
  Serial.println(" ;");   
}

Hey @gjennings66, through testing and by looking at the pins with a logic analyser, it seems that if you do not include the Nicla_system library and the "nicla::begin();" in your code, every time of connection will stop after circa 50 seconds.

It is related to how the bootloader has been implemented in my opinion. Nevertheless adding those 2 lines, fixes the problem.

#include "Nicla_System.h"

void setup() {
  nicla::begin();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.