Hello, I'm new to arduino programming and I dont have deep knowledge of C or C++ and I have encountered a problem in my code. Hope I've posted in the right section..
I've been trying a program to read an array of size 6 and verify it by comparing with a known string in order to proceed to another executable section.
My code is like this:
But doesn't seem to work. It is not reading the values but everytime the else part directly gets executed. Where have I gone wrong?
Thanks for any help.
Check that there is Serial data available, with the aptly named Serial.available(), returns -1 when there is no data.
Only then read into your array.
Read up to the size of your array-1 or until a set character like '\n' (line feed), whichever comes first, this leaves a gap for your null terminator, so your last element is 0.
When comparing, use strcmp to compare your arrays, 0 means they are equal. (Im assuming they are ascii arrays representing strings).
Thank you. But it is overriding the Serial.available()
Once it waits until I enter a character, then it becomes an infinite loop printing random characters.
Should I put the serial data check after or before the for loop?
char r[6]; int i;
while(Serial.available()>0)
for(i=0;i<6;i++)
r[i]=char(Serial.read());
You really need a more reliable way to receive serial data - it arrives much more slowly than an Arduino works.
One of the examples in serial input basics (perhaps the second one) should meet your needs.
When you have received the data and have saved it in a string (a char array with a terminating 0) you can compare it with another string using the strcmp() function.