element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products and Partners
    Products and Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Community Hub
Community Hub
Member's Forum Arduino Serial
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Community Hub to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 21 replies
  • Subscribers 632 subscribers
  • Views 2363 views
  • Users 0 members are here
  • arduino serial stuff
Related

Arduino Serial

roby1
roby1 over 9 years ago

Hi..i have a question, maybe a noob one for ppl who know. I have maked a counter each time when i press the button the counter increments with 1 and display on serial something like:

nr = 1, nr = 2, nr = 3 ...etc. What i wanna know and i have tryed over 4 days is how i diplay only once the value on a single line and each time when i count,  ""to somehow overwrite"" the existing value and displayng it like: ""nr = 1"" then on the same line 1 is replaced with 2..then with 3 and so on every time i count.  More simple, then same line remains unchanged and only the value get overrited (upgraded).

 

HERE IS THE CODE:

 

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

const int ledPin = 10;       // the pin that the LED is attached to

 

 

// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses

int buttonState = 0;         // current state of the button

int lastButtonState = 0;     // previous state of the button

 

 

void setup()

{

  pinMode(buttonPin, INPUT);

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

}

 

 

 

 

void loop()

{

  buttonState = digitalRead(buttonPin);

 

 

  if (buttonState != lastButtonState)

  {

    if (buttonState == HIGH)

    {

      buttonPushCounter++;

      Serial.println("on");

      Serial.print("number of button pushes: ");

      Serial.println(buttonPushCounter);

      Serial.write('\n');

      Serial.write('\r');

    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  lastButtonState = buttonState;

 

 

  if (buttonPushCounter % 4 == 0) {

    digitalWrite(ledPin, HIGH);

  } else {

    digitalWrite(ledPin, LOW);

  }

  Serial.flush();

  Serial.end();

  Serial.begin(9600);

  Serial.flush();

}

 

 

and i get something like this on serial:

 

on

number of button pushes = 1

on

number of button pushes = 2

on

number of button pushes = 3

on

number of button pushes = 4

on

number of button pushes = 5

 

etc.

 

and i want to get:

on

number of button pushes = 1 ----- and on this line 1 to be replaced with 2 at the next count, then with 3 and so on..

 

I have tryed Serial.print( '\r' )  for carryege return but didnt workt.

 

Can someone tell me where im wrong?? Or what i need to print so see what i want.

  • Sign in to reply
  • Cancel
  • RParkerE
    RParkerE over 9 years ago in reply to balearicdynamics

    Yes I knew that it could be resized, however I thought it required a manual action. If you were not to manually resize the window I thought it could work as a "smoke and mirrors" fix. But I do agree that a terminal supporting ANSI Escapes is what is needed.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago

    Putty is a perfectly good example of a terminal and can respond to commands to change colour, move cursor etc etc, just look at the mirad of Raspberry PI scrips that make use of this

    Here is an example of one from a friend of mine that installs a whole bunch of stuff and heavily leverages terminal emulation escape sequences to make a much friendlier screen

     

    https://bitbucket.org/snippets/scargill/ekon6o/the-script

    I am pretty sure that by default putty will emulate an  XTerm screen https://www.x.org/docs/xterm/ctlseqs.pdf

     

    it certainly follows the ansi colour commands so for example, just put putty in  local echo and type "ESC"[0;92m and continue typing, The "ESC" is the actual escape key on the keyboard, the text will now be green

     

    oir this simple mod to the ASCIITabletest.ino example will glean a colourful output

     

     

    .......

    void loop() {

      // prints value unaltered, i.e. the raw binary version of the

      // byte. The serial monitor interprets all bytes as

      // ASCII, so 33, the first number,  will show up as '!'

      Serial.print("\x1B[0;");

      Serial.print(thisByte);

      Serial.print("m");

      Serial.write(thisByte);

     

     

      Serial.print(", dec: ");

      // prints value as string as an ASCII-encoded decimal (base 10).

      // Decimal is the  default format for Serial.print() and Serial.println(),

      // so no modifier is needed:

      Serial.print(thisByte);

    .......

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • gecoz
    gecoz over 9 years ago in reply to RParkerE

    Clearing the entire terminal seems the easiest way to achieve what requested. I can be done something like:

     

    ...
    void loop()
    {
      buttonState = digitalRead(buttonPin);
    
    
      if (buttonState != lastButtonState)
      {
        if (buttonState == HIGH) 
        {
          buttonPushCounter++;
          Serial.write('\f'); //clear screen
          Serial.println("on");
          Serial.print("number of button pushes: ");
          Serial.print(buttonPushCounter);
        } 
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state, for next time through the loop
      lastButtonState = buttonState;
    
    
      if (buttonPushCounter % 4 == 0) {
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
      Serial.flush();
    }

     

    Fabio.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • balearicdynamics
    balearicdynamics over 9 years ago in reply to RParkerE

    Pter, I think that the Arduino Serial Monitor can't be managed in this way as this is - as a matter of fact - a java multi line text window that canbe resized as you want.

     

    What is needed is a terminal supporting ANSI Escapes, it is correct and in practice this means a terminal program (like the Linux term) that can be configured in VT100+ mode.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • RParkerE
    RParkerE over 9 years ago

    Another "workaround" would be to figure out how many lines long the Arduino Serial Monitor is when you are using it and feed it that many line feeds effectively clearing the screen.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • RParkerE
    RParkerE over 9 years ago in reply to gecoz

    Could you use the ANSI escape codes to do this? Such as \u001b[2K to clear the line or \u001b[2J to clear the entire terminal screen. I know this isn't sending "backspaces" but its the same effect.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • gecoz
    gecoz over 9 years ago in reply to dougw

    This is a possibility, although, if I understand right what you mean, there is the added complication that you need to count the number of digits you have just printed, in order to send the right number of backspaces, and avoid garbage digits left on the screen (i.e. if you print '9' you need only one backspace, but if you print "10" you need 2 backspaces, and so on).

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • dougw
    dougw over 9 years ago

    I don't know much about this setup, but it should be possible to send or flush the serial buffer without sending a linefeed. Then it is a simple matter of sending a backspace before you send a new count.??

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • gecoz
    gecoz over 9 years ago

    As RParkerE correctly pointed out, the Serial Monitor that comes with the Arduino IDE cannot handle the escape codes correctly. You need to use a terminal emulator. A common one is PuTTy, available on all platforms. One thing to take into account is that, as it stands, your code would still print on multiple lines, because of the println command, which automatically adds a LF. To stay on the same line, the print statements in your loop() function should be limited to the information that you want to print on the same line, something like:

     

    Serial.print("number of button pushes: ");
    Serial.print(buttonPushCounter);
    Serial.write('\r');

     

    Hope this helps.

    Fabio.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • RParkerE
    RParkerE over 9 years ago in reply to RParkerE

    You can try using Tera Term as your serial terminal if you are on Windows, or Minicom if on Linux, (or really any true serial terminal) which should support escape codes and allow you to overwrite that line

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube