Inputing Multiple Values Thorugh Serial Monitor

So currently I am on work placement away from home and I do not have an arduino with me, however, I have a fair bit of spare time and a couple of projects that I want to finish in my short break so I decided to get the coding out of the way (I plan to use to emulate the arduino).

I am trying to input different variables through the serial monitor as if I was calling on an RTC. I had planned to either enter a string like <11> and set an hours value to 11 or <34> to set minutes to 34
I found someone’s code (sorry for the bad call out, I cant seem to find it again) and tried modifying the showNewData function at the bottom:

const byte numChars = 32;
char receivedChars[numChars];
boolean hrs = false;
char hoursinput = 'h';

boolean newData = false;

void setup() {
   Serial.println("<Arduino is ready>");

void loop() {

void recvWithStartEndMarkers() {
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '<';
   char endMarker = '>';
   char rc;

// if (Serial.available() > 0) {
   while (Serial.available() > 0 && newData == false) {
       rc =;

       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;

       else if (rc == startMarker) {
           recvInProgress = true;

void showNewData() {
   if (newData == true) {
     if (hrs == true){
       Serial.println("hours set to");
       hrs = false;
     if (receivedChars[] == 'h'){
       hrs = true;

I know this is probably beginner grade stuff and it will probably be something simple like declaring the wrong type of variable but it has been quite awhile since my last programming class. If there is a better way of doing it (say inputting ‘h8’ and somehow slicing the string after the h) please let me know.

Thanks in advanced,

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I guess you found the code in Serial Input Basics - updated

You don’t describe your problem; do we have to guess?

Your code seems to be missing a ‘}’ at the end.

    if (receivedChars[] == 'h')

What is this supposed to do?

You never clear the newData flag.

There are a few ways to do what you need. You can send a text like <h11,m30,s5> or you can send a text followed by a text followed by a text or

Let’s assume, as you described, you send , receivedChars[0] contains the character ‘h’, receivedChars[1] contains the character ‘1’, receivedChars[2] contains the character ‘2’ and receivedChars[3] contains the nul character.

So to check if one wants to set the hours, the code can check receivedChars[0], not receivedChars.

if(receivedChars[0] == 'h')

The hours are in the text after the ‘h’ as a string (lower case s). You can print them easily using the second element of the receivedChars (remember arrays are indexed from 0, so the second element has the index 1)


As you (might) know, when printing a string, Serial.println requires a pointer to (or address of) the first element of the text to print. Usually you use receivedChars which is equivalent to &receivedChars[0]. This is read as

|      |       |
|      |       +-- index of element
|      +---------- array
+----------------- address of

So the Serial.println above will take the address of the second element (index 1) and print that and the text following (till it encounter the nul character). So the printed result is 12.

You can use the function atoi() to convert the text “12” to the number 12

int hour = atoi(&receivedChars[1]);

With that approach, you do not need to keep a flag to indicate that you want to set the hours; it could look like

  int hours = atoi(receivedChars[0]);

Possibly nicer will be to use a switch

void showNewData
  // check if new message is complete
  if(newData == false)
    // nothing to do

  // convert received value  
  int value = atoi(&receivedChars[1]);

    case 'h':
    case 'm':
    case 's':
      Serial.println("Invalid option");

  newData = false;

void setHours(int hours)
  // do whatever you have to do to set the hours in the RTC

void setMinutes(int minutes)
  // do whatever you have to do to set the minutes in the RTC

void setSeconds(int seconds)
  // do whatever you have to do to set the seconds in the RTC

not compiled

Ahh.. I did not know that a point was needed to read an index, that explains why i was getting invalid index errors (that's why i had receivedChars[], i was messing around and forgot to change it back :P). But now it works perfectly thank you for your help :)