Okay , I see exactly what you mean.
But suddenly I have to take into account that the first reading my oldbuffer will be empty , so to avoid that there is an action to first reading I must say that the buffer 2 takes the value of buffer 1 ?
But suddenly I have to take into account that the first reading my oldbuffer will be empty , so to avoid that there is an action to first reading I must say that the buffer 2 takes the value of buffer 1 ?
I don't see why. The first time data arrives, it is different from what was already known, right? Shouldn't you take action when the data is different?
Or, is the idea to do something only when you have two values and they are different?
When it will read the first sequence . cmdBuffer = sequence and oldbuffer = empty , so it will trigger an action. If I do not want it triggers the first time , I have to say that the cmdBuffer = OldBuffer first time
Or does not depend on the starting content of the buffer.
I come to you to thank you for the help you bring me , I could finally implement what I needed your help, I continued my project that is doing well . thank you
If you now would have had the decency to use code tags when you kicked a eight-or-so months old thread, we would maybe accept the fact that you use String (with capital S).
Yes, String is simple to use; and yes, String can leave holes in your memory and can cause all kinds of memory issues.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have been reading a lot in these threads, but i can't get a clean start with my little project...
So maybe you can help.
I have a device that generates a string that holds timing info, every second.
i want to use this info to signal a light, buzzer or even display it on LCD or something.
With a serial port monitoring program i got this :
Data : 41 30 30 30 30 0d ==> Character info A0000.
Data : 41 30 30 30 31 0d ==> Character info A0001.
Data : 41 30 30 30 32 0d ==> Character info A0002.
Data : 41 34 34 35 39 0d ==> Character info A4459.
So you can read the for characters as time 00:00 to 44:49.
But so far, i've been unsuccesfull...
How can i get those 4 bytes as a value (separate) so i can process them ?
#define array_length 6
byte myArray[array_length];
int i;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
for (i = 0; i < array_length; i++) {
myArray[i] = Serial.read();
}
Serial.flush();
for (i = 0; i < array_length; i++) {
Serial.print("Value");
Serial.print(i);
Serial.print("=");
Serial.print(myArray[i]);
Serial.println();
}
Serial.println();
}
delay (50);
// do something, calculate or display.
}
I hope you can push me in the right direction.
Thanks, Nick
Keep in mind that serial communication is pretty s___l___o___w.
So it takes quite a long time until (all) characters of a serial message are received.
In your code you try to read 6 characters (array_length) whenever there is one (or more) characters received. This may not work properly in most cases, because there are less than 6 characters available (at this time).
Try to read and understand the code in Serial Input Basics - updated / Example 3 as mentioned in #32
The code there shows reliable ways of reading serial messages.
I use this kind of code in many of my own projects with success.
Thanks, did some reading and i tried another function, "Serial.readBytesUntil".
This seems to work much better.
char buffer[6];
int myMinutes;
int mySeconds;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
while(!Serial.available());
int size = Serial.readBytesUntil('\n', buffer, 6);
for (int i = 0 ; i < size ; i++) {
// debug to screen, to see what was received
Serial.print(buffer[i], HEX);
Serial.print(",");
}
Processing_Data();
}
void showNewData() {
Serial.println();
Serial.print(myMinutes);
Serial.print(" - ");
Serial.print(mySeconds);
Serial.println();
}
void Processing_Data(){
// convert ascii to separate variables
int myMinutes = (10 * (buffer[1] - '0') + (buffer[2] - '0'));
int mySeconds = (10 * (buffer[3] - '0') + (buffer[4] - '0'));
showNewData();
}
void Write_to_Display(){
// write to external display and test for leading zero's.
}
Only the output for the conversion is '0' and '0' (zero)
This remains an issue for me.
Also code doen't work :
digit_1={atoi(buffer[1])}; // digit #1 on large display (=ten minutes indicator)
digit_2={atoi(buffer[2])}; // digit #2 on large display (=one minute indicator)
digit_3={atoi(buffer[3])}; // digit #3 on large display (=ten second indicator)
digit_4={atoi(buffer[4])}; // digit #4 on large display (=one second indicator)
Otherwise you have 2 "versions" of variables "myMinutes" and "mySeconds". The global ones, declared at the top of the setch, and local variables in function "Processing_Data". Local variables are only visible to the function in which they are declared.