output serial monitor, goes on and on and on

Hi,

I'm very new to this forum, and after reading lots of different tutorials and try to find the solution to my problem myself I'd hope somebody
can help me.

Hi have a push button and a led light, but I want to serial monitor to do the following. Only report On/Off one time per change.
So lets say the led is off, than there should be in the serial monitor only 1 time Off, until the next change is taking place.
Zo it always you be somiting like
Off
On
Off
On
Off
On

I tried delay and other possibilities but with no success :frowning:

The next project I want to make is also a pushbutton led, but also an extra led that will show how many times the button has been pressed, so 1 time blink once en than delay 5 sec. and if it was pressed than blink 2 times and delay 5 sec. but hope I can magage to do that myself :slight_smile:

It sounds like you need to keep a record of the last state.
Then, each time through "loop()" see if the state you read from the button has changed.
If it has, print the new state, and store the new state.

(uncompiled, untested)

byte lastState;
...
...
void printState (byte state)
{
  Serial.println(state ? "HIGH" : "LOW");
}
..
..

void setup ()
{
  ...
  lastState = digitalRead (buttonPin);
  printState (lastState);
}


void loop ()
{
   byte newState = digitalRead (buttonPin);
   if (newState != lastState) {
      printState (newState);
      lastState = newState;
   }
}

[UNTESTED CODE]

#include <Button.h>

Button button = Button(12,PULLUP);
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  button.isPressed(); //update internal state of the button library
  if (button.stateChanged()){
    if (button.isPressed()){ Serial.println("on"); }
    else { Serial.println("off"); }
  }
}

thank you for your reply.

I'll look into that this evening.

your the best :slight_smile: