Go Down

Topic: serial communication noob simple question! (Read 531 times) previous topic - next topic

flintec

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

void loop()
{
while(Serial.available() == 0);

int  y = Serial.read() - '0' ;
serial.println(y);
}
if i start serial monitor i can just send the numbers 0-9 and get the same back from the arduino how do i modify this so i can write 10 and get 10 back? or 24 and get 24 back?

PaulS

You are right. It is a simple question. One that is asked about three times a week. Spend some time searching and trying to figure this out yourself.

The key is that serial data is sent just like you pounded the keys - one letter at a time.

The problem will be telling 120, on the Arduino from 12. What marks the end of the packet?

flintec

hmmm i dont know if i really understand i dont need to modify the program? just end it with a 0?

PaulS

Quote
i dont know if i really understand

You don't.

Quote
i dont need to modify the program?

Yes, you do.

Quote
just end it with a 0?

End what? How would that help?

The point was that the Arduino is reading a stream of data from the serial port. It is just like you reading what I am typing. Notice,though,thatwhenItypeImakeiteasyforyoubyusingseparatorsbetweenpacketssoyoucaneasilyseewhereapacketstartsandends.

You'll need to provide some way, on the sending end, for the Arduino to know when it has received a complete packet. Only when that happens should the Arduino try to understand what it has received.

Here is some code that lets you send <12> or <120> or <47> using the serial monitor. The Arduino will then read all the data between the start of packet marker (<) and the end of packet marker (>), and store that data in an array.
Code: [Select]

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

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // 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;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        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';
  }
}


Where it says "Process the packet", inData will contain everything that you typed between the < and the >. You can then print that, or convert it to an int, or whatever.

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!

Arduino
via Egeo 16
Torino, 10131
Italy