instinctivepinoy
instinctivepinoy

Reputation: 9

How do I have the serial monitor of the Arduino show a string a value but only the value is changing?

I already have my code to work, I'm just trying to make it look nicer now. Just to give a really short summary on what my device does: Smart parking system that detects cars going in and out of a parking lot and displays how many vacant spots are available or not at the entrance. Right now the output looks like this:

Vacant Spots: 1
Vacant Spots: 1
Vacant Spots: 1
Vacant Spots: 1
Vacant Spots: 0
.
.
.
.
.

This is in the case of when a car is entering so it's decrementing by 1 and it increments when a car leaves since there's a added vacant spot available. What I'm trying to do is have the output look like this:

" Vacant Spots: 1

"

The only thing I want to change is the numerical value. I don't want a continuous stream of " Vacant Spots: 1 "s to show at the LCD display for the parking user to see. Is there a way to just clear the serial monitor after the loop ends without having it output a new value below it continuously? I've provided the code for the program. I have 3 xbees (1 coordinator and 2 routers). The two routers dont have code on them and are just sending data to the coordinator. The coordinator is where it receives the data. This is the code for the coordinator:

int readValue = 0;
void setup()
{
  Serial.begin(9600);
}
int vslots = 1;

void loop()
{

  if(Serial.available()>21)
  {
        if(Serial.read() == 0x7E)
        {
          for(int i=0;i<19;i++)
          {
          byte discard = Serial.read(); //Next 18 bytes, it's discarded
          }
          readValue = Serial.read();

            bool flagTrue = false;
            bool flagFalse = false;

         
            if((readValue == 0) && flagTrue == false ) //EXIT
              {    
                flagTrue = true;
                flagFalse = false;
                Serial.print("Vacant Spots: ");
                Serial.println(vslots);    
              }
              else if((readValue == 16 && flagFalse == false) && vslots >= 1)
//DECREMENT (CAR ENTERING)    
              {
                flagTrue = false;
                flagFalse = true;
                vslots -= 1;
                Serial.print("Vacant Spots: ");
                Serial.println(vslots);
              }

     
             if((readValue == 18) && flagTrue == false )
              {    
                flagTrue = true;
                flagFalse = false;
                Serial.print("Vacant Spots: ");
                Serial.println(vslots);    
              }
               else if((readValue == 19 && flagFalse == false) && vslots <= 10)
//INCREMENT (CAR EXITING)    
              {
                flagTrue = false;
                flagFalse = true;
                vslots += 1;
                Serial.print("Vacant Spots: ");
                Serial.println(vslots);
               }
           


         }  
   }
}

Upvotes: 0

Views: 1516

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409364

The ln suffix in println stands for new line. That means println will "print" the value and a newline.

So as an initial solution you could change from using println to plain print:

Serial.print("Vacant Spots: ");
Serial.print(vslots);  // Note: not println

Then we have to solve the problem of going back to the beginning of the line. to overwrite the old text. This is typically done with the carriate-return character '\r'. If we add it to the beginning of the printout:

Serial.print("\rVacant Spots: ");  // Note initial cariage-return
Serial.print(vslots);  // Note: not println

Then each printout should go back to the beginning of the line, and overwrite the previous content on that line.

To make sure that each line is equally long, you could use e.g. sprintf to format the string first, and then write it to the serial port:

char buffer[32];
// - for left justification,
// 5 for width of value (padded with spaces)
sprintf(buffer, "\rVacant Spots: %-5d", vslots);
Serial.print(buffer);

Upvotes: 0

Dharmik
Dharmik

Reputation: 44

First, you need to understand that Arduino serial terminal is not like a real terminal software It does not support a command sent on UART. So to achieve what you want you will require real terminal software such as putty. Which allows you to make changes by sending character bytes using UART communication.

//ADD these lines before printing on a serial terminal
  Serial.write(27);       // 27 is an ESC command for terminal
  Serial.print("[2J");    // this will clear screen
  Serial.write(27);       // 27 is an ESC command for terminal
  Serial.print("[H");     // move cursor to home 
//now print what you want...
  Serial.print("Vacant Spots: ");
  Serial.println(vslots);

For more info and methods you can check this post here. But had tried everything and suggesting to go with putty.

Upvotes: 1

Related Questions