How to print to Serial only once in the loop?

Is there a way to accomplish this? If so, how?

Is there a way to accomplish this?

Yes.

If so, how?

Only call Serial.print() once.

If you define the circumstances under which you do, and do not, want to call Serial.print(), we can help you see that it happens that way.

Basically, though, you need to count how many times you have already printed. Only actually call print if the count is 0.

PaulS:

Is there a way to accomplish this?

Yes.

If so, how?

Only call Serial.print() once.

If you define the circumstances under which you do, and do not, want to call Serial.print(), we can help you see that it happens that way.

Basically, though, you need to count how many times you have already printed. Only actually call print if the count is 0.

If you put it in the loop, it just keeps on printing...

Why put, into “loop”, something you want done once? That’s what “setup” is for.

[quote author=Nick Gammon link=topic=82603.msg620827#msg620827 date=1323571326] Why put, into "loop", something you want done once? That's what "setup" is for. [/quote]

But I want it to keep on updating, as the String value updates also.

Perhaps if you explained a bit more. You want it to keep updating, but only print once? Why?

Well not really keep updating, but, it takes input from serial, then prints it on a LCD. But it prints it to the LCD over, and over again.

The loop function is called repeatedly, hence its name. Anything in it will be done many times. However if you want to update the LCD only when something changes (and getting information out of you about what you are doing is not easy), the you could do something like this:

int old_something;

void loop ()
 {
  int something = get_my_data ();
  if (something != old_something)
    Serial.print (something);  // display if it changed
  old_something = something;
  }

[quote author=Nick Gammon link=topic=82603.msg620848#msg620848 date=1323574360] The loop function is called repeatedly, hence its name. Anything in it will be done many times. However if you want to update the LCD only when something changes (and getting information out of you about what you are doing is not easy), the you could do something like this:

int old_something;

void loop ()
 {
  int something = get_my_data ();
  if (something != old_something)
    Serial.print (something);  // display if it changed
  old_something = something;
  }

[/quote] Wow, thanks. Didn't know it was that simple.

Thanks a bunch!

That's good as long as you don't get the same thing through serial twice.

Another way is to keep a flag, one bit in a byte will do. That leaves 7 other bits for other flags.

  • if flag is 0, get the data, upon data complete set the flag (flag = 1)
  • else (flag is 1) print/process data, clear the flag (flag = 0)

This can work when getting the complete data may take many passes through loop.

could you explain the flag? or give example code?

if (printFlag == 0x00){
Serial.print("stuff");
printFlag = 0x01;
}

In arduino, If i Up arduino, it has to print “POSITION : UP” and Down arduino, it has to print “POSITION : DOWN” only once. but it is printing multiple times. How to solve this?

if (pO < -20) {

Serial.print(“POSITION : DOWN”);
Serial.println();

} else if (pO > 10 ) {

Serial.print(“POSITION : UP”);
Serial.println();

}

Set a flag (a single byte variable if it is easier for you).

byte last_position = 1; // 1 = "UP"

Then in the ifs:

if (read_position() == 1 && last_position == 0){
print "Position UP"

and vice_versa for the "down" if statement.

In short you want to print the line ONLY WHEN THE POSITION IS NOT THE SAME AS THE LAST POSITION.

I.e. This is called a "state change".

Hello Sir,
I tried this, still its not working.
Could you please help me modifying the code?

byte last_position == 1

if (pO > 10 && last_position == 1 ) {
Serial.println(“POSITION : UP”);
last_position == 0;

} else if (pO < -20 && last_position == 1 ) {

Serial.println(“POSITION : DOWN”);
last_position == 0;
}

byte last_position == 1

Not working != not compiling.

Hello Everyone,

I want to ask that if I want to serially print the updated value of "i" for "For Loop" then how am I going to serially print it.

Please help....

paavnideshmukh62:
Hello Everyone,

I want to ask that if I want to serially print the updated value of “i” for “For Loop” then how am I going to serially print it.

Please help…

Using the print method of the Serial object.

paavnideshmukh62: Hello Everyone,

I want to ask that if I want to serially print the updated value of "i" for "For Loop" then how am I going to serially print it.

Please help....

What you write tells me that you have heard of or seen some code that you don't understand at all. This is the point where you click the LEARNING topic in the green bar at the top of your screen and begin to learn. Seriously, there are tutorials and lessons available for this and the 100 other questions lined up behind it.

AWOL:
Using the print method of the Serial object.

Thank u for ur response. I would like to share my code with u.

#include <EEPROM.h>

int dx = 10;
int dxx = 12;
byte value;
int r;
float b;
float c=0;
int i;
int g;
//int dx=10;
const int STEP_PIN = 3;
const int Enable_PIN = 13;
const int DIR_PIN = 6;
long currentMicros = 0;
long previousMicros = 0;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
    Serial.println("Enter the Value for X");
     pinMode(STEP_PIN, OUTPUT);
   pinMode(Enable_PIN, OUTPUT);
   pinMode(DIR_PIN, OUTPUT);
   digitalWrite(STEP_PIN, LOW);
   digitalWrite(Enable_PIN, HIGH); 
   digitalWrite(DIR_PIN, HIGH);
   
  while (!Serial) {
    
  }
}

void loop() {

// int val =10;
if (Serial.available()){
 while (Serial.available() == 0);

float a = Serial.parseFloat(); 
  Serial.print(a);
  Serial.println();
a=a*5.26;  // into mm conversion
  
  if (a>=b){
    c=a-b;
    Serial.println("Give the Speed ");
  while (Serial.available() == 0);

int s = Serial.parseInt(); 
Serial.println(s);
  s = map(s, 0, 100, 3000, 150);
for (int i=0; i <c; i++)
{
// I want to read the value of i once in serial monitor
 // What I get is the value of "i" but not once. I want to the updated value of "i" only once
 Serial.print(i);
   digitalWrite(Enable_PIN, LOW);
         digitalWrite(DIR_PIN, LOW);
 digitalWrite(STEP_PIN,HIGH);
          delayMicroseconds(s);
          digitalWrite(STEP_PIN,LOW);
       
  }

if (a<=b)
{
c=a-b;
c=-(c);
Serial.println("Give the Speed ");
  while (Serial.available() == 0);

int s = Serial.parseInt(); 
Serial.println(s);
 s = map(s, 0, 100, 3000, 150);

 
  for (int i=0; i <c; i++)
{
 
 Serial.print(i);
  digitalWrite(Enable_PIN, LOW);
         digitalWrite(DIR_PIN, HIGH);
 digitalWrite(STEP_PIN,HIGH);
          delayMicroseconds(s);
          digitalWrite(STEP_PIN,LOW); 



 }
} 
  }
  Serial.print(c);
  Serial.println();
  b=a;
  Serial.println("Enter the Value for X");
}
}

What I want to read is the updated value of “i” from “For Loop” only once in serial monitor.
Please check the value of i as the series as “0123456…”. I tried EEPROM update, read , write but it is not working.
//Enter the Value for X
10.00
Give the Speed
2
01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515252.60
//