Hello all! Sorry about my english.
I'm trying to do a sketch that definetly receive a string with some protocol and convert diferent values for doing anything.
The protocol is, start: "[" end:"[". So I can send [V180] and refresh my vertical servo angle, or [H100] and refresh my horizontal servo angle too. But not at the same time. You should send one and after the another.
This is the code (working):
#define baudRate 9600
#define VERDADEIRO 1
#define FALSO 0
char Texto[50];
char *Ponteiro_Texto;
char ucInicial = 0;
void setup(){
Serial.begin(baudRate);
}
void loop(){
while (Serial.available() > 0){
unsigned char ucRecebe;
ucRecebe = Serial.read(); // Read data
if (ucRecebe == '['){ // verify start
ucInicial = VERDADEIRO; // Protocol stars here....
Ponteiro_Texto = Texto; // restart pointer
}else if (ucInicial == VERDADEIRO){ // start receive?
*Ponteiro_Texto = ucRecebe; // accept data
Ponteiro_Texto++; // pointer ++
}
if (ucRecebe == ']'){ // verify end
decodificaSerial(); // execute decodificaSerial
ucInicial = FALSO; // Wait for new starter
}
}
}
void decodificaSerial(){
int posicaoVertical;
int posicaoHorizontal;
switch (Texto[0])
{
case 'V':
posicaoVertical = atoi(Texto + 1);
Serial.print("Angulo servo Vertical: ") ;Serial.println(posicaoVertical);
posicaoVertical = 0;
break;
case 'H':
posicaoHorizontal = atoi(Texto + 1);
Serial.print("Angulo servo Horizontal: ");Serial.println(posicaoHorizontal);
posicaoHorizontal = 0;
break;
}
Serial.flush(); //Limpa buffer Serial para novo tratamento
}
But the original in a MICROCHIP PIC18F i use this line: Ponteiro_Texto = &Texto; If i tried to use this with Arduino, returns the error:
tratamento_serial.cpp: In function 'void loop()':
tratamento_serial:22: error: invalid conversion from 'char (*)[50]' to 'char'
The code should be:
#define baudRate 9600
#define VERDADEIRO 1
#define FALSO 0
char Texto[50];
char *Ponteiro_Texto;
char ucInicial = 0;
void setup(){
Serial.begin(baudRate);
}
void loop(){
while (Serial.available() > 0){
unsigned char ucRecebe;
ucRecebe = Serial.read(); // Read data
if (ucRecebe == '['){ // verify start
ucInicial = VERDADEIRO; // Protocol stars here....
[color=#ff0000]Ponteiro_Texto = &Texto; // restart pointer[/color]
}else if (ucInicial == VERDADEIRO){ // start receive?
*Ponteiro_Texto = ucRecebe; // accept data
Ponteiro_Texto++; // pointer ++
}
if (ucRecebe == ']'){ // verify end
decodificaSerial(); // execute decodificaSerial
ucInicial = FALSO; // Wait for new starter
}
}
}
void decodificaSerial(){
int posicaoVertical;
int posicaoHorizontal;
switch (Texto[0])
{
case 'V':
posicaoVertical = atoi(Texto + 1);
Serial.print("Angulo servo Vertical: ") ;Serial.println(posicaoVertical);
posicaoVertical = 0;
break;
case 'H':
posicaoHorizontal = atoi(Texto + 1);
Serial.print("Angulo servo Horizontal: ");Serial.println(posicaoHorizontal);
posicaoHorizontal = 0;
break;
}
Serial.flush(); //Limpa buffer Serial para novo tratamento
}
Anyone for this error?
Thx in advance and soooo sorry about the english!! hahahaha ;D