Struggling with Strings

While a PV Control system is operational, it would be really nice if the safety margin could be tweaked on-the-fly.  The ideal "sweet spot" could then be determined between importing and exporting.

Using  if (Serial.available() > 0), the Serial port could be routinely polled without interrupting normal operation.  What I would like to have is the ability to type "123" follwed by 'c/r', and for these characters to be converted into the decimal value 123. 

But string manipulation is not my forte.  Maybe someone can point me in the right direction please ...


stuart's picture

Re: Struggling with Strings

My code over at does something similar, but using up/down values instead of actual numbers.

calypso_rae's picture

Re: Struggling with Strings

Thanks Stuart, that's an impressive response time!

Yes, I can see how this works with individual characters.  This approach could presumably be used to nudge my safety margin up and down.  Maybe that would be the best way to do it, using just the  '+' or '-' keys followed by [cr].   Each time either character is entered, the new safety margin value could be displayed. 

I rather like the idea of entering, say, 100 and seeing how long it takes to go from importing to exporting; and then entering -100 and seeing if it takes the same amount of time to go from exporting to importing.  Any imbalance would be due to some inherent offset in my system which could then be calibrated out  using the approriate value.

I'm interested to see your interrupt code for switching a load at zero-crossing points.  My preferred way of doing this is to get a dedicated trigger device to do the actual detection, and get the Arduino to 'arm' it (or not) at the right point in each preceeding mains cycle. 

Robert Wall's picture

Re: Struggling with Strings

Serial.readBytesUntil(character, buffer, length)

followed up by atoi( ). This seems to work, returns zero for non-numeric input.

char inbuf[5] = {0,0,0,0,0};
int value;
int counter;
int chars;

void setup(void)
    counter = 1;                 // Just to show it's looping

void loop(void)
    chars = Serial.readBytesUntil('\n', inbuf, 5);
    Serial.print("counter = "); Serial.print(counter++);
    if (chars)
        value = atoi(inbuf);
        Serial.print("  input value = "); Serial.println(value);

You may need to change the terminating character ('\n') to suit the serial host/terminal.

calypso_rae's picture

Re: Struggling with Strings

Thanks Robert, works a treat!

(do you ever take a break ;)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.