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.
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.