How to stop updating the terminal if no state change?

Everything from void loop() to delay(20); works perfectly. I copied that from a HC-05 setup demo on the www.

From there to the end also works but… because stateButton1 is checked each time through the loop, its status is updated and written to the terminal.

What I want is to update the terminal only if stateButton1 changed from the last time it was checked.

Suggestions?

void loop() {

   if (Genotronex.available()){
      BluetoothData=Genotronex.read();
   if(BluetoothData=='1'){   
      digitalWrite(ledpin,1);
      Genotronex.println("____________ LED  On D13 On");
   }
   if (BluetoothData=='0'){
      digitalWrite(ledpin,0);
      Genotronex.println("____________ LED  On D13 Off");
   }
}
delay(20);
  
stateButton1 = digitalRead(buttonPin1);
 {if (stateButton1 == HIGH)
      digitalWrite(LED1, LOW);
 {if (stateButton1 == HIGH)
    Genotronex.println("LED On D7 Off");
    

    
{if (stateButton1 == LOW) 
      digitalWrite(LED1, HIGH);}
{if (stateButton1 == LOW)
    Genotronex.println("LED On D7 On");

You need another variable named something like previousState which you compare to the current value and only do something if there is a difference.

saximus:
You need another variable named something like previousState which you compare to the current value and only do something if there is a difference.

It laughs at me!

Everything working up to line 44. buttonstateNew = stateButton1;{

Now I have the two messages alternating on the terminal… forever. I’ve tried every combination I can think of, the one below is just the latest. (I don’t know what to use in place of //do nothing either.)

I’m also willing to ditch everything and start over if I’m on the wrong path to my goal.

Any suggestions appreciated.

#include <SoftwareSerial.h> //import the serial library
SoftwareSerial Genotronex(10, 11); //Rx , Tx
const int  buttonPin1 = 2;
int stateButton1;
const int  LED1 = 7;
int buttonstate; 
int buttonstateOld;
int buttonstateNew;
int ledpin=13;
int BluetoothData;

void setup () {
  Genotronex.begin(9600);
  Genotronex.println("____________ Bluetooth Connected - Press 1 or 0 to toggle LED");
  pinMode(buttonPin1, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(ledpin, OUTPUT);
}

 void loop() {
 void start();
   if (Genotronex.available()){
      BluetoothData=Genotronex.read();
   if(BluetoothData=='1'){   
      digitalWrite(ledpin,1);
      Genotronex.println("____________ LED  On D13 On");
}
   if (BluetoothData=='0'){
      digitalWrite(ledpin,0);
      Genotronex.println("____________ LED  On D13 Off");
}
}

delay(20);
  
stateButton1 = digitalRead(buttonPin1);{

  if (stateButton1 == HIGH)
      digitalWrite(LED1, LOW);
  
  if (stateButton1 == LOW) 
     digitalWrite(LED1, HIGH);
}
     
buttonstateNew = stateButton1;{
    
  if ((buttonstateNew = HIGH) && (buttonstateOld = LOW));
    Genotronex.println("LED On D7 Off");

  if ((buttonstateNew = LOW) && (buttonstateOld = HIGH));
    Genotronex.println("LED On D7 On");
 
  if ((buttonstateNew = HIGH) && (buttonstateOld = HIGH));
//don't do anything
 
  if ((buttonstateNew = LOW) && (buttonstateOld = LOW));
//don't do anything

}
buttonstateOld = buttonstateNew;
 
}

warnmar10:
(I don’t know what to use in place of //do nothing either.)

You write nothing. The last two if statements should be deleted altogether. You also shouldn’t have semicolons after your if statements. You also have unnecessary curly braces. Pressing Ctrl+T in the IDE makes mistakes like that much easier to see.

See if this does what you’re trying to do:

#include <SoftwareSerial.h> //import the serial library
SoftwareSerial Genotronex(10, 11); //Rx , Tx
const int  buttonPin1 = 2;
int stateButton1;
const int  LED1 = 7;
int buttonstate;
int buttonstateOld;
int buttonstateNew;
int ledpin = 13;
int BluetoothData;

void setup () {
  Genotronex.begin(9600);
  Genotronex.println("____________ Bluetooth Connected - Press 1 or 0 to toggle LED");
  pinMode(buttonPin1, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(ledpin, OUTPUT);
}

void loop() {
  void start();
  if (Genotronex.available()) {
    BluetoothData = Genotronex.read();
    if (BluetoothData == '1') {
      digitalWrite(ledpin, 1);
      Genotronex.println("____________ LED  On D13 On");
    }
    if (BluetoothData == '0') {
      digitalWrite(ledpin, 0);
      Genotronex.println("____________ LED  On D13 Off");
    }
  }

  delay(20);

  stateButton1 = digitalRead(buttonPin1);

  if (stateButton1 == HIGH)
    digitalWrite(LED1, LOW);

  if (stateButton1 == LOW)
    digitalWrite(LED1, HIGH);

  if (stateButton1 != buttonstateOld)
  {
    if (stateButton1 = HIGH)
      Genotronex.println("LED On D7 Off");

    if (stateButton1 = LOW)
      Genotronex.println("LED On D7 On");

    buttonstateOld = stateButton1;
  }
}

saximus:
You write nothing. The last two if statements should be deleted altogether. You also shouldn't have semicolons after your if statements. You also have unnecessary curly braces. Pressing Ctrl+T in the IDE makes mistakes like that much easier to see.

See if this does what you're trying to do:

Thank you for the assist. Your code does nothing if stateButton1 is low. If I break the beam making stateButton1 high the LED off message scrolls endlessly and stops only when stateButton1 goes low again. It also doesn't print the LED on message when it goes low.

When I started this I had either the on message or off message scrolling depending on the state of stateButton1. It is making me crazy.

Perhaps this is what you are looking for? You may still get more than one line of output depending on what kind of switch you are connecting- a standard mechanical switch will bounce (go on and off) a few times whenever it is pressed.

(Obviously you need to declare prevstateButton1 somewhere up the top of your program)

 stateButton1 = digitalRead(buttonPin1);
 if (prevstateButton1 != stateButton1) {
  if (stateButton1 == HIGH) {
    digitalWrite(LED1, LOW);
    Genotronex.println("LED On D7 Off");
  } else { 
    digitalWrite(LED1, HIGH); 
    Genotronex.println("LED On D7 On");
  };
  prevstateButton1 = stateButton1;
 };

rw950431:
Perhaps this is what you are looking for? You may still get more than one line of output depending on what kind of switch you are connecting- a standard mechanical switch will bounce (go on and off) a few times whenever it is pressed.

(Obviously you need to declare prevstateButton1 somewhere up the top of your program)

Thanks I'll try that in the morning. The switch is actually infrared emitter and transistor pair. I can debounce if needed. The problem is sending the state change to the terminal only once. It has been very frustrating and I am out of my element.

rw950431:

 stateButton1 = digitalRead(buttonPin1);

if (prevstateButton1 != stateButton1) {
 if (stateButton1 == HIGH) {
   digitalWrite(LED1, LOW);
   Genotronex.println("LED On D7 Off");
 } else {
   digitalWrite(LED1, HIGH);
   Genotronex.println("LED On D7 On");
 };
 prevstateButton1 = stateButton1;
};

That's it exactly! Thank you, thank you, thank you.

:slight_smile: