Hello,
I am relatively new to Arduino coding and have undertaken the task of sending data via Serial ports to two different Arduino boards. I am using an Arduino Mega for the Master and a Arduino Uno for the slave. They are connected in the following manner:
(RX1) on the mega to (TX0) on the uno
(TX1) on the mega to (RX0) on the uno
(GND) on the mega to (GND) on the uno
The serial comm works fine and I can use the Serial.print() function on the mega to send data to the uno while using the Serial.readString() fuction on the uno to read the data string in. The problem that I am having is that when I reset the mega, the uno’s program runs the start up function but will not run the loop function until the serials are disconnected. Then when I plug the serial link back in the program freezes once again. It should be noted that on initial start up everything runs correctly in regard to the serial communication. It is only after reset of one or both of the boards that the problem occurs.
I should mention that I am looking at the Uno’s serial through a USB connection when this happens. Could this have something to do with it?
Additionally I am using a RockBlock Sat Comm setup through soft serial to send the data received from the mega, as well as a 4D Systems resistive touch screen on the Mega as a timer display and control (which is attached to Serial 0 of the mega).
The reason for the reset is so that the timer runs correctly. For some reason on startup the timer counts one second for approximately every five seconds or so of real time. After a reset on the board the timer works fine but then the serial communications do not. This a lot to take in so I am posting the entire uno code and parts of the mega code. If Additional information is required please feel free to ask.
Please if anyone can help it would be greatly appreciated. Thank you well in advance to any and all how can help.
Uno Code:
#include <IridiumSBD.h>
#include <SoftwareSerial.h>
//Global Time Constaint
const int Time_Displacment = 5;
// Mega Variables to bring in.
String Message;
// Data string and text message variables
String Comp_Msg;
char Text [100];
// Message timer
unsigned int Send_Timer = 0;
int Run_Time_Min = 0;
int Run_Time_Sec = 0;
//NOTE: Serial Comm is wire as follows:
//Mega (RX1) to Uno (TX)
//Mega (TX1) to Uno (RX)
//Mega (GND) to Uno (GND)
// Sat Comm Variables
SoftwareSerial nss(11, 12); // RXD to pin 11, TXD to pin 12
IridiumSBD isbd(nss, 8); // On/Off to pin 8, 5V in to 5Volt pin, GND to GND
static const int ledPin = 13;
int err;
int signalQuality = -1;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
nss.begin(19200);
isbd.attachConsole(Serial);
isbd.setPowerProfile(1);
isbd.begin();
}
void loop()
{
digitalWrite(ledPin, HIGH);
// Place call to Serial read function
// function should read from the mega/uno serial
// and store the variables in there corrisponding palces.
//sets up message
Packer();
// timer for the sat comm send interval
if (millis()- Send_Timer >= 1000)
{
//delay(1000);
Run_Time_Sec++;
Send_Timer = millis();
Serial.print("Time is: ");
Serial.print(Run_Time_Min);
Serial.print("Min and ");
Serial.print(Run_Time_Sec);
Serial.println("Sec");
}
if (Run_Time_Sec == 60)
{
Run_Time_Min++;
Run_Time_Sec = 0;
err = isbd.getSignalQuality(signalQuality);
if (err != 0)
{
Serial.print("SignalQuality failed: error ");
Serial.println(err);
return;
}
Serial.print("Signal quality is ");
Serial.println(signalQuality);
}
if (Run_Time_Min == Time_Displacment)
{
//Data_Sender();
Run_Time_Sec = 0;
Run_Time_Min = 0;
}
}
bool ISBDCallback()
{
digitalWrite(ledPin, (millis() / 1000) % 2 == 1 ? HIGH : LOW);
return true;
}
void Data_Sender()
{
err = isbd.sendSBDText(Text);
Serial.println("Data sent");
if (err != 0)
{
Serial.print("sendSBDText failed: error ");
Serial.println(err);
return;
}
Serial.println("Hey, it worked!");
Serial.print("Messages left: ");
Serial.println(isbd.getWaitingMessageCount());
}
void Packer()
{
if (Serial.available() > 0)
{
Message = Serial.readString();
Message.toCharArray(Text, sizeof(Text));
}
Serial.println(Message);
Serial.println("");
Serial.println(Text);
Serial.println("");
}
Mega Timer Code:
void Lap_Timer (void)
{
// once button is pressed set the starting point to the new controller run time.
if (Screen_Input == 1)
{
Timer_Start_Stop = true;
}
if (Screen_Input == 0)
{
Timer_Start_Stop = false;
}
// If the timer start/stop button is set to on then run the clock.
if (Timer_Start_Stop == true)
{
if (millis ()- start_point >= 1000)
{
Seconds++;
start_point = millis ();
}
if (Seconds == 60)
{
Minutes ++;
Seconds = 0;
}
}
// update the screen.
genie.WriteObject (GENIE_OBJ_LED_DIGITS, 0, Seconds);
genie.WriteObject (GENIE_OBJ_LED_DIGITS, 1, Minutes);
genie.WriteObject (GENIE_OBJ_LED_DIGITS, 2, N_Laps);
}// End Lap_Timer function.
Mega 4D Systems Screen Event Handler Code:
// Function handels all touch screen events.
void myGenieEventHandler(void)
{
genieFrame Event; // gets the event from the frame
genie.DequeueEvent(&Event); // Remove this event from the queue
if (Event.reportObject.object == GENIE_OBJ_4DBUTTON) // If this event is from a Slider
{
// if an event happens with an object index of two change the value of Screen_Input.
if (Event.reportObject.index == 2)
{
Screen_Input = genie.GetEventData(&Event); //Toggle Button
}
// if an event happens with an object index of four, perfrom the following actions:
// reset start point, increment the lap number, reset seconds, and reset minutes.
if (Event.reportObject.index == 4)
{
Reset = genie.GetEventData(&Event); //Reset Button;
start_point = millis();
N_Laps++;
Seconds = 0;
Minutes = 0;
}
}//end event if.
}// end myGenieEventHandler