Serial.parseInt stops the program

Hello everyone, i have a problem. It seems to be the function, Serial.parseInt() that makes my code stop working. I have tried to recreate the code from top to bottom and that is how i found out that it is making the code stop.

  • It will not show anything in my Serial monitor. Can anyone figure out what is wrong with my code, and tell how i can fix it?
//Liberies that are includes
#include <Arduino.h>
#include <Stepper.h>

//Defines how many steps for a 360 degree turn.
const int StepsPerRevolution = 200;

//Defines which pins the stepper motors are using.
Stepper feeder = Stepper(StepsPerRevolution, 8, 9, 10, 11);
Stepper bender = Stepper(StepsPerRevolution, 4, 5, 6, 7);

int a = Serial.parseInt();
int b = Serial.parseInt();
int c = Serial.parseInt();

#define MICROSTEPS 16
#define FEEDSTEPS 8

int DELAY = 1000;


void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600); //Turning serial monitor on.
    feeder.setSpeed(50); //Setting the feeder speed to 50 RPM.
    bender.setSpeed(100); //Setting the bender speed to 100 PRM.
    Serial.println("For trekant skriv (1,sidelængde)"); //Describes how to make a triangle with a specific side length  
    Serial.println("For kvadrat skriv (2,sidelængde)"); //Describes how to make a square with a specifik side length.
}

void loop() {  
  if (Serial.available()>0){

  a = Serial.parseInt();
  b = Serial.parseInt();
  c = Serial.parseInt();
  }

  if (a == 1) {
    Serial.println(" ");
    Serial.print("Producing a triangle");
    Serial.print(b);
    Serial.println("mm");
  for (int x=0; x<3; x++){
    Serial.print("..");
    feeder.step(FEEDSTEPS*-b*MICROSTEPS);
    delay(DELAY);
    bender.step(100);
    delay(DELAY);
    bender.step(-100);
  }
  feeder.step(FEEDSTEPS*200*MICROSTEPS);
  Serial.println("Done, receive your triangle");
  a = 0;
  b = 0;
  }







  
  
}

int a = Serial.parseInt() remind me - what is the serial interface line speed at this point?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

You definitely should wait until after you call Serial.begin() before you start looking for serial input.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

…R

Or better still, use a super simple serial transfer library to reliably transmit/receive serial data packets. It can be installed via the Arduino IDE Libraries Manager (search “serialtransfer”).

Example code:

TX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  float myFloat = 100.5;
  
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  myTransfer.txFloat(myFloat, 3); //insert the float "myFloat" at index 3 since "hi\n" already takes up indicies 0-2
  
  myTransfer.sendData(7); //3 bytes for "hi\n" plus 4 bytes for the float "myFloat"
  delay(100);
}

RX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  float myFloat;
  
  if(myTransfer.available())
  {
    /////////////////////////////////////////////////////////////// Handle Entire Packet
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();

    /////////////////////////////////////////////////////////////// Parse Out Float From Packet
    myTransfer.rxFloat(myFloat, 3);
    Serial.print("Received float: "); Serial.println(myFloat);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Power_Broker:

Have a look at the examples in Serial Input Basics - simple relia ....

Or better still, use a super simple serial transfer library

I have no intention of criticising your library and perhaps you can reciprocate by not presenting it as "better" than my Serial Input Basics. I wrote my contribution as a teaching tutorial and I think it works as that.

Maybe for the future you could say "Or, you may prefer to use a super simple ....."

...R

Robin2:
Or better still, use a super simple serial transfer library

I have no intention of criticising your library and perhaps you can reciprocate by not presenting it as "better" than my Serial Input Basics. I wrote my contribution as a teaching tutorial and I think it works as that.

Maybe for the future you could say "Or, you may prefer to use a super simple ....."

...R

99.9% of people here just want their project to work. Linking a tutorial is great, but if you know there are libraries (in this case there are two) that will fix the problem easy, you should include at least one of them as well.

Give beginners the choice - whether they want to "roll their own" solution or "just want it to work".

Power_Broker:
99.9% of people here just want their project to work.

Fair enough. That's what I had in mind with the words "you may prefer to"

I guess my own preference is to provide a learning tool.

...R