Serial1 dont save to variable

Hello everyone, I have a nano that sends the value of the millis () function to the arudino mega via RS485 to the Serial1 port. I need this value to be stored in a global variable, but this does not happen, I hope they will help me here with a solution

this code mega

#include <Arduino.h>

#include <SPI.h>

#include <Blynk.h>

#include <Ethernet2.h>

#include <BlynkSimpleEthernet2.h>

#include <SoftwareSerial.h>

#define DIR 30

#define DIR1 31

/*

  26 октября 16:20

    Pins 10, 11, 12 and 13 are reserved for Ethernet module.

    DON'T use them in your sketch directly!

*/

// SoftwareSerial RS485(50,51); // (RX,TX)

// ro - RX

// di - TX

BlynkTimer timer1;

BlynkTimer outputtimer ;

char auth[ ] ="auth";

int i = 0 ;

void GetData()

{

if (Serial1.available() >0 )

{

i = Serial1.read();

}

}

void OutputData(){

Serial.print("i = ");

Serial.println(i);

}

void setup()

{

Blynk.begin(auth);

// RS485.begin(9600);

Serial.begin(9600);

Serial1.begin(9600);

timer1.setInterval(5,GetData);

outputtimer.setInterval(1000,OutputData);

pinMode(DIR,OUTPUT);

digitalWrite(DIR,LOW);

pinMode(DIR1,OUTPUT);

digitalWrite(DIR,LOW);

}

void loop()

{

Blynk.run();

timer1.run();

outputtimer.run();

}

When in your code is the data from the Serial is read?

I am missing where those methods call the GetData() function, can you point out where the loop() reads the GetData() function?

Though, GetData() is not how'd I would do a read serial but its your thing.

I did it on a timer,

timer1.setInterval(5,GetData);

I think this is the problem, but I do not know how to do it better

Could be. Me I use ESP32s and freeRTOS, completely how the blynky thingy works. Sorry to have wasted your time. Hopefully someone with the blinky thingy experience comes along and gives you a hand.

A simple place to start would be to serial print something in GetData to make sure it's being called. Once you know it is, only print if you see that there is something available on Serial1.

I try and is it work

void GetData()

{

if (Serial1.available() >0 )

{

i = Serial1.read();

Serial.write(Serial1.read());

}

}

I was prompted to use the volatile keyword, but I don't fully understand how ...

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