Serial Programming Question

So I have a master and a slave Arduino set up(There will be multiple slaves once I have this figured out). On the slave eventually Receiving will be used to match the address to the constant stored on each address. Then there will be additional code that will happen if the address matches(The idea is this will allow me to address a specific Arduino unit.

With what I have now I just want it to print the address that the master sends but it’s not doing so. What am I doing wrong?

//Master
#include <SoftwareSerial.h>

//Serial Receive and Transmit Pins
const int SerialRX = 2;
const int SerialTX = 3;
const int SerialControl = 4;

//Quick Change Control Variables for Serial.
const int SerialTransmit = HIGH;
const int SerialReceive = LOW;
boolean Receiving = false;
boolean Transmitting = false;

//List of Known Arduinos
char Arduino1[13] = "05150E100E2C";
char Arduino2[13] = "0515BCE5E8EB";

//Declaring the Serial Object
SoftwareSerial RS485Serial(SerialRX, SerialTX);

//Variables for Send and Receiving. 
char ReceivedData[13];
char ReceivedChar;
byte ReceivingIndex = 0; 


void setup()
{
	Serial.begin(9600);
	Serial.println("Master Arduino.");
	pinMode(SerialControl, OUTPUT);
	digitalWrite(SerialControl, SerialTransmit);
	Transmitting = true;
	RS485Serial.begin(9600);
}

void loop()
{
	if (Receiving == true)
	{
	}
	else if (Transmitting == true)
	{
		for(int i=0; i<12; i++)
		{
			RS485Serial.write(Arduino1[i]);
		} 
		delay(10000);
	}
}
//Slave
#include <SoftwareSerial.h>

//Serial Receive and Transmit Pins
const int SerialRX = 2;
const int SerialTX = 3;
const int SerialControl = 4;

//Quick Change Control Variables for Serial.
const int SerialTransmit = HIGH;
const int SerialReceive = LOW;
boolean Listening = false;
boolean Receiving = false;
boolean Transmitting = false;


//Declaring the Serial Object
SoftwareSerial RS485Serial(SerialRX, SerialTX);

//Variables for Send and Receiving. 
char ReceivedData[13];
char ReceivedChar;
byte ReceivingIndex = 0; 

//Variables to determine which Slave the Master is talking to.
char Arduino1[13] = "05150E100E2C";

void setup()
{
	Serial.begin(9600);
	Serial.println("Slave Arduino.");
	pinMode(SerialControl, OUTPUT);
	digitalWrite(SerialControl, SerialReceive);
	Listening = true;
	RS485Serial.begin(9600);
}

void loop()
{
	if (Listening == true)
	{
		while(RS485Serial.available() > 0) 
		{
                        Receiving = true;
			if(ReceivingIndex < 12) // One less than the size of the array
			{
				ReceivedChar = RS485Serial.read(); // Read a character
				ReceivedData[ReceivingIndex] = ReceivedChar; // Store it
				ReceivingIndex++; // Increment where to write next
				ReceivedData[ReceivingIndex] = '\0'; // Null terminate the string
			}
		}
		if(Receiving == true)
		{
			for(int i=0; i<13; i++)
			{
				Serial.print(ReceivedData[i]);
			} 
			Serial.println("\n");
			Receiving = false;
		}
		
		// Now do something with the string (but not using ==)
	}
	else if (Transmitting == true)
	{
		if (RS485Serial.available())
		{
			
		}
	}
}

I don't see the variable 'Value1' in your Master code:

			RS485Serial.write(Valve1[i]);

Originally designed to just be going to a garden valve but I want another Arduino in an outside weatherproof enclosure to display stats so I changed it to Arduino since not all slaves will be a valve. It failed to compile so I had already changed it and hadn't made the change here yet.

It still fails to print anything on the Slave's Serial monitor(Other than the Statement say "Slave Arduino".

In your case I will add some “debug prints” only to see where it really fails. Something like this:

void loop()
{
	if (Listening == true)
	{
                Serial.println("Listening...");
		while(RS485Serial.available() > 0)
		{
                        Serial.println("Receiving...");

                        Receiving = true;
			if(ReceivingIndex < 12) // One less than the size of the array
			{
				ReceivedChar = RS485Serial.read(); // Read a character
				ReceivedData[ReceivingIndex] = ReceivedChar; // Store it
				ReceivingIndex++; // Increment where to write next
				ReceivedData[ReceivingIndex] = '\0'; // Null terminate the string
			}
		}
		if(Receiving == true)
		{
			for(int i=0; i<13; i++)
			{
				Serial.print(ReceivedData[i]);
			}
			Serial.println("\n");
			Receiving = false;
		}
		
		// Now do something with the string (but not using ==)
	}
	else if (Transmitting == true)
	{
		if (RS485Serial.available())
		{
			
		}
	}
}

Did you receive any data from the Master? Did you cross the Tx and Rx?

Hmmm.... It finally printed the key I sent from Master. I didn't change a thing except adding your debugs. I removed the Listening once since it was spamming like crazy. It seems to work once.

Slave Arduino.
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
Receiving...
05150E100E2C

After that there is a pause, I'm assuming is from my delay in the Master and then it spams Receiving and never prints out the code again.

I found it! I never set the ReceivingIndex back to 0. Now it works perfectly every 10 seconds!

Quick Question. When I hook up more than one Arduino will they all have their own incoming queues waiting for the .read() command? That way the first one doesn’t erase the command stating that address of the intended slave?

Yes, I think that they all will have their own incoming queues and they all will "listen" the same message. After that you will need to program the behaviour of the response.

Working on that now, thanks!

To compare the two strings do I need to add the null to my constant address on each slave? Or since I only declared 12 characters is number 13 automatically null?

char Arduino1[13] = "05150E100E2C";

No. In that case, the null character is added autocratically by the language.

while(RS485Serial.available() > 0)

This is not reliable because the Arduino can work much faster than data arrives.

Have a look at the examples in serial input basics which check for an end marker (or start and end markers) to be sure that all the data has arrived.

...R

luisilva:
No. In that case, the null character is added autocratically by the language.

Ok thank you!

Robin2:

while(RS485Serial.available() > 0)

This is not reliable because the Arduino can work much faster than data arrives.

Have a look at the examples in serial input basics which check for an end marker (or start and end markers) to be sure that all the data has arrived.

...R

I see that now after reading those posts. I'll implement a start and end marker.