hello,
i am using matlab to send byte by byte data to arduino (for example 12) and i need check its value in order to proceed but nothing happens. so how can i check these values.below is the code used for arduino
thanks
How many characters does it take to send 12? If you guessed 2, you are right. How many bytes does it take to send 12? 1 byte. So how is the data sent? And '12', look at your keyboard and find the 12 character. Single quotes for single characters, double quotes for strings. Robin2's serial input basics will help you understand serial communication.
Well, 12 will come in, from the Serial Monitor for example, as '1', '2' and maybe CR and/or LF if you have that selected at the bottom of the serial monitor.
So you need to do this:
if(Serial.available()>1 ) // got 2 bytes at least?
{
inchar10 = Serial.read() - 0x30; // read and convert to ASCII, see www.asciitable.com
if (inchar10 >= 0 && inchar10 <= 9){ // got a 0 to 9? Yes, read next number
inchar1 = Serial.read() - 0x30; // read and convert to ASCII, see www.asciitable.com
Serial.print(inchar10,HEX);
Serial.print (" ");
Serial.print(inchar1,HEX);
Serial.print (" ");
data = inchar10 *10 + inchar1;
Serial.print (data, HEX);
}
if(inchar==12)
{
Serial.println (" got my number!");
}
}
hello groundFungus,
i checked the thread but it didnot help as it deals with characters while i need to deal with bytes if there is any further suggestions it would be much appreciated.
I am not sure if this portion will work. 'inchar' is an integet. And you are trying to compare it with '12'. when you try to compare integer with character, it compares the acii code. but 12 is not a character and doesnt have an ascii code.
So try storing converting each byte to corresponding character and store it to a string. and at the end of the transmission compare the string.
I am not sure if this portion will work. 'inchar' is an integet. And you are trying to compare it with '12'. when you try to compare integer with character, it compares the acii code. but 12 is not a character and doesnt have an ascii code.
int x = '12';it may surprise you, but that's perfectly legal.