I want to compare the received serial 8 bit binary data with another constant 8 bit binary data.if serially received binary data 11101110 then it will display "valid data" if received binary data 11111111 then it will display "invalid data'. I have written the code but condition is not satisfied after matching data. kindly check my code.
byte inData[24]; // Make this big enough to hold the entire event
byte index; // Use this to keep track of where the next byte goes
void setup()
{
Serial.begin(9600);
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
Data is invalid
actually not comparing with serially received data 11101110. what should i do?
Post your code, using code tags ("<>" button). Please edit your original post to include code tags.
What do you think BIN is doing here?
inData[index] = iByte,BIN;
The following line is not valid, as you are comparing a byte (0 to 255) to a large decimal integer which, incidentally, may not be 11101110, as the default for integers is a 16 bit integer (-32768 to 32767). To specify a byte value, use 0b11101110;
as per your suggestion i have changed the code:
<byte inData[24]; // Make this big enough to hold the entire event
byte index; // Use this to keep track of where the next byte goes
void setup()
{
Serial.begin(9600);
So far, so good. It will get 'stuck' in this loop while there is still serial data in the buffer. However you must realise that the Arduino processor will work much, much faster than serial. The main loop() code will execute thousands of times in between each serial character arriving, even if your sender sends with no gaps.
This reads one byte from Serial and then copies this same byte into the even positions of inData. (0, 2, 4, 6, 8, 10, 12, 14, 16) You probably didn't intend to increment index twice.
Also not that by declaring int index=0 you have created a new local variable which will not be accessible after the loop finishes.
Serial.write(inData[index]);
Which index do you think this is referring to? The local variable that was used inside the for() loop or the global variable which was declared at the top and has never had a value assigned to it?
if (inData[index] == 0b11111111)
Serial.println("invalid data");
You know I'm actually surprised that this didn't work, because all incoming bytes are copied to inData[0] and then this code will look at that position in the array.
Are you sure your data is coming in in bytes? Perhaps it's the characters '0' and '1' being typed in?
thank you for explanation.Actually i am new in arduino. With the help of forum i am trying to learn the language. what should be the actual code? arduino is receiving binary bit serially one by one total 8 bit and after receiving 8 bits it will display received 8 bits and compares with 11101110 and show result.
arduino is receiving binary bit serially one by one total 8 bit and after receiving 8 bits it will display received 8 bits and compares with 11101110 and show result.
In that case then this is the sort of thing you need to do.
As each byte comes in you accumulate it in one variable. You do this by inclusive ORing the bit received, shifted to the left by the number of bytes received so far, with what you have already received.
This is how I would do it but it is not tested as I have no simply way of sending a hex value of 0 and 1 like you say is happening. This means you are not using the arduino serial monitor but something else. If you are using the serial monitor then you are not sending hex values like you said but ASCII values and this code will not work.
byte index;
int data;
void setup()
{
Serial.begin(9600);
}
void loop() {
index = 0;
data = 0;
boolean gotData = false;
while (Serial.available()>0){
gotData = true;
data |= (Serial.read() << index);
index++ ;
delay(4); // very bad technique do not use normally
}
if( gotData){
Serial.print(data,BIN);
if (data == 0b11101110)
Serial.println("valid data");
else
Serial.println("invalid data");
}
}
when arduino receiving 11101110 serial monitor showing:
110001invalid data
110001invalid data
110001invalid data
110000invalid data
110001invalid data
110001invalid data
110001invalid data
110000invalid data
data is coming in bytes. I am attaching transmitter side code:
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(1, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("ENCODING OUTPUT");
Serial.println();
}
void loop() {
// get any incoming bytes:
if (Serial.available() > 0) {
int thisChar = Serial.read();
if (thisChar == '1') {
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
Serial.print("1");
delay(1000);
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
Serial.print("1");
delay(1000);
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
Serial.print("1");
delay(1000);
digitalWrite(13,LOW);
digitalWrite(1,LOW);
Serial.print("0");
delay(1000);
}
}
i am giving input in serial monitor 11 then output 11101110. these data is received in another arduino
No wonder you can't get it to work, you have no idea how the data is being sent.
Serial.print("1");
Sends the hex number 0x31 or the decimal number 49 to the serial port not a 1. The same goes for the zero, it sends 0x30 or 48 decimal.
Next there is a socking great delay between sending the individual numbers. This causes the receive side to see no more data in the buffer and so the
while (Serial.available()>0){
Ends and what has been received is assumed to be the whole data.
Also sending the message "ENCODING OUTPUT" completely screws up the receive side as you are not taking it into account so you don't know where these numbers start and end. In short your data is out of sync.
Finally why on earth are you sending this information in such a strange way?
I know i have little bit knowledge about arduino. I would request you to help me in writing the code. I have two arduino boards, both are connected to RF modem. one is transmitter another is receiver.
But I want to send High and low pulses serially to the Rx. If I send 1(one) then 1000x3=3000ms will be High and 1000ms will be low. similarly receiver section will decode these pulse and compare it.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(1, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("ENCODING OUTPUT");
Serial.println();
}
void loop() {
// get any incoming bytes:
if (Serial.available() > 0) {
int thisChar = Serial.read();
if (thisChar == '1') {
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
// Serial.print("1");
delay(1000);
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
Serial.print("1");
delay(1000);
digitalWrite(13,HIGH);
digitalWrite(1,HIGH);
// Serial.print("1");
delay(1000);
digitalWrite(13,LOW);
digitalWrite(1,LOW);
Serial.write("1110");
delay(1000);
}
}
}
in Receiver section:
byte inData[24]; // Make this big enough to hold the entire event
byte index;
void setup()
{
Serial.begin(9600);
}
void loop() {
while (Serial.available()>0){
byte iByte = Serial.read();
for(int index=0; index<8;index++)
{
inData[index] = iByte;
index++ ;
}
Serial.write(inData[index]);
}
if (inData[index] == 0b11101110)
Serial.println("valid data");
if (inData[index] == 0b11111111)
Serial.println("invalid data");
}
I want to encode the each bit and decode each bit in receiver section for that reason i am sending each bit serially one by one.
You should not be doing this. Pin 1 is what is used for the serial signal, if you have done a Serial.begin then writing to pin 1 will have no effect.
I think you are misunderstanding the basic idea of your project. Do not send data that is timing specific there is simply no need to do this, and I can't reconcile this requirement with what you started the thread asking.
I have two arduino boards, both are connected to RF modem.
What RF modem?
What you should be doing is to send a byte or bytes that contain the information you want. Then the receive end will decode this information and send out pulses to whatever according to this data. Do not clog the RF channel by continuously sending data. Only send data when you want something to change at the receiving end.
Look at something like the virtual wire library to send data if the modem will not do.