Is my Arduino ok?

Hi :slight_smile:
I have some moderately annoying issues I was hoping you guys might be able to help me with. my Arduino NANO 33 IOT doesn't seem to work properly.

the first issue is that nothing the Arduino prints to the serial port shows up until I send some text over back to it using the serial monitor. my code works I'm fairly certain. it starts just fine. accepts input as it should. and prints all the text its supposed to After I send the first text to it over the serial monitor. but any print commands run before the first

secondly. reset doesn't work properly? if i press the button on the device itself, it just stops. and if i reset it in software. i get a similar result. the device just freezes. the only way to reset the device now is to reupload the code from the IDE. kind of a problem since I plan on copying the code to several other Arduinos and use them as trackers. is this normal? do i need to do something to cause reset to work?

note, the Arduino was bought from RS components.

to help explain the first point. here is the code I'm currently trying to run. note that both these problems seem to happen with everything I write. although I have not tested external code.

the code uses a state variable to control where it is and what its doing. as you can see. it starts in state 1000. its boot phase. here its supposed to try and connect to a network (not implemented yet). and print some text to serial monitor just to show the user its up and running. then it sets itself to state 1001. its idle phase where its waiting for commands from the user. as mentioned. it accepts commands perfectly well. and prints the appropriate responses after that. but the text "Arduino tracker system v0.1" never shows up unless i manually set the state to 1000 using a command. in which case it shows up just fine (presumably because I've now sent text over the serial monitor to the Arduino and its now speaking fine).

the state 1000 boot phase btw was an attempt to deal with the fact that i was getting nothing out of the serial.print commands put into void setup(). which also don't produce anything.

is this normal? am i just being stupid or is there something wrong with my Arduino? its always been like this btw.

#include <SPI.h>
#include <WiFiNINA.h>
int SBS = 16; // Serial buffer size, controlls how much of the serial buffer is read, max is 64
int CC = 7; // command count. how many commands there are in total
// note, for the command index below the first dimention needs to match the nummer of
// commands, while the second needs to match the serial buffer size

char commandIndex[7] [16] = { "T", "restart", "devicecfg", "ipconfig", "debug", "ping" "edit"};

// note, state is the most important value in the program. it controlls program flow
// and ensures the correct code gets execuded in the correct order. the start state is 1000
// with each subsequent subroutine getting its own state. command decoding for example is 1010.
// these values MUST NOT be modified without knowing what youre doing.

int state = 1000;
int lastState = 0;

char commandRegister[16];// <- this too needs to match the size of the input buffer
int commandIndexLookup = 0; // <- the command currently being checked.
char userInput[16]; //<- also needs to be the same as the serial buffer size.
void(* resetFunc) (void) = 0;  // declare reset fuction at address 0
// system variables
//==============================================================
// command variables
int regnr;
char location[32];
byte MAC[6];
char ssid[];
int status = WL_IDLE_STATUS;




void setup() {

  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);



}

void loop() {
  while (state == 1000) {
    delay(1000);
    Serial.println("Arduino tracker system v0.1");
    WiFi.macAddress(mac);
    
    state = 1001;
  }

  while (state == 1001) {
    if (Serial.available() != 0) {
      for (int i = 0; i < SBS ; i++) {
        byte o = Serial.read();
        if (o == 10) {
          i = (SBS + 1);
          state = 1010;
        }
        userInput[i] = o;
      }
      state = 1010;
      Serial.println(userInput);
    }
  }
  while (state == 1010) {
    for (int i = 0; i < SBS; i++) {
      commandRegister[i] = commandIndex[commandIndexLookup][i];
    }
    if (strcmp(commandRegister, userInput) == 0) {
      state = (999 - commandIndexLookup);
      commandIndexLookup = 0;
    }
    else if (commandIndexLookup > CC) {
      Serial.println ("unknown command");
      state = 1001;
    }
    else
    {
      commandIndexLookup ++;

    }
  }
  while (state == 999) {
    Serial.println("Terminating!");
    lastState = state;
    state = 1001;
  }

  while (state == 998) {
    Serial.print("Restarting");
    delay(250);
    Serial.print(".");
    delay(250);
    Serial.print(".");
    delay(250);
    Serial.print(".");
    resetFunc(); //call reset
  }

  while (state == 997) {
    Serial.println("device configuration");
    Serial.println("(1) Device registration number");
    Serial.print(":");
    if (regnr == 0;) {
      Serial.println("not configured");
    }
    else {
      Serial.println(regnr);
    }
    lastState = state;
    state = 1001

  }
}

thanks in advance

That's no good. Please do that. Thanks for posting in code tags, though...

knut
Step 1, write yourself a code that just exercises the serial monitor, no other device or code purpose, to convince yourself how that works. I think you'll find that the behaviour you are describing is due to your code, not some inherent 'feature' of the Arduino. That will remove that doubt, so you can proceed to debug your more complex software without questioning your baseline.
C

Yes, or sometimes you can find a ready made sketch suitable for testing, in the IDE code examples.

Sometimes the exercise of writing it yourself is more educational, and my suggestion is simple enough that it shouldn't consume much time, just a bit of reading and thought.
But YMMV - some learn from the examples, some learn better by DIY. It's all good, provided the exercise is educational.
C

I've learned to aim for the lowest bar here, and hope that the response will at least pass that. If it's better, that's a bonus! The advantage of the IDE example sketches is that they are pre-tested.

I'm not familiar with your board but I think that it has native USB; add while(!Serial) to setup as shown below. If the board indeed has native USB, that will more than likely solve your first issue with the printing.

void setup() {

  Serial.begin(9600);
  // wait for communication channel to be opened
  while(!Serial);
  pinMode(LED_BUILTIN, OUTPUT);

}

OK. thank you this seems to have worked. guess it was just not connected for the first few milliseconds. but still tried to print.
but this did not however. solve the issue of it not resetting properly. it still just freezes. any suggestions?

Your printing will go through the internal USB hardware of the microcontroller. If there is no communication channel established (yet), the printing is basically ignored. while(!Serial) waits till that communication channel is established (e.g. you open a terminal program like Serial Monitor).

Unfortunately not.

i did, thanks for the advice, turns out the problem was that the USB controller hadn't connected yet, so the first few prints just went thought ignored. but i still have the problem of the board not resetting properly. any ideas?

Unfortunately not.

Sorry!

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