hi there
I am coding a recent project of mine and have run into a problem where my code won't index an array correctly.
The code has been designed to sort values into an array to be used later.
I have checked the incoming values and they are what I am expecting and the communication code seems to be working fine but when I print the array index value to see what's going on I get crazy values as seen in the photo linked below. I want it to count up sequenchly till the array is full and then sent back to 0.
I have also linked the code below. (this includes the whole code but I have blocked out a lot of it as I was trying to narrow the possible faults points coursing this problem)
// slave node
#include <SoftwareSerial.h>
#define RS_RO 10
#define RS_DI 11
#define RS_DE_RE 12
SoftwareSerial RS_Slave(RS_RO , RS_DI); // TX , RX
// sorting and reseving incuming darta
//index 0 1 2
int controle_camandes [3] {3,3,3};
byte cc_index = 0;
int in_data;
// sorting and sending out going darta
//index 0 1 2 3 4
int senser_readings [5] {001,2,2,2,002};
byte sr_index = 0;
void setup() {
Serial.begin(9600);
RS_Slave.begin(9600);
pinMode(RS_DE_RE, OUTPUT);
// enabeal pin low to actvate reseve fucion
digitalWrite(RS_DE_RE,LOW);
Serial.println("slave node is ready");
}
void loop() {
/* // sending darta
digitalWrite(RS_DE_RE,HIGH);
for (sr_index = 0; sr_index < 5; sr_index ++)
{
RS_Slave.write(senser_readings[sr_index]);
}
digitalWrite(RS_DE_RE,LOW);
*/
//reseving and sorting darta
//001 = new transmicion started
//002 = end of transmicion
while (RS_Slave.available() && in_data != 2 )
{
in_data = RS_Slave.read();
//Serial.println (in_data);
if (in_data == 1)
{
//cc_index = 0;
// Serial.println ("reseving data");
}
if (in_data == 2)
{
// Serial.println ("all data reseved");
}
if (in_data != 1 || 2)
{
controle_camandes [cc_index] = in_data;
cc_index ++;
}
}
Serial.println (cc_index);
/*
if (cc_index > 5)
{
Serial.println ("dater format not exspected");
for (cc_index = 0; cc_index < 3; cc_index ++)
{
controle_camandes [cc_index] = 0;
}
}
*/ in_data = 0;
//end of comuncacions code
/*
for (cc_index = 0; cc_index < 3; cc_index ++)
{
Serial.println ( controle_camandes [cc_index] );
}
*/
//Serial.println ( controle_camandes [0] );
//other code
delay(1000);
}
