Help!!! output action based of number of inputs signals!

Trying to decrement by 10 when the button is pressed 4 times. I modified the button press example to keep a player health that deducts 10 from 100 every time the button is pressed and it works fine.

// this constant won’t change:
const int buttonPin = 2;
const int ledPin = 13;

// Variables will change:
int buttonPushCounter = 100;
int playerHealth = 0;
int lastPlayerHealth = 0;

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.print(“Player Health: 100”);
Serial.println(" \t");
}

void loop() {
// read the pushbutton input pin:
playerHealth = digitalRead(buttonPin);

if (playerHealth != lastPlayerHealth) {
// if the state has changed, increment the counter
if (playerHealth == HIGH) {

// if the current state is HIGH then the button

buttonPushCounter-=10; // <<<change this variable
Serial.print(“Player Health: “);
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println(” HIT!”);
}
// Delay a little bit to avoid bouncing
delay(50);
}

lastPlayerHealth = playerHealth;

if (buttonPushCounter == 0) {
digitalWrite(ledPin, HIGH);
Serial.println(“Player Dead”);
while(1);
}
}

This is the fourth modification and all I get in return is blank screens and/or looping “player dead” comment when button press is LOW and and player health integer alternating from 100 to 90 instead of 100 to 0. Please help.

// this constant won’t change:
const int buttonPin = 2;
const int ledPin = 13;
// Variables will change:
int buttonPushCounter;
int buttonState;
int hitMax;

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.print(“Player Health: 100”);
Serial.println(" \t");

}

void loop() {
int playerHealth = 100;
int lastPlayerHealth;
// if the state has changed, increment the counter
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (playerHealth != lastPlayerHealth) {

// if the current state is HIGH then the button
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH);
buttonPushCounter++;

if (buttonPushCounter % 4 >= 0 ) {

Serial.println(“HIT!”);
Serial.print("Player Health: ");
Serial.println(playerHealth);
}
if (buttonPushCounter % 4 == 0) {
playerHealth -=10;
Serial.println(“HIT!”);
Serial.print("Player Health: ");
Serial.println(playerHealth);
return;
}
lastPlayerHealth = playerHealth;

}

delay(50);
}

if (playerHealth == lastPlayerHealth) {
Serial.println( “Player Dead”);

}
}

Thanks for attempting to follow the rules when posting code. However, you have used the wrong tags.

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Weedpharma

Thanks for the info, I’ll refer to the user guide before posting again.