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 2366 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
  • 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
  • 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
  • gecoz
    gecoz over 9 years ago in reply to roby1

    There are several LCD libraries you can use, depending on the LCD and the way it is wired. Assuming you will be using an LCD based on the HD44780 chipset, you can use the Arduino LiquidCrystal library (for example, have a look at the setup and examples in Arduino LCD Set Up and Programming Guide, which shows a 4-bit data wiring for the LCD). Using the library APIs, you probably won't need to use ASCII escape sequences at all, as you will define the geometry of your display (number of rows and columns), then you can use the home() API to clear the display and position the cursor at the top left corner of the display, and use setCursor()  and print() to position the cursor on the exact row and column on the display, and write what you need to.

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

    Try the below:

     

    Serial.write(27);          // ESC Comand
    Serial.write("[2J");      // CLEAR SCREEN

     

    or

     

    Serial.write(27);          // ESC Comand
    Serial.write("[2K");      // CLEAR LINE

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

    I had tryed taht too....but as i sow the arduino does not seems to recognize that funcion...i dont see nothing changed....thx anyway

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

    i had tryed that function already...it didint work on serial monitor or hyperterminal....byt thx anyawy

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

    kk....i understood...thx for help

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

    Hei..the function .." Serial.write('\f'), ..seems to dont work on arduino serial monitor....but it works on hyper terminal connected to arduino werry well...as i wanted to work.

    If i use an LCD ( i will come in 3 days) i will be able to see the right form of values as i see in hyper terminal??

     

    PS: thx for the \f sintax...its pretty helpfull

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

    i know what u mean..i can use  ..."Serial.print()" and this will feed the screen with "empty" values so i will be able to see the " clear screen"...but thats not an option in my case....but thank you

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

    IMHO there is another factor to be considered. By a usability point of view it has not too much sense to launche the Arduino IDE to run a (debug anf limited) terminal.

    This means that you can't use your project only in a computer where you had previoysly installed the Arduino development environment, that is very limiting.

     

    Enrico

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