Arduino serial communication

Asked

Viewed 235 times

0

I’m trying to figure out a certain exercise my teacher gave me in class. Basically the exercise is: With two arduinos prepared to make the serial communication between TX and RX, this must transmit four sequence of codes through a keyboard with two keys.

Pin 0 - RX

Pin 1 - TX

Pin 7 - Button A

Pin 8 - Button B

Pin 13 - Led

Key A Button B

Upshot 0 0 Led out

0 1 Transmit 1010101010101010

1 0 Transmit 11111100000101010

1 1 Led On

I studied the material on Arduino’s own website about serial programming, I developed my own code but I find problems with it. I’ll let it down for me those who want to take a look.

NOTE: I can guarantee that there is nothing wrong with the hardware and this question is only about the programming itself.

CODE:

// TRANSMISSOR

int numA = 7; 

int numB = 8;


void setup(){

Serial.begin(9600); // Configurado a serial

}

void loop(){


numA = 0; // Bit 0A criado

numB = 0; // Bit 0B criado

Serial.write(numA); // Bit 0A transmitido

Serial.write(numB); // Bit 0B transmitido

delay(2000); // Tempo de 2 segundos



numA = 1; // Bit 1A criado

numB = 1; // Bit 1B criado


Serial.write(numA); // Bit 1A transmitido

Serial.write(numB); // Bit 1B transmitido


delay(2000); // Tempo de 2 segundos

} 


//RECEPTOR


int recByteA;

int recByteB;

int led = 13;


void setup(){

Serial.begin(9600); // Configurado a comunicação serial

pinMode(led,OUTPUT); // Configurado o LED como saída

}


void loop(){

if(Serial.available() > 0){ // Verifica se existe informação na serial em 
Serial.avaliable

recByteA = Serial.read(); // Caso sim o Bit é gravado em recByte que vem de Serial.read

}

if(Serial.available() > 0){ // Verifica se existe informação na serial em Serial.avaliable

recByteB = Serial.read(); // Caso sim o Bit é gravado em recByte que vem de Serial.read

}

if(recByteA == 0 && recByteB == 0){ // Testa se o Bit que esta em recByteA e B é igual a 0

digitalWrite(led,LOW); // Se o Bit for Zero o LED apaga

}

else if(recByteA == 1 && recByteB == 1){ // Testa se o Bit que esta em recByteA e B é igual a 1

digitalWrite(led,HIGH); // Se o Bit for Um o LED liga

}

else if(recByteA == 0 && recByteB == 1){ // Testa se o Bit que esta em recByteA é igual a 0 e recByteB é igual a 1

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

recByteA = 1;

recByteB = 0;

}

else if(recByteA == 1 && recByteB == 0){ // Testa se o Bit que esta em recByteA é igual a 1 e recByteB é igual a 0

recByteA = 1;

recByteB = 1;

recByteA = 1;

recByteB = 1;

recByteA = 1;

recByteB = 1;

recByteA = 0;

recByteB = 0;

recByteA = 0;

recByteB = 0;

recByteA = 0;

recByteB = 1;

recByteA = 0;

recByteB = 1;

recByteA = 0;

recByteB = 1;

recByteA = 0;

}

}
  • First, start reading the documentation, then, from what you learn, try to make a solution on your own. If it doesn’t work or you have any questions in the process, you can come back and ask here in the community.

  • I have read the material on the Arduino site itself, the code I will put below is from the experience I am doing but I am not getting results.

  • // TRANSMISSOR

int numA = 7; 
int numB = 8;

void setup(){
Serial.begin(9600); // Configurado a serial
}

void loop(){
 
numA = 0; // Bit 0A criado
numB = 0; // Bit 0B criado
Serial.write(numA); // Bit 0A transmitido
Serial.write(numB); // Bit 0B transmitido
delay(2000); // Tempo de 2 segundos
 
numA = 1; // Bit 1A criado
numB = 1; // Bit 1B criado
Serial.write(numA); // Bit 1A transmitido
Serial.write(numB); // Bit 1B transmitido
delay(2000); // Tempo de 2 segundos
}

  • If better if you [Dit] the question, but already I say that the doubt will only be part of the scope if you are sure that the electronic part is correct. It is not up to the community to discuss details about hardware implementation.

  • OK I will edit it, I guarantee that in the physical part everything is right and I understand perfectly that such a subject does not fit the community to give me answers when it comes to hardware.

  • I actually just created my account so I apologize for the lack of editing

  • I will try to make the question clearer and focus more on the programming itself. Thank you.

  • Why do you change the value of the variables recByteA and recByteB several times in a row? That doesn’t seem to make much sense;

  • Hey, thanks for the review. After reading your comment I realized it didn’t make much sense anyway, I used a Serial.write() to send the code I needed in binary. Thank you

Show 4 more comments

1 answer

0

From the little experience I have with Arduin, this part is likely to give error, because the execution of the loop will probably be faster than the serial transmission. Then it will read the two bytes in different loop adding them always to the recByteA variable.

// receptor
// Verifica se existe informação na serial em Serial.avaliable
if(Serial.available() > 0)
{
    // Caso sim o Bit é gravado em recByte que vem de Serial.read
    recByteA = Serial.read(); 
}
// Verifica se existe informação na serial em Serial.avaliable
if(Serial.available() > 0)
{
    // Caso sim o Bit é gravado em recByte que vem de Serial.read
    recByteB = Serial.read(); 
}

One option would be to change the if for while thus locking the loop on that part until the two bytes are read

while(Serial.avaliable() == 0) delay(1);
recByteA = Serial.read();
while(Serial.avaliable() == 0) delay(1);
recByteB = Serial.read();
  • Thank you guys ! I read your comment and I managed to solve the problem ! Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.