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