After coding a decent amount of code, pulling my hair out, walked away.. googled for the answer and with no real help other than recoding it with else if statements... I'm here to see if I missed anything...
I'm running a SainSmart uno, nothing added to it.. and a simple winform program on a dell with windows xp I'm using the usb cable, usb serial port, on the board that came with it and COM4 usb port on the computer....
I have serial communication between the board and the winform.. I can turn the made in led light with a check box event from the form.. I can send a request to the board and retrieve variables and display them in a form on the computer.....
I have tried using char arrays with no relief... String arrays aren't allowed in a switch statement.. I have added booleans and everything to try to get it working..
please remember I'm fairly new to c++.. and I have altered this code multiple times, so I might have something in there that doesn't make since lol..
int ledPin = 13;
boolean responce = false;
String message ="";
boolean sDone=false;
boolean gotName = false;
boolean gotValue = true;
int data =0;
String name[10];
String value[10];
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(){
if(responce) {
message += '\r\n';
Serial.println(message);
Serial.println(name[0]);
message ="";
responce = false;
gotName = false;
gotValue = true;
}
}
void serialEvent(){
while (Serial.available()) {
// get the new char
char inChar = (char)Serial.read();
// comma marks the end to values
if (inChar == ','){
//reset gotName for the next name[data]
gotValue = true;
gotName= false;
data++;
// = follows the end of name. now get name value..
}else if(inChar == '='){
gotValue = false;
gotName= true;
}else if(inChar == '\n'){
// the new line tells us we're done reading
}else{
// we have found a = sign, a value should follow
if((gotName) && (!gotValue)){
value[data] +=inChar;
}else{
// we're still reading a name..
name[data] += inChar;
}
}
}
for(int i=0; i <= data; i++){
switch (name[i]) {
case 'apple':
digitalWrite(ledPin, HIGH);
message +="Light on";
name[i] =0;
break;
default:
// turn all the LEDs off:
digitalWrite(ledPin, LOW);
message += value[data];
message +=',';
value[i]=0;
}// moved this to for loop to see if that's the problem
if (i >= data){
responce = true;
}
}
}
What I am trying to do is send serial data from the form that looks like this..
apple=sweet, pear=hard, and so on..
So, as you can tell it don't work like I had planed. I was trying to read the serial into name[0] until it finds the "=" and then read it into value[0] until it finds a comma marking the end of value.. then add one to "data" so it can start reading into name[1] and so on...
char doesn't return anything like it should... string wont work the for (i) switch so I can send multiple commands in one transmittion and send the "message" along with the return carrage to get my form to quit the ReadLine();
This is the code I know works with serial...
int ledPin = 13;
int servoLast, servoNew,servoMove;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
digitalWrite(ledPin, HIGH);
Serial.write("Light on");
Serial.write('\r\n');
break;
case 'b':
digitalWrite(ledPin, LOW);
Serial.write("Light Off");
Serial.write('\r\n');
break;
case '1':
Serial.write("37.061906");
Serial.write('\r\n');
break;
case '2':
Serial.write("-85.119327");
Serial.write('\r\n');
break;
default:
digitalWrite(ledPin, LOW);
Serial.write(inByte);
Serial.write('\r\n');
}
}
}
if you want the winform code, I'll have to zip it up and attach it as it's a Visual c++ 2010 project..