Send data by serial

Hello, i'm trying to send data from a Potentiometer (and button) and read the value or state of this one in the computer, but i don't know how i could actualize only the value of the state.

Is it possible to update only that value and not send every second or milliseconds the value that is being read by serial?

Some code would help...

And if it doesn't need to send it every second (or whatever), when DO you want to end it?

Is it possible to update only that value and not send every second or milliseconds the value that is being read by serial?

I'm going to guess that English is not your first languate.

You do not, on the Arduino, read anything by serial. You write to the serial port.

You can write every value that you read, OR you can keep track of the last value that you wrote, and only write a new value when the current value is not the same as the last value written.

Exactly how to do that depends on the code that you didn't post.

Hello and thank you for your fast reply.

Yes sorry i wanted say that i try to write to the serial port.

I want to write only the new value of the variable StateButton

int led = 9 ;
int button = 3;
int StateButton;


void setup()
{
 Serial.begin(9600);    
 pinMode(led,OUTPUT);
 pinMode(button,INPUT);
 Serial.print("State of the Button : ");
}


void loop()
{
 
 StateButton = digitalRead(button);

 if(StateButton == HIGH)
 {

   digitalWrite(led,HIGH);
   Serial.print(StateButton);
   delay(100);
     
 } 
 else{
   digitalWrite(led,LOW);
   Serial.print(StateButton);
   delay(100);         
 }  
}

Please go back and read How to use the forum. After that, edit your post to include code-tags. After that we're ready to help further :slight_smile:

acWill:
Hello, i'm trying to send data from a Potentiometer (and button) and read the value or state of this one in the computer, but i don't know how i could actualize only the value of the state.

Is it possible to update only that value and not send every second or milliseconds the value that is being read by serial?

Which software are you going to use on the computer for displaying data?

Is it the "serial monitor", contained in the Arduino IDE?

Or are you going to use a "terminal emulation software on your PC like maybe "RealTerm"?

With the serial monitor you just can send ASCII characters, newline and linefeed control characters, having §no fancy monitor decoration".

But you can do this:

  • watch out for the sensor readings and only "send data on change.

So if you take a sample reading from your push button, let's say one sample each 10 millisecond, you need NOT send the data in the same sample rate.
You simply could watch out for "button state change" and only send a new line of values after the button state has changed (i.e. from "button pressed" to "button "released", or the other way round.
That's perfectly possible.

And when using a terminal emulation software likle RealTerm and many others, your possibilities are much wider: You can, for example:

  • erase the screen contents
  • use different colors for foreground color (text) and backround)
    You can send sequences to set the cursor to certain row and column of the screen, and so on.

This would enable you to do something like that:

Have the potentiometer reading with yellow text on blue background in row 10 and column 15 of the screen.
And have the button state with red text on black background in row 5 and column 6 of the screen.

But this can't be done with the serial monitor, you would have to use a serial terminal emulation software on your PC instead of the serial monitor to do it.

So what do you want: Simple serial monitor as the display, sending ony data when the button state has changed. one line after another line?

Or do you want to have fancy colors on the PC serial screen, cursor position placement on screen as you like in rows and columns?

Here is a smal test code for demonstration of VT100 /ANSI control sequences with Arduino:

// Demonstration code for ANSI/VT100 terminal codes on Arduino
// Created by "jurs" for German Arduino forum
// Do NOT use the Arduino serial monitor with this sketch!
// You must use a terminal emulation software to show the serial output correctly.
// When using Windows you can use "PUTTY" configured to your Arduino COM Port.

