Hi. I am sure the answer to this is simple, I just can't work it out. (If anyone read this post straight after I posted it, apologies posted the wrong transmitter code. Corrected now)
I am new to coding and have learnt quite a bit over the last few months and now trying something a bit more ambitious. I have 1 Nano connected to 8 IR sensors. Based on the the status of these sensors, I want to communicate 3 different states to a 2nd Arduino via the serial comms bus. I decided to use int values to show the sensor state:
int sensorValue1 = 0 //sensor inactive;
int sensorValue1 = 4 //sensor has been triggered;
int sensorValue1= 16 //sensor active but not triggered;
On the receiver side, I need to do calculations with these int values.
The serial comms code I am using has been borrowed and modified for may application. I don't fully understand it, but it works if I want to display the output on my laptop. data1, data2, etc are however strings and I can't use them in calculations. I have spend hours trying to figure out how I can convert these to integers, so that the rest of my code can work with the calculations. Can anyone help please?
Transmitter code (Nano)
int rowStatus1 = 0; // Sensor status, with test values
int rowStatus2 = 4; // Sensor status, with test values
int rowStatus3 = 16; // Sensor status, with test values
int rowStatus4 = 0; // Sensor status, with test values
int rowStatus5 = 4; // Sensor status, with test values
int rowStatus6 = 16; // Sensor status, with test values
int rowStatus7 = 0; // Sensor status, with test values
int rowStatus8 = 4; // Sensor status, with test values
int serialDelay = 5000;
void setup()
{
Serial.begin(9600); // Open serial comms to arduino Nano
}
void loop() // run over and over
{
Serial.print(rowStatus1); Serial.print ("A");
Serial.print(rowStatus2); Serial.print ("B");
Serial.print(rowStatus3); Serial.print ("C");
Serial.print(rowStatus4); Serial.print ("D");
Serial.print(rowStatus5); Serial.print ("E");
Serial.print(rowStatus6); Serial.print ("F");
Serial.print(rowStatus7); Serial.print ("G");
Serial.print(rowStatus8); Serial.print ("H");
Serial.print ('\n');
delay (serialDelay);
if (Serial.available())
Serial.write(Serial.read());
}
Receiver code using a Mega for now for debugging, but will use Nano later:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
char c;
String dataIn;
int8_t indexOfA, indexOfB, indexOfC, indexOfD, indexOfE, indexOfF,
indexOfG, indexOfH;
String data1, data2, data3, data4, data5, data6,
data7, data8;
int rowStatus1 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus2 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus3 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus4 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus5 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus6 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus7 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
int rowStatus8 = 0; // Read from serial port. 0 = row not active, 4 = sensor triggered, 16 = Aactive but not triggered
void setup()
{
Serial.begin(9600);// Open serial communications to PC
mySerial.begin (9600);
Parse_the_Data();
}
void loop()
{
while(mySerial.available()>0)
{
c = mySerial.read();
if(c=='\n')
{
break;
}
else
{
dataIn+=c;
}
}
if(c=='\n')
{
Parse_the_Data();
//Show data on Serial Monitor
Serial.println("Data4 = " + data4);
Serial.println("Data4 = " + data4);
Serial.println("Data4 = " + data4);
Serial.println("Data4 = " + data4);
Serial.println("Data5 = " + data5);
Serial.println("Data6 = " + data6);
Serial.println("Data7 = " + data7);
Serial.println("Data8 = " + data8);
Serial.println("===============================================");
c=0; //reset the variable
dataIn="";
}
}
///////////////////////////////////////////////////////////////////////////////////////////
void Parse_the_Data()
{
indexOfA = dataIn.indexOf("A");
indexOfB = dataIn.indexOf("B");
indexOfC = dataIn.indexOf("C");
indexOfD = dataIn.indexOf("D");
indexOfE = dataIn.indexOf("E");
indexOfF = dataIn.indexOf("F");
indexOfG = dataIn.indexOf("G");
indexOfH = dataIn.indexOf("H");
data1 = dataIn.substring (0, indexOfA);
data2 = dataIn.substring (indexOfA+1, indexOfB);
data3 = dataIn.substring (indexOfB+1, indexOfC);
data4 = dataIn.substring (indexOfC+1, indexOfD);
data5 = dataIn.substring (indexOfD+1, indexOfE);
data6 = dataIn.substring (indexOfE+1, indexOfF);
data7 = dataIn.substring (indexOfF+1, indexOfG);
data8 = dataIn.substring (indexOfG+1, indexOfH);
}
I want to convert the data strings to my rowStatus integers.
Thanks
Norman