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 2346 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
Parents
  • 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 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 in reply to gecoz

    You could always print the same number of digits,  starting with 0001 then it is always 4 backspaces...

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

    You could always print the same number of digits,  starting with 0001 then it is always 4 backspaces...

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

    Indeed, but I'm going to play the devil's advocate here, and ask: what if the counter reaches 9999? image On a more serious note, it is amazing how, from a simple starting problem, a miriad of great suggestions and ideas follow, showing the many different ways the same problem can be solved. I suppose, this is one of the many things that make the element14 community so good.

    • Cancel
    • Vote Up +1 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