Go Down

Topic: Reading from serial and converting ISSUE? (Read 419 times) previous topic - next topic

Jan 12, 2012, 01:04 am Last Edit: Jan 12, 2012, 01:34 pm by albrufsky Reason: 1
  this has been solved LOOK at PaulS response.

Hello fellow programmers, I am a n00b and think I'm doing something wrong. :smiley-eek-blue:  So I am issuing this challenge: I challenge you to create a program for an arduino (if that wasn't already clear) that reads a string from serial like "14,90", "2,20", etc and split and convert it into an int array. All help is very appreciated.  I thank you very much in advance. You may ask questions in a quote to this thread or in a post if you wish.

P.S. Don't tell me not to do a challenge and just ask for help.

Most people aren't just going to do your work for you.  From my experience you need to put forth some effort, show and explain what you've done, and then explain at what step you become unsure.

Just some advice if you actually expect useful responses.


Oh definately I totally understand its just that I followed many guides and tutorials on doing this and not  one has worked for me so I was wondering if there was anything that worked for someone else.
I guess I was trying to think a little more creatively than logically.
I am sorry for the misunderstanding.

Well post what you have, and what you're having trouble with, and I'm sure people will be glad to help!

Thank you for helping me out there. I will post that up when I'm near my computer next!

P.S. I'm sending this from a phone.


I've posted code to do this many times. Just in case you've not seen it, the code to receive the data looks like:
Code: [Select]

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
   // Other stuff...

void loop()
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
    char inChar = Serial.read();
    if(inChar == SOP)
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    else if(inChar == EOP)
       ended = true;
      if(index < 79)
        inData[index] = inChar;
        inData[index] = '\0';

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';

Send "<14,90>", and inData will contain "14,90". The strtok() and atoi() functions can then parse the tokens ("14" and "90" and convert them to ints (14 and 90). Exactly how to use them is your part of our homework.

Thank you so much paulS!!!!!! Also, I think it strange that I was looking around the forum quite a while and still did not find this code.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131