// defines taken from list at http://www.fourwalledcubicle.com/files/LUFA/Doc/090605/html/dd/d6d/a00111.html
#define ANSI_ESCAPE_SEQUENCE(c) "\33[" c
#define ESC_RESET ANSI_ESCAPE_SEQUENCE("0m")
#define ESC_BOLD_ON ANSI_ESCAPE_SEQUENCE("1m")
#define ESC_ITALICS_ON ANSI_ESCAPE_SEQUENCE("3m")
#define ESC_UNDERLINE_ON ANSI_ESCAPE_SEQUENCE("4m")
#define ESC_INVERSE_ON ANSI_ESCAPE_SEQUENCE("7m")
#define ESC_STRIKETHROUGH_ON ANSI_ESCAPE_SEQUENCE("9m")
#define ESC_BOLD_OFF ANSI_ESCAPE_SEQUENCE("22m")
#define ESC_ITALICS_OFF ANSI_ESCAPE_SEQUENCE("23m")
#define ESC_UNDERLINE_OFF ANSI_ESCAPE_SEQUENCE("24m")
#define ESC_INVERSE_OFF ANSI_ESCAPE_SEQUENCE("27m")
#define ESC_STRIKETHROUGH_OFF ANSI_ESCAPE_SEQUENCE("29m")
#define ESC_FG_BLACK ANSI_ESCAPE_SEQUENCE("30m")
#define ESC_FG_RED ANSI_ESCAPE_SEQUENCE("31m")
#define ESC_FG_GREEN ANSI_ESCAPE_SEQUENCE("32m")
#define ESC_FG_YELLOW ANSI_ESCAPE_SEQUENCE("33m")
#define ESC_FG_BLUE ANSI_ESCAPE_SEQUENCE("34m")
#define ESC_FG_MAGENTA ANSI_ESCAPE_SEQUENCE("35m")
#define ESC_FG_CYAN ANSI_ESCAPE_SEQUENCE("36m")
#define ESC_FG_WHITE ANSI_ESCAPE_SEQUENCE("37m")
#define ESC_FG_DEFAULT ANSI_ESCAPE_SEQUENCE("39m")
#define ESC_BG_BLACK ANSI_ESCAPE_SEQUENCE("40m")
#define ESC_BG_RED ANSI_ESCAPE_SEQUENCE("41m")
#define ESC_BG_GREEN ANSI_ESCAPE_SEQUENCE("42m")
#define ESC_BG_YELLOW ANSI_ESCAPE_SEQUENCE("43m")
#define ESC_BG_BLUE ANSI_ESCAPE_SEQUENCE("44m")
#define ESC_BG_MAGENTA ANSI_ESCAPE_SEQUENCE("45m")
#define ESC_BG_CYAN ANSI_ESCAPE_SEQUENCE("46m")
#define ESC_BG_WHITE ANSI_ESCAPE_SEQUENCE("47m")
#define ESC_BG_DEFAULT ANSI_ESCAPE_SEQUENCE("49m")
#define ESC_CURSOR_POS(L,C) ANSI_ESCAPE_SEQUENCE(#L ";" #C "H")
#define ESC_CURSOR_UP(L) ANSI_ESCAPE_SEQUENCE(#L "A")
#define ESC_CURSOR_DOWN(L) ANSI_ESCAPE_SEQUENCE(#L "B")
#define ESC_CURSOR_FORWARD(C) ANSI_ESCAPE_SEQUENCE(#C "C")
#define ESC_CURSOR_BACKWARD(C) ANSI_ESCAPE_SEQUENCE(#C "D")
#define ESC_CURSOR_POS_SAVE ANSI_ESCAPE_SEQUENCE("s")
#define ESC_CURSOR_POS_RESTORE ANSI_ESCAPE_SEQUENCE("u")
#define ESC_ERASE_DISPLAY ANSI_ESCAPE_SEQUENCE("2J")
#define ESC_ERASE_LINE ANSI_ESCAPE_SEQUENCE("K")

#define ESC_CURSOR_OFF ANSI_ESCAPE_SEQUENCE("?25l")
#define ESC_CURSOR_ON ANSI_ESCAPE_SEQUENCE("?25h")

void s(char* str) 
{ // put string to Serial - short form
  Serial.print(str);
}

void sendcursor(int r, int c)
{ // set ANSI terminal cursor to specified row and column
  char str[12];
  sprintf(str,"\33[%d;%dH",r,c);
  s(str);
}

void settext(char* fg, char* bg, char* cup, char* str)
// send text to terminal with foreground color, background color, cursor position
{
  s(cup); 
  s(fg);
  s(bg);
  s(str);
}

void clearsquare(char* fg, char* bg, int r1, int c1, int r2, int c2)
{ // clears a square on the terminal window with colors set
  s(fg);
  s(bg);
  for (int i=0;i<r2-r1;i++) 
  {
    sendcursor(r1+i, c1);
    for (int i=0;i<c2-c1;i++) s(" ");
  }  
}

char time[]=__TIME__;
int stunden,minuten,sekunden;

void ShowScreen()
// Die Bildschirmmaske ausgeben (was gleich bleibt)
{
  char* color;
  s(ESC_BG_BLUE);
  s(ESC_FG_GREEN);
  s(ESC_ERASE_DISPLAY);
  s(ESC_CURSOR_OFF);
  for (int i=0;i<6;i++)
  {
    switch (i)
    {
      case 0: color=ESC_BG_RED;break;
      case 1: color=ESC_BG_YELLOW;break;
      case 2: color=ESC_BG_GREEN;break;
      case 3: color=ESC_BG_MAGENTA;break;
      case 4: color=ESC_BG_CYAN;break;
      case 5: color=ESC_BG_BLACK;break;
     } 
     clearsquare(ESC_FG_BLACK,color, 2+i,2+2*i, 15-i,32-2*i);
  }  
}

void showTime()
// Die veränderlichen Teile des Bildschirms ausgeben (Zeitanzeige)
{
  char textbuffer[10];
  sprintf(textbuffer,"%02d:%02d:%02d",stunden,minuten,sekunden);
  settext(ESC_FG_WHITE, ESC_BG_BLACK, ESC_CURSOR_POS(8, 13), textbuffer);
}


void tickOneSecond()
{
  sekunden++;
  if (sekunden>=60) {sekunden=0;minuten++;}
  if (minuten>=60) {minuten=0;stunden++;}
  if (stunden>=24) {stunden=0;}
}


void setup()
{
  Serial.begin(9600);
  ShowScreen();
  stunden=atoi(time);
  minuten=atoi(&time[3]);
  sekunden=atoi(&time[6]);
}

unsigned long alteSekunde;
void loop()
{
  if (alteSekunde==millis()/1000) return; // selbe Sekunde, loop verlassen
  alteSekunde=millis()/1000;
  tickOneSecond(); // Uhrzeit weiterticken lassen
  showTime();  // Uhrzeit anzeigen
}

(Code contains comments in German language) and is creating a 24h clock displayed on the screen.
If you like different ANSI/VT100 demonstration, I also would be able to pull out some kind of "visual text menu" from the German part of the forum, i posted acouple of years ago. Please let me know, if you like to see a different demonstration than the one above.

How to use:
Grab yourself a terminal emulation software (i.e. RealTerm or as you like) for ayour PC, set it to the serial port of your Arduino board, select "ANSI/VT100" as tne terminal emulation and give it a try, The sketch simply does some demonstration with different colors and cursor positioning (code does NOT work with the "serial monitor", but requires VT100/ANSI terminal emulation for display).

Im going to use on the computer the "serial monitor", contained in the Arduino IDE

It is like changing the status of the button, but is it possible to act or overwrite that data without having to send again? (My Board is a Arduino Uno)

acWill:
I want to write only the new value of the variable StateButton

That's called state change detection. For that, open the StateChangeDetection example in Digital :slight_smile:

acWill:
It is like changing the status of the button, but is it possible to act or overwrite that data without having to send again? (My Board is a Arduino Uno)

No, overwriting existing data is not possible with using the serial monitor.

It is easy with VT100/ANSI terminal emulation software.
But it cannot be done with the serial monitor software

The serial monitor is simply a text ticker: You always can add text.
You can start with a newline, then add text as you need, then start with another newline.
But in the serial monitor you never can reset the cursor to a different position and overwrite existing text.

If you need cursor placement, overwriting and colors, use terminal emulation software with VT100/ANSI terminal emulation instead of "serial monitor!

Terminal emulation software is available for all operating systems, so no matter whether you are using Windows, Linux or MAC OS on your PC, you can have software. In LINUX systems, one or more "terminal" software iprograms are always included in the default installation. Windows had also a terminal emulation included, "HyperTerminal" was part of every Windows installation from Windows 2.0 up to Windows XP, but since Windows Vista it is no longer included. You have to use third-party software like PUTTY or REALTERM to have VT100/ANSI terminal capabilities with Windows nowadays.