the code has compiled, but don't work.
the code should call the function ping every 5 seconds if it does not receive "the command" via serial port, but it hangs on the call interruption and not run but nothing.
#include <TimerOne.h>
byte MEUEND=B00001111;
byte OUTEND=B00000000;
byte MEND=B00001111;
byte MEND2=B11110000;
byte MTIPO=B00000011;
void setup()
{
Serial.begin(9600);
Serial.flush();
}
void envia(Mensagem Msg)
{
Serial.println(Msg.Fcontrole);
Serial.println(Msg.msg.id);
Serial.println(Msg.msg.dados);
Serial.println(Msg.msg.payload);
Serial.println(Msg.verifica);
Serial.println(Msg.numseq);
Serial.flush();
}
Mensagem recebe()
{
Mensagem Msg;
Msg.Fcontrole=Serial.read();
Msg.msg.id=Serial.read();
Msg.msg.dados=Serial.read();
Msg.msg.payload=Serial.read();
Msg.verifica=Serial.read();
Msg.numseq=Serial.read();
Serial.flush();
return(Msg);
}
void trata(Mensagem Msg, int* y) //função que recebe a mensagem
{
byte endereco;
endereco=Msg.Fcontrole>>4;
endereco= endereco&MEND;
if (endereco == MEUEND) //vê se foi eu quem mandou
return;
if (endereco == OUTEND)
{
byte tipo;
tipo = Msg.Fcontrole&MTIPO;
if(tipo==0)
if(Msg.msg.id=='p') //testa pra ver se é ping
{
pong();
}
else
{
*y=1;
return;
}
}
}
void ping ()
{
Mensagem Msg;
Msg.Fcontrole = MEUEND;
Msg.Fcontrole=Msg.Fcontrole<<4;
Msg.Fcontrole=Msg.Fcontrole&MEND2;
Msg.msg.id='p';
envia(Msg);
}
void pong()
{
Mensagem Msg;
Msg.Fcontrole = OUTEND; //pra testar com 1 arduino só usar OUTEN
Msg.Fcontrole=Msg.Fcontrole<<4;
Msg.Fcontrole=Msg.Fcontrole&MEND2;
Msg.msg.id='q';
envia(Msg);
}
void loop()
{
Serial.flush();
Mensagem Msg;
int x=0;
int y=0;
for(;x==0;y=0)
{
ping();
Timer1.initialize(5000000);
Timer1.attachInterrupt(ping);
while(y==0)
{
if(Serial.available()>5)
{
trata(recebe(),&y);
}
}
Timer1.detachInterrupt();
}
}
PS.: the struck Mensagem and Req is declared in TimeOne.h