I have used initially the RF modules like with the 433 MHz transmitter and receiver that time the virtual wire library handled the receiving of messages from the transmitter, i have now picked up the XBee although they are not with me at this time i have already picked the development in code terms, so i made the following code adding the buffer array accumulator function that was originally written by pauls and other of my own, the problem is it works one time , the characters or words entered in the serial turn on as strings only and everything works but when one second time enters any character or word the characters just get lost.
following is the code:
char inData[20]; // Allocate some space for the string
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int val[4];
int slaveSel = 0;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
for (int pinNumber = 3; pinNumber <= 7; pinNumber++)
{//function to read a jumper out of 5 jumpers
pinMode(pinNumber, INPUT);
for (int i = 0; i <= 4; i++)
{
val[i] = digitalRead(pinNumber);
if (val[i] == 1){ slaveSel = pinNumber;}
}
}
}
void loop()
{
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
String Data = inData;
if (Data == "on")
{
digitalWrite(13, HIGH);
index = 0;
for(int i=0;i<19;i++)
{
inData[i]=0;
}
}
if (Data == "off")
{
digitalWrite(13, LOW);
index = 0;
for(int i=0;i<19;i++)
{
inData[i]=0;
}
}
}
The actual one below behaves Ditto:
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup(){
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This){
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if(strcmp(inData,This) == 0){
for(int i=0;i<19;i++){
inData[i]=0;
}
index=0;
return(0);
}
else{
return(1);
}
}
void loop()
{
if(Comp("m1 on")==0){
Serial.write("Motor 1 -> Online\n");
}
if(Comp("m1 off")==0){
Serial.write("Motor 1 -> Offline\n");
}
}
What i basically intend to do is read the words as they are sent from the wireless radio pipe type connection of Radio such as XBee's or Bluetooth
for (int i = 0; i <= 4; i++)
{
val[i] = digitalRead(pinNumber);
if (val[i] == 1){ slaveSel = pinNumber;}
}
Why are you reading the same pin 5 times, and storing the value in an array large enough to hold 4 values? Do you expect that the value for that pin to change?
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
Where is the serial data coming from? If it is from the radio, why is the radio connected to the debug pins? Move the radio to two other pins, and use NewSoftSerial/SoftwareSerial to read those pins. Then, you can use the hardware serial port to print debug information.
The 433 MHz radios are cheap, but they are not very reliable. They are subject to a lot of interference. You need to know what the radio is actually receiving.
Why are you reading the same pin 5 times, and storing the value in an array large enough to hold 4 values? Do you expect that the value for that pin to change?
Corrected the code as below:
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
for (int pinNumber = 3; pinNumber <= 7; pinNumber++)
{//function to read a jumper out of 5 jumpers , the jumper that is connected to a pin ,that pinNumber is saved under a variable slaveSel
pinMode(pinNumber, INPUT);
val[pinNumber] = digitalRead(pinNumber);
if (val[pinNumber] == 1){ slaveSel = pinNumber;}// loads the pinNumber actually active sue to jumper on it in slaveSel variable
}
}
Where is the serial data coming from? If it is from the radio, why is the radio connected to the debug pins?
Im forming the code for the XBee's im yet to receive and im getting that Serial data from the Serial Monitor, just checking.
Yes thanks for guiding i'll use the NewSoftwareSerial with the XBee's.
You don't need the array at all. All you want to do is set slaveSel to pinNumber if analogRead(pinNumber) returns HIGH.
and im getting that Serial data from the Serial Monitor, just checking.
OK. Then you CAN use Serial.print() to debug the code. If the radio was what you were getting data from, sending data out would not work.
So, what is in inData when the while loop ends? Probably not what you think is in it.
You are typing some data in the Serial Monitor, and hitting send. You are expecting that the data arrives at the Arduino nearly instantly, when that is no where near what really happens. Data arrives at the serial port like I type - very slowly (but, at least it doesn't hammer the backspace key nearly as often as every other letter combined).
You need to read data until the end of packet marker arrives BEFORE trying to process the data. Using other than the serial monitor, I'd suggest something like "<ThisIsAPacket>" where < and > are the start and end of packet markers. With the Serial Monitor, however, it is hard to expect the user to type the start and end markers. Fortunately, the start marker is optional, and the Serial Monitor can append a carriage return/line feed to the data sent, which can then be used as the end of packet marker.
All you want to do is set slaveSel to pinNumber if analogRead(pinNumber) returns HIGH
Done Below:
for (int pinNumber = 3; pinNumber <= 7; pinNumber++)
{//function to read a jumper out of 5 jumpers
pinMode(pinNumber, INPUT);
if (digitalRead(pinNumber)) {
slaveSel = pinNumber;
}
}
You are expecting that the data arrives at the Arduino nearly instantly
The data arrives one by one byte by byte like in case of "work" w then o then r then k (correct me if im wrong, sir)
"" where < and > are the start and end of packet markers.
So at the end of the string i want to send i must do like(in the Serial Monitor? dn;t have other terminal programme) for example for "work" i have to put in work> (end marker a necessity and start marker not ,as you said)
Thank you sir, i hope this will work to much extent, However NewSoftwareSerial libraries benefactor would be revealed when i get the XBee's and do stuff on it.
The data arrives one by one byte by byte like in case of "work" w then o then r then k (correct me if im wrong, sir)
Yes, it does. There may be many, many iterations of loop() between characters.
So at the end of the string i want to send i must do like(in the Serial Monitor? dn;t have other terminal programme) for example for "work" i have to put in work> (end marker a necessity and start marker not ,as you said)
You can have the Serial Monitor append a carriage return and line feed. Use one of them ('\n' or '\r') as the end of packet marker.
Using NewSoftSerial (0023 or earlier) or SoftwareSerial (1.0+) will require the same start/end of packet marker/processing.
What is the difference if I use the /n or /r or the > as end markers.
Just out of curiosity any peculiar reason.
From the code point of view, none. From the user's perspective, adding the \n and \r is automatic. Adding the > is not. When the other Arduino/XBee is sending the data, it won't matter what EOP marker you have it add.
After nearly 650 posts here, I'd expect a little more detail from you - saying it "isn't helping" isn't helping.
The details have worked out there way from above till here because the adjustments can't be applied and diagnosed in one post , the above adjustments have resulted in the result which says >>The leading marks like \n ,\r and even the complete packet <> isn't helping.<<
What do returning 0 or 1 as a char mean to you? Do you mean true or false?
The whole Comp function is fundamentally flawed. Serial data doesn't necessarily arrive in a chunk like that. You might get "m1" and later on " on". So neither will match "m1 on".
You need to buffer it up, and then when you get a terminator (as PaulS was saying) then you compare.
Mr.Nick ,Below is the code im utilising now(the /n or/r or <> packets are used within the serial monitor window while sending words to the array to group up as strings like >> "on>" or "on/n" or "" or "on/r"), the one you commented with is the code from which this below one is extracted ,im not using the genuine code:
char inData[20]; // Allocate some space for the string
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int val[4];
int slaveSel = 0;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
for (int pinNumber = 3; pinNumber <= 7; pinNumber++)
{//function to read a jumper out of 5 jumpers
pinMode(pinNumber, INPUT);
if (digitalRead(pinNumber)==1) {
slaveSel = pinNumber;
}
}
}
void loop()
{
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
String Data = inData;
if (Data == "on")
{
digitalWrite(13, HIGH);
index = 0;
Data = 0;
for(int i=0;i<19;i++)
{
inData[i]=0;
}
}
if (Data == "off")
{
digitalWrite(13, LOW);
index = 0;
for(int i=0;i<19;i++)
{
inData[i]=0;
}
}
}
const int bufferSize = 20;
char inData[bufferSize]; // Allocate some space for the string
Ok Sir i will incorporate contants
The whole Comp function is fundamentally flawed. Serial data doesn't necessarily arrive in a chunk like that. You might get "m1" and later on " on". So neither will match "m1 on".
The genuine code that i tried which you have commented works pretty well and it sends exactly "m1 on" this is proven by the working of the IF conditon but this happens only once this is the main problem that this happens only once ,either the serial is clogged with buffer or something else , i have tried out with Serial.flush(); but it flushes everything.
I have not build the code for handling the "onf" , i do not need to make code do anything in case onf is entered , Im just checking out whether what i send on the Serial Monitor is entering the code or not ? and it does pretty well for the first time but not the second time onwards.
The code you just posted is junk
Why i need to think to handle additional intricacies if even the basic one's aren't solved by adding handling the IF other things are entered when they simply won't be entered because its me only checking this stuff.
What if line noise corrupts a bit in one of your received characters?
Worth defining, Sir. but This is not happening simply because if this phenomenon would have been there then even the first one char array would have had a Ton of corruption which is not there at all proven by the IF condition works or else for the very first time also it wouldn;t have worked