How to avoid loop and over writing the previous output?

Hello! Where having a problem in varying the output in serial monitor without closing it. We want to have single output coming from the Arduino, we tried to put the function on how to avoid the loop and to have a single output, but using this we can't vary the value in the monitor. How can we change the output without the serial monitor repeating it and display it once?

Here's the code

void loop(){

//Pulse the latch pin: //set it to 1 to collect parallel data digitalWrite(latchPin, 1);

//set it to 1 to collect parallel data, wait delayMicroseconds(20);

//set it to 0 to transmit data serially digitalWrite(latchPin, 0);

//while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first

switchVar1 = shiftIn(dataPin, clockPin);

//Print out the results. //leading 0's at the top of the byte //(7, 6, 5, etc) will be dropped before //the first pin that has a high input //reading

Serial.print(switchVar1, DEC);

//white space

XBee.print(switchVar1, DEC);

//delay so all these print satements can keep up.

delay(500);

while(1){/empty/}; }

Your code cannot compile, and I don't understand the question. If you don't want to repeat values in the serial monitor, then save the last value you printed in a static of global variable, and if the new value is the same, don't print it.

Please remember to use code tags when posting code.

Not clear about the question.

If you stop the loop, then nothing will work in Arduino. You might need to think your solution in a different way, freezing loop is not a good idea.

If you want to run any function only once, put that inside setup().

Think in terms of state machine, in loop check whether any state occurred and do some action.

For e.g. here x is just a variable and can be set from serial read or what ever the way.

void loop(){
  if(x==1) do_action1();
  if(x==2) do_action2();
}

Here is the entire code. The problem that I need to solve is on how I can display a single output and restart the program again so that the value in the serial monitor can vary without closing it. I tried to to use the function on how to stop the void loop and yes it display a single output but when I tried to send a another data to it, it didn’t change. It only change when I close the serial monitor the run again the program.

Thanks for the help. :slight_smile:

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>


SoftwareSerial XBee(2,3);


//define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;


//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72;  //01001000



void setup() {
  //start serial
  XBee.begin(9600);
  Serial.begin(9600);


  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, INPUT);

}
 
  
void loop(){
  
//Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin, 1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially
  digitalWrite(latchPin, 0);
  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin); 
  //Print out the results.
  //leading 0's at the top of the byte
  //(7, 6, 5, etc) will be dropped before
  //the first pin that has a high input
  //reading 
  Serial.print(switchVar1, DEC);
  //white space
  XBee.print(switchVar1, DEC);
  //delay so all these print satements can keep up.
  delay(500);
  
  exit(0);

}
 

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) {
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  //we will be holding the clock pin high 8 times (0,..,7) at the
  //end of each time through the for loop

  //at the begining of each loop when we set the clock low, it will
  //be doing the necessary low to high drop to cause the shift
  //register's DataPin to change state based on the value
  //of the next bit in its serial information flow.
  //The register transmits the information about the pins from pin 7 to pin 0
  //so that is why our function counts down
  for (i = 7; i >= 0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
      //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

You don't want to restart your program. As Grumpy_Mike says, keep a variable to remember the previous switchVar1 value and compare the old and the new one; only if the differ, write it to XBee and/or serial port.

Do a first read / send in setup() and remember the value; the rest can be in loop().

Alexes: The problem that I need to solve is on how I can display a single output and restart the program again so that the value in the serial monitor can vary without closing it.

I don't understand. This is my best guess.

You want a number to be displayed (say 34567) You want another number to be displayed later (maybe only a second later) (say 56789).

Why would there be any question of closing the Serial Monitor - you could display different numbers for 5 years without closing it.

...R

You need to explain what you are trying to achieve, not how you think you can achieve that goal.

This is the xyproblem I think.http://xyproblem.info/

Robin2: Why would there be any question of closing the Serial Monitor

Because that resets the Arduino ;)

sterretje:
Because that resets the Arduino :wink:

Well, does he want to do that? or does he NOT want to do that?

I assumed from his Original Post that he does NOT want a reset.

…R

He probably does not want to, but he wrote code that forced the use of reset.