single print of serial message

Hello,

I'm trying to print a single message when a LED is on, but it is not working.

I'm using the button state change tutorial from arduino.cc.

What i'm trying to do is:

  • When the button is pushed 4 times the led get's on, and a single serial message is being print with "LED ON".

now I only get continues messages on the serial monitor when pressed the fourth time.

Can anyone help me to get a single message printed?

The code i'm using:

const int buttonPin = 2;  
const int ledPin = 13;   


int buttonPushCounter = 0;  
int buttonState = 0;     
int lastButtonState = 0;  

void setup(){
  pinMode(buttonPin, INPUT);  
  pinMode(ledPin, OUTPUT);    
  Serial.begin(9600);        
}

void loop(){
  buttonState = digitalRead(buttonPin);
  
   if (buttonState != lastButtonState) {

     if (buttonState == HIGH) {
     
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:   ");
       Serial.println(buttonPushCounter, DEC);
     }
     else {

       Serial.println("off");
     }
   }

   lastButtonState = buttonState;

   if (buttonPushCounter % 4 == 0) {
     digitalWrite(ledPin, HIGH);
   } else {
     digitalWrite(ledPin, LOW);
   }

   if (buttonPushCounter % 4 == 0 && buttonState == HIGH) {
       Serial.println("LED ON");
   }
}

Greetings

Dared

You are not using the internal pull-up resistor for the switch pin. This implies that you have an external pull-down resistor wired with the switch. Do you?

Do you want to send the message, and turn the LED on, every 4th time the button is pressed? I'll presume that this is what you mean.

If you want to send the message just once, and turn the LED on just once, when the 4th press occurs, you need to keep track of the state of the LED. If the button press count is 4, and the LED is not on, turn it on and send the message.

The key is the underlined part. Instead of turning the light on EVERY time the button press count is 4, turn it on (and send the message) only when the count is 4 && the led is not on.

Hey

Yes, i'm using an axternal resistor like the tutorial on arduino.cc of 10K on the pushbutton.

Do you want to send the message, and turn the LED on, every 4th time the button is pressed? I'll presume that this is what you mean.

yes, thats exactly what I mean.

So how do I make the code for && led = OFF ?? Can u help me with that, because i'm just a beginner, got an arduino uno for like 2 days.

now I have:

   if (buttonPushCounter % 4 == 0 && ledPin = LOW) {
     digitalWrite(ledPin, HIGH);
     Serial.println("LED on")
   } else {
     digitalWrite(ledPin, LOW);
   }

but that gives an error.

Add a global variable, ledState with an initial value of LOW.

if (buttonPushCounter % 4 == 0 && ledState == LOW)
  {
     ledState = HIGH;
     digitalWrite(ledPin, ledState);
     Serial.println("LED on")
   }
   else
   {
     ledState = LOW;
     digitalWrite(ledPin, ledState);
   }

Allright, thanks!

One more question:

on the serial monitor of the arduino program when pushed the fourth time, it goes like:

led on
led on
led on
led on
etc etc

and keeps making a new lines

Is it possible that it only print led on once?

Hopefully, this will work better.

if (buttonPushCounter % 4 == 0)
{
   if(ledState == LOW)
   {
     ledState = HIGH;
     digitalWrite(ledPin, ledState);
     Serial.println("LED on")
   }
   else
   {
     digitalWrite(ledPin, LOW);
   }
}
else
{
   ledState = LOW;
}

not really :slight_smile: now the led stays on all pushes, and still keeps printing LED ON 1000 times.

is there not a code for print once?

is there not a code for print once?

No. You need to define what "print once" means.

if (buttonPushCounter % 4 == 0)
{
   if(ledState == LOW)
   {
     ledState = HIGH;
     digitalWrite(ledPin, ledState);
     Serial.println("LED on")
   }
   else
   {
     digitalWrite(ledPin, LOW);
   }
}
else
{
   ledState = LOW;
   digitalWrite(ledPin, LOW); // This will make the LED go off
                // when count is not a multiple of 4
}

If this still causes the message to be repeated, post all of your code.

You could use a boolean to make it only print when the boolean is false/true.

boolean hasPrinted = false;

if (buttonPushCounter % 4 == 0)
{
   if(ledState == LOW)
   {
     ledState = HIGH;
     digitalWrite(ledPin, ledState);
     if(!hasPrinted)
     {
     Serial.println("LED on")
     hasPrinted = true;
     }
   }
   else
   {
     digitalWrite(ledPin, LOW);
   }
}
else
{
   ledState = LOW;
   digitalWrite(ledPin, LOW); // This will make the LED go off
                // when count is not a multiple of 4
}

That will make it only print once, whenever that whole if statement is called.

You'll need to determine when to reset the hasPrinted value.