Serial issues.
i have sent , parsed string on serial , putting string dmx master library, keep getting random things happening, appear when value sent between 10-100 moving on part of dmx master library , giving bizarre results. i'm hoping can on code below , see if stands out why may getting these un expected results.
code: [select]
#include <dmxmaster.h>
char buffer[256];
string st, channel, msgvalue;
void setup()
{
pinmode(13, output);
serial.begin(115200);
while(serial.available())
serial.read();
}
void loop() {
if (serial.available() > 0) {
int index=0;
delay(300); // let buffer fill up
int numchar = serial.available();
if (numchar>180) {
numchar=180;
}
while (numchar--) {
buffer[index++] = serial.read();
}
splitstring(buffer);
}
}
void splitstring(char* data) {
serial.print("data entered: ");
serial.println(data);
char* parameter;
parameter = strtok (data, ":"); // note space before comma in " , "
while (parameter != null) {
setled(parameter);
parameter = strtok (null, ":"); // space before comma in " , "
}
// clear text , serial buffers
for (int x=0; x<180; x++) {
buffer[x]='\0';
}
while(serial.available())
serial.read();}
void setled(char* data) {
if ((data[0] == 'a') || (data[0] == 'a')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c1val=ans;
serial.print("c1 = ");
serial.println(c1val);
dmxmaster.write(1,c1val);
}
if ((data[0] == 'b') || (data[0] == 'b')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c2val=ans;
serial.print("c2 = ");
serial.println(ans);
dmxmaster.write(2,c2val);
}
if ((data[0] == 'c') || (data[0] == 'c')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c3val=ans;
serial.print("c3 = ");
serial.println(ans);
dmxmaster.write(3,c3val);
}
if ((data[0] == 'd') || (data[0] == 'd')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c4val=ans;
serial.print("c4 = ");
serial.println(ans);
dmxmaster.write(4,c4val);
}
if ((data[0] == 'e') || (data[0] == 'e')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c5val=ans;
serial.print("c5 = ");
serial.println(ans);
dmxmaster.write(5,c5val);
}
if ((data[0] == 'f') || (data[0] == 'f')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c6val=ans;
serial.print("c6 = ");
serial.println(ans);
dmxmaster.write(6,c6val);
}
if ((data[0] == 'g') || (data[0] == 'g')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c7val=ans;
serial.print("c7 = ");
serial.println(ans);
dmxmaster.write(7,c7val);
}
if ((data[0] == 'h') || (data[0] == 'h')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c8val=ans;
serial.print("c8 = ");
serial.println(ans);
dmxmaster.write(8,c8val);
}
if ((data[0] == 'i') || (data[0] == 'i')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c9val=ans;
serial.print("c9 = ");
serial.println(ans);
dmxmaster.write(9,c9val);
}
if ((data[0] == 'j') || (data[0] == 'j')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c10val=ans;
serial.print("c10 = ");
serial.println(ans);
dmxmaster.write(10,c10val);
}
if ((data[0] == 'k') || (data[0] == 'k')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c11val=ans;
serial.print("c11 = ");
serial.println(ans);
dmxmaster.write(11,c11val);
}
if ((data[0] == 'l') || (data[0] == 'l')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c12val=ans;
serial.print("c12 = ");
serial.println(ans);
dmxmaster.write(12,c12val);
}
if ((data[0] == 'm') || (data[0] == 'm')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c13val=ans;
serial.print("c13 = ");
serial.println(ans);
dmxmaster.write(13,c13val);
}
if ((data[0] == 'n') || (data[0] == 'n')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c14val=ans;
serial.print("c14 = ");
serial.println(ans);
dmxmaster.write(14,c14val);
}
if ((data[0] == 'o') || (data[0] == 'o')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c15val=ans;
serial.print("c15 = ");
serial.println(ans);
dmxmaster.write(15,c15val);
}
if ((data[0] == 'p') || (data[0] == 'p')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c16val=ans;
serial.print("c16 = ");
serial.println(ans);
dmxmaster.write(16,c16val);
}
if ((data[0] == 'q') || (data[0] == 'q')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c17val=ans;
serial.print("c17 = ");
serial.println(ans);
dmxmaster.write(17,c17val);
}
if ((data[0] == 'r') || (data[0] == 'r')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c18val=ans;
serial.print("c18 = ");
serial.println(ans);
dmxmaster.write(18,c18val);
}
if ((data[0] == 's') || (data[0] == 's')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c19val=ans;
serial.print("c19 = ");
serial.println(ans);
dmxmaster.write(19,c19val);
}
if ((data[0] == 't') || (data[0] == 't')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c20val=ans;
serial.print("c20 = ");
serial.println(ans);
dmxmaster.write(20,c20val);
}
if ((data[0] == 'u') || (data[0] == 'u')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c21val=ans;
serial.print("c21 = ");
serial.println(ans);
dmxmaster.write(21,c21val);
}
if ((data[0] == 'v') || (data[0] == 'v')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c22val=ans;
serial.print("c22 = ");
serial.println(ans);
dmxmaster.write(22,c22val);
}
if ((data[0] == 'w') || (data[0] == 'w')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c23val=ans;
serial.print("c23 = ");
serial.println(ans);
dmxmaster.write(23,c23val);
}
if ((data[0] == 'x') || (data[0] == 'x')) {
int ans = strtol(data+1, null, 10);
ans = constrain(ans,0,255);
int c24val=ans;
serial.print("c24 = ");
serial.println(ans);
dmxmaster.write(24,c24val);
}
}
code: [select]
char buffer[256];
are sending many characters?
code: [select]
if (serial.available() > 0) {
int index=0;
delay(300); // let buffer fill up
int numchar = serial.available();
if (numchar>180) {
numchar=180;
}
this crap. buffer holds 64 characters. delaying, in hopes whole packet arrive fool's dream.
the packets need followed end-of-packet marker (of sort; carriage return not uncommon). read , store data arrives (no delay()) until end of packet marker arrives (of course, means index needs global or static). when end of packet marker arrives use have stored, , reset index.
code: [select]
while (numchar--) {
buffer[index++] = serial.read();
}
more crap. char array not null terminated.
code: [select]
string st, channel, msgvalue;
more crap. have char array. not need wrap char array in string.
if makes feel better, substitute "less perfect code" crap.
Arduino Forum > Using Arduino > Programming Questions > Serial issues.
arduino
Comments
Post a Comment