Hello dear!!
I made a simple app to android (by app inventor) that I want to send datas to arduino board by bluetooth.
I got this situation watching by serial monitor:
tempoEspera : 52
96
tempoEspera : 96
0
tempoEspera : o
0
tempoEspera : of
0
tempoEspera : f
0
tempoEspera : o
0
tempoEspera : of
0
tempoEspera : f
I want to take three variables to work in my sketch, "tempoEspera" that receives only numbers and other two "on", "off" to control some function in my whole program but they fell through as showed.
String readString;
int ledPinState = LOW;
void setup() {
Serial.begin(9600);
}
void loop()
{
master();
value();
}
void master()
//void Led_Bluetooth()
{
while ( Serial.available() )
{ // Indica que ha dados no buffer de transferencia
delay( 3 );
char c = Serial.read();
readString += c; // Constroi uma string - "on" or "off"
}
if ( readString.length() >0 )
{
Serial.println( readString );
if ( readString == "on" )
{
ledPinState = HIGH;
}
if ( readString == "off" )
{
ledPinState = LOW;
}
readString="";
}
}
void value()
{
char tempoEspera[] = {' ',' '}; // Receive up to 7 bytes
while (!Serial.available()); // Wait for characters
Serial.readBytesUntil('n', tempoEspera, 2);
int incomingValue = atoi(tempoEspera);
Serial.println(incomingValue);
Serial.print("tempoEspera : "); // Escreve a palavra contadorTempo_ON no serial monitor
Serial.println(tempoEspera);
}
Thanks!!
Sorry about my last post
If you send an end-of-packet marker, instead of hoping that you've waited long enough for all the serial data that makes up a packet to arrive, you'd have a much easier time.
If, for instance, you send "on!" or "off!", you would read and save whatever data had arrived, as fast as it arrived, and do nothing with the saved data until the '!' arrived. Then, you'd have off or on in readString.
Now, you have some letters in readString, but they may not be (and appear never to be) a complete packet.
Can you send a short example code?/ I am beginner..
RicardoLadislau:
Can you send a short example code?/ I am beginner..
First, can you modify your sending app to send an end-of-packet marker? If not, writing code to receive one is silly.
You did mean is impossible to do this only by arduino code?/
You did mean is impossible to do this only by arduino code?
It's a lot harder. You have to guess when you have received a complete packet.
Have a look at serial input basics. The techniques can also be used in code for receiving data on a PC - if that's what you are having a problem with.
...R
Okay guys I am so greatful it's running as expected...
I am really glad about this forum...
my whole code is following..
Thanks everyone
// Novas variáveis para link com buffer serial de entrada
int receiver; // Esta sera a variavel para receber o tempo ON
int receiver_2;
int incomingValue; // Buffer de dados para comparação de funçoes a serem executadas
String readString;
int ledPinState = LOW;
void setup()
{
Serial.begin(9600);
}
void loop()
{
dadosEntrada ();}
void dadosEntrada ()
{
char buffer[] = {' ',' ',' ',' ',' ',' ',' ',' '}; // Receive up to 7 bytes
//if (!Serial.available()); // Wait for characters
if (Serial.available() > 0)
Serial.readBytesUntil('n', buffer, 8);
incomingValue = atoi(buffer);
Serial.print("incomingValue : ");
Serial.println(incomingValue);
Serial.print("receiver : "); // Escreve a palavra contadorTempo_ON no serial monitor
Serial.println(receiver);
if (incomingValue > 0 && incomingValue <= 20)
{
receiver = incomingValue;
Serial.print("receiver : "); // Escreve a palavra contadorTempo_ON no serial monitor
Serial.println(receiver);
}
if (incomingValue >= 200 && incomingValue <= 4000)
{
receiver_2 = incomingValue / 200;
Serial.print(" receiver_2 : ");
Serial.println( receiver_2);
}
if ( incomingValue == 5101 )
{
ledPinState = HIGH;
Serial.print("ledPinState : ");
Serial.println(ledPinState);
}
if ( incomingValue == 6101 )
{
ledPinState = LOW;
Serial.print("ledPinState : ");
Serial.println(ledPinState);
}
}
This project is about electronic shower
I hope it can help someone in the future..