1

I am currently working with an Arduino Micro / Linduino and making a program based on a lot of switch statements to execute certain functions that will be passed to a microprocessor (LT2974) on a board serially. For the most part, all of the functions I have work, except there is one that I am trying to determine if it is my program that is causing issues, or the Linear Tech libraries that are provided. Upon research, I did discover something strange in my code that I cannot explain.

Here is a quick snippet of my code:

unsigned long serialdata;
int inbyte;

long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.read();     
    if (inbyte > 0 && inbyte != '/')
    {
      serialdata = serialdata * 10 + inbyte - '0';
      Serial.print(serialdata);
      Serial.print("test \n");
    }
  }
  inbyte = 0;
  return serialdata;
}

void loop()
{
  getSerial();
  // Switch statement represents Slot choices
  switch (serialdata)
  {

    case 1:
      {
        commands(Slot1);
        break;
      }
    case 2:
      {
        commands(Slot2);
        break;
      }
    case 3:
      {
        commands(Slot3);
        break;
      }
    case 4:
      {
        commands(Slot4);
        break;
      }
  }
}

I have left out the following switch statements that are found in the commands function as they are not where the issue lies.

For some reason, when I pass data through to the Arduino, and then go to reprint it out with my Serial.print command in getSerial(), there is an additional number printing, and I believe the issue to lie here.

So, for example, if I type "/1/" (the slashes were used to pass multiple variables as shown in the function, but I'm only passing one for now), the Arduino reponds with:

1test 
4294967268test 

here is "/2/"

2test 
4294967258test 

and here is passing multiple variables with "/2/9/"

2test 
9test 
4294967258test 

All of the values I just showed you do not correspond to any of my switch statements implemented, and so no actual data is being returned besides the print command from getSerial(). So can anyone explain what is going on here?

EDIT: I added the data types of serialdata and inbyte to the top of the post.

  • 4294967268 is (unsigned long)(20 - '0'). – Edgar Bonet Apr 7 '16 at 12:54
1

The problem is that you are blindly reading serial data without first looking to see if there is actually any serial data available to read.

If there is no data there Serial.read() will return -1. Since you have only provided a snippet of your program I can't say what data type inbyte is, but my guess is it's some form of unsigned 32-bit value, since -1 mapped into an unsigned 32-bit value is 4294967295.

You need to be checking the presence of data, and only reading when you should read, and also looking for the end of your data sequence (add some terminating character, like a line feed) to know when to stop looking for and reading characters.

  • I edited my post. Did you mean to say that you believe serialdata is a 32 bit unsigned int? because that is what I am printing and that would make sense. Also, I am reading the data and storing it to inbyte, but shouldn't it not go through the Serial.print part since the last character i am entering is '/' and I told it to ignore that? – Trever Wagenhals Apr 7 '16 at 13:32
  • Ah yes, serialdata of course I meant. – Majenko Apr 7 '16 at 13:33
  • So it seems the issue is that my getSerial() function reads the data, stores it to inbyte, and doesn't recognize the data as <0 or '/' so it is going into the loop and printing that value to serialdata, which looks strange but is actually correct? So the next question is why is it going into the loop again then? – Trever Wagenhals Apr 7 '16 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.