0

I am trying ... with minimal success to change/Check and also run voids through the serial monitor .the code i put i am having probles is that when i am try to send a value larger than 9 the value assigns a diffrent number Ex i set to 200 but arduino sets it as 512.. i got around it deviding the value by 1.6 and got me the new number and only works upto 90 , but i am tryn to see how it should be done correctly...

const uint16_t inputLength = 512;
int ss = 0;
int bHold = 0;

void setup() { 
  Serial.begin(9600);
 }

 void loop() {
  if ( Serial.available() > 0 )
  {
    static char input[inputLength];
    static uint16_t i;
    char c = Serial.read();

    if ( c != '\r' && c != '\n' && i < inputLength - 1)
      input[i++] = c;

    else
    {
      input[i] = '\0';
      i = 0;

      uint16_t array[80];
      uint16_t j = 0;

      if ( !strncmp(input, "ss", 2) )
      {        
        char* p = input + 2;
        while ( (p = strchr(p, ' ')) != NULL )
          array[j++] = strtol(p, &p, 10); //< changed from 16

        for ( uint8_t i = 0; i < j; i++ )
        {
          ss = array[i];
          Serial.println(ss);
        }
      }

      if ( !strncmp(input, "bHold", 5) )
      {        
        char* p = input + 5;
        while ( (p = strchr(p, ' ')) != NULL )
          array[j++] = strtol(p, &p, 10);   //< changed from 16

        for ( uint8_t i = 0; i < j; i++ )
        {
         bHold = array[i];
        // bHold=bHold /1.6;            
         Serial.println(bHold);
        }
      }

      if ( !strncmp(input, "Whos There", 10) )
      {
        Serial.print("me");
      }
      if ( !strncmp(input, "hello", 5) )
      {
        Serial.print("hi");
      }
      if ( !strncmp(input, "sa", 2) )
      {
        Serial.print("  ss= ");
        Serial.print(ss);

        Serial.print("  bHold= ");
        Serial.print(bHold);

        Serial.println(" ");
        Serial.println("==============================");
      }
    }
  }
}

Thank you for any tips you can give...

EDIT: I am trying to pass a new value with command on the serial monitor "ss 500"..so that in the arsuino ss = 500. but in this case it makes it equal to 1280.

EDIT2: Chaged the strol to 10...works perfectly ...Thank you TisteAndii!!!!

  • What do you mean with "ss = array[i], DEC;". That is "ss=DEC" and gives the same value ss. Should be "ss = array[i]; Serial.println(ss, DEC);" – Mikael Patel Feb 4 '16 at 6:40
  • Edit the code in your post if you have made any changes to it so far. – TisteAndii Feb 4 '16 at 8:43
0

You are setting the input string's base to 16 instead of 10 in your strtol conversions, which is why you are getting 1280 when you expect 500.

  • Ex i set to 200 but arduino sets it as 512 - TisteAndii is right. 0x200 is 512 in decimal. – Nick Gammon Feb 4 '16 at 10:22

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.