String from Serial help
i'm trying capture string serial line, having issue.
if send
xxx\r
yyy\r
zzz\r
i want yyy in "string". here's code i've been playing with. can me figure out why string xxxyyy???
if send
xxx\r
yyy\r
zzz\r
i want yyy in "string". here's code i've been playing with. can me figure out why string xxxyyy???
code: [select]
#define serial_speed 57600
#define led 13
char chararray[256];
char c;
int i = 0;
int r = 0;
void setup()
{
pinmode(led, output);
chararray[0] = '\0';
// set serial 57600 bps
serial.begin(serial_speed);
serial.println("type , hit enter, type more , hit enter.");
delay(5000);
if(serial.available() > 0)
{
while(serial.available() > 0)
{
c = serial.read();
if(c == '\r')
{
r++;
if(r <= 1)
{
continue;
}
else
{
break;
}
}
else if(c == '\n')
{
continue;
}
else
{
i++;
chararray[i-1] = c;
}
}
serial.print("\"");
serial.print(chararray);
serial.println("\"");
}
digitalwrite(led, high);
}
void loop()
{
}
do want run continuously in loop? or once (i.e. 1 string)?
here's how write it, if wanted accept single string, terminated \r:
if want continuously receive strings terminated \r, move inside of setup() loop().
b
here's how write it, if wanted accept single string, terminated \r:
code: [select]
#define serial_speed 57600
#define led 13
void setup()
{
char chararray[256];
char c;
int i = 0;
pinmode(led, output);
chararray[0] = 0;
// set serial 57600 bps
serial.begin(serial_speed);
serial.println("type , hit enter, type more , hit enter.");
while (serial.available() == 0); // nothing - wait first char
c = serial.read();
while (c != '\r')
{
if (c != '\n') // skip new lines
{
chararray[i++] = c; // store character, , increment our index
chararray[i] = 0; // terminate string
}
while (serial.available() == 0); // wait next char
c = serial.read();
}
serial.print("\"");
serial.print(chararray);
serial.println("\"");
digitalwrite(led, high);
}
void loop()
{
} if want continuously receive strings terminated \r, move inside of setup() loop().
b
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > String from Serial help
arduino
Comments
Post a Comment