Hi, in this code I am reading from Serial, storing values in global variables and printing the global variables values to serial. When iterating for the first time the global variables retain thier values and are correctly printed on serial, however in the subsequent loops global variables loose their values. for example: if last name is: Kanwar in first loop it prints correctly, but in subsequent loop is prints as: anwar. i.e K gets lost from global char array.
(Problem is mainly occuring in lName[16] (i.e last name), zones[6] & daysRestricted[7] global variables)
Attaching my code as file as in tag limit is exceeding.
Any suggestions are much appreciated!
Thanks & Best Regards
Attacment: Code file (.ino)
Name: SerialRW
Serial monitor: (Input and Output)
Enter A to Add, Enter B to Block card OR X to Exit:
Enter 8-digit New Card number (Hexadecimal as AABBCCDD) or enter 'X' to exit : AA BB CC DD
Enter First Name: LOKESH
Enter Last Name: KANWAR
Enter Zones(1,2,3,4,5,6) allowed for this card:
Zones allowed: 123465
Enter Entry Time in HHMM: 01 00
Enter EXIT Time in HHMM: 21 00
Restriced Days:
Monday -> 1, Tuesday->2, Wednesday->3, Thursday->4, Friday->5, Saturday->6, Sunday->7
For Example to restric Entry on Saturday & Sunday, Enter: 67. To restrict Entry on Wednesday, Friday & Sunday Enter: 357
Enter Days on which Entry restricted:
167
Card Number Entered:
AA BB CC DD
First Name: LOKESH
Last Name: ANWAR
Zones allowed: 123
Entry Time: 01 00
Maximum Entry Time: 21 00
Days on which entry NOT allowed: 167
Rather than making folks wade through 16+K of code, most of which is probably unrelated to your problem, you might get more help by posting an MRE. The is the smallest code possible that compiles and demonstrates your problem and only your problem. It includes no extraneous code unrelated to the problem. In fact, many times while in the process of creating the MRE, you'll find the problem yourself.
for example: if last name is: Kanwar in first loop it prints correctly, but in subsequent loop is prints as: anwar. i.e K gets lost from global char array.
It sounds very much like you are writing past the end of an array
gfvalvo:
Rather than making folks wade through 16+K of code, most of which is probably unrelated to your problem, you might get more help by posting an MRE. The is the smallest code possible that compiles and demonstrates your problem and only your problem. It includes no extraneous code unrelated to the problem. In fact, many times while in the process of creating the MRE, you'll find the problem yourself.
Thanks, for the informaion about MRE. It does helps to break down the working code and find the error.
UKHeliBob:
It sounds very much like you are writing past the end of an array
Spot on! I guess that is what experience is. I was indeed overwriting the same variable twice and in another function, due to which another two different variables were being overwritten in the memory.
Solved! Thanks a lot.
In whatToDo() is a byte array with size 1. Then readFromSerial() is called with that single byte array, which calls Serial.readBytesUntil().
Do you know if the .readBytesUntil() will read just one byte and add a zero terminator, or if it only returns a zero-terminator or if there is no zero-terminator at all ?
The Serial port is restarted sometimes:
Serial.end();
Serial.begin(9600);
I don't know if that is okay.
There are "while(1)" and other "while" statements. We don't do that. We let the loop() run as often and as fast as possible and capture and process data when it comes in.
The strtol() is called with 'tempTimeArray', but its size is only 2 and it is not a zero-terminated string.
readBytesUntil check two conditions, terminating character and size of the array i.e which ever comes first
There are instances the Serial read some times adds terminating character and sometimes dosen't, even if it doesn't I check the whole array and do error check.
Serial.flush() as I read is not supported, so I read somewhere and used this method "Serial.end(); Serial.begin(baudRate);" to clear the garbage from Serial, I have found that it does removed unwanted characters from serial, mostly when we write to Serial using print, after that Serial.read does returns garbage. By using it I have not faced any problem at all.
While (1) in my case I used so that loop runs till I break the loop by using return in a function. I could have used a variable and had done counter check, but I think if I am always returning something from a function then there is no harm in using while(1), because loop will break when conditions are met.
if strtol() does not finds terminating character at the end of the array, then it stores/returns the address of first valid number, in case the array is not valid it returns 0. So, in my case strtol() works perfectly fine.
I appreciate you having the look at my code. I have although solved the problem which I was facing, as mentioned in earlier posts..
P.S: In the code, In the function daysRestricted I had used the array "zones" by mistake which was creating havoc and wasted my 8 hours..