Send information via TX/RX from an arduino uno to a mega

Hello

I have two Arduinos, the 1st sends a number with two decimal places via Serial.println, which is the "velocidade" variable.

Programming of the 1st aduino:

...
  distancia =  3.14159265 * raio; //metros
  float tempo = millis() - ant_tempo;//tempo da meia volta em ms
  ant_tempo = millis();
  tempo = tempo / 1000; //em segundos
  float velocidade = distancia / tempo;
  velocidade = velocidade * 3.6;

  Serial.println(velocidade);
...

Until this part everything is ok, my problem is how do I make the 2nd arduino read and treat this received value and store it in the variable "velRecebida"

Programming of the 2nd aduino:

#define pinA1 A2
#define pinB1 A1
#define pinC1 A5
#define pinD1 A6
#define pinE1 A7
#define pinF1 A3
#define pinG1 A4

#define pinA2 8
#define pinB2 7
#define pinC2 4
#define pinD2 5
#define pinE2 6
#define pinF2 9
#define pinG2 10

#define pinP1 45
#define pinP2 47

byte pinSegmento1[8] = {pinA1, pinB1, pinC1, pinD1, pinE1, pinF1, pinG1, pinP1}; 
byte pinSegmento2[8] = {pinA2, pinB2, pinC2, pinD2, pinE2, pinF2, pinG2, pinP2}; 

                    // abcdefgp
byte digito[10] = {0b11111100, //zero
                   0b01100000, //1
                   0b11011010, //2
                   0b11110010, //3
                   0b01100110, //4
                   0b10110110, //5
                   0b10111110, //6
                   0b11100000, //7
                   0b11111110, //8
                   0b11110110}; //9

int velRecebida;

int digito1;
int digito2;

void setup(){
  Serial.begin(9600);

  for (int nL=0; nL < 8; nL++) {
     pinMode(pinSegmento1[nL], OUTPUT);
  }
  for (int nL=0; nL < 8; nL++) {
     pinMode(pinSegmento2[nL], OUTPUT);
  }
}

void loop(){

digito1 = (velRecebida / 10) % 10;
digito2 = velRecebida % 10;

if (digito1 == 0){
       for (byte nL=0; nL < 8; nL++) {
     digitalWrite(pinSegmento1[nL], 0);
  }
     for (byte nL=0; nL < 8; nL++) {
     digitalWrite(pinSegmento2[nL], bitRead(digito[digito2], 7-nL));
  }}
else{
     for (byte nL=0; nL < 8; nL++) {
     digitalWrite(pinSegmento1[nL], bitRead(digito[digito1], 7-nL));
  }
     for (byte nL=0; nL < 8; nL++) {
     digitalWrite(pinSegmento2[nL], bitRead(digito[digito2], 7-nL));
  }}

    delay(100);
}

The Serial Input Basics tutorial on this forum will show you how to reliably send and receive serial data between Arduinos, or Arduino and another computer.

1 Like

Welcome to the Forum.
Please do not open several threads for the same subject. The forum rules forbids this even if the topics are sent to different forum categories and using the different languages.

As a general rule, you shouldn't perform any operations that rely on interrupts in an interrupt service routine (ISR). These operations would include serial communications, delay() calls, etc. Also, there is no reason to detach and attach the interrupt in the ISR because interrupts are already disabled.

Is there some reason you are using an interrupt to detect the sensor_indutivo input change? You could do that in loop() using state change detection and avoid a lot of issues later. For example:

void loop()
{
  static int lastSensorState = digitalRead(sensor_indutivo);
  int currSensorState = digitalRead(sensor_indutivo);

  if (currSensorState != lastSensorState)
  {
     lastSensorState = currSensorState;
     if (currSensorState == LOW)
     {
          // State went from HIGH to LOW
     }
  }
}

Can you send me a link that can help me?

I have merged your cross-posts @gildrian.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

web search

Thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.