// https://forum.arduino.cc/t/help-for-community-driven-masters-project/995395
const int buttonPin4 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3;
const int buttonPin2 = 4;
const int buttonPin1 = 5;
const int ledPin = 8;
// Variables will change:
int buttonState4; // the current reading from the input pin
int lastButtonState4 = HIGH; // the previous reading from the input pin
int buttonState3;
int lastButtonState3 = HIGH;
int buttonState2;
int lastButtonState2 = HIGH;
int buttonState1;
int lastButtonState1 = HIGH;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long lastDebounceTime4 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime1 = 0;
int x = 0;
long attacker;
long opponent;
void setup() {
Serial.begin(9600);
Serial.println("Hello Community Driven Thing!\n");
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lastButtonState4 = !digitalRead(buttonPin4); // the previous reading from the input pin
lastButtonState3 = !digitalRead(buttonPin3);
lastButtonState2 = !digitalRead(buttonPin2);
lastButtonState1 = !digitalRead(buttonPin1);
}
// roll ndice number of dice
unsigned char roll(unsigned char ndice)
{
unsigned total = 0;
for (unsigned char tt = 0; tt < ndice; tt++)
total += random(6) + 1;
return total;
}
void loop() {
// read the state of the switch into a local variable:
int reading4 = !digitalRead(buttonPin4);
int reading3 = !digitalRead(buttonPin3);
int reading2 = !digitalRead(buttonPin2);
int reading1 = !digitalRead(buttonPin1);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading4 != lastButtonState4) {
// reset the debouncing timer
lastDebounceTime4 = millis();
}
if (reading3 != lastButtonState3) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime4) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading4 != buttonState4) {
buttonState4 = reading4;
// only toggle the LED if the new button state is HIGH
if (buttonState4 == HIGH) {
x++;
// moved up to before x is used / needed
if (x >= 2) {
x = 0;
}
// Serial.print(x);
if (x) {
digitalWrite(ledPin, HIGH);
// Serial.println(" so write HIGH");
}
else {
digitalWrite(ledPin, LOW);
// Serial.println(" so write LOW");
}
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState4 = reading4;
if (x == 0) {
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading3 != buttonState3) {
buttonState3 = reading3;
if (buttonState3 == HIGH) {
attacker = roll(1);
Serial.print("attacker: ");
Serial.print(attacker);
Serial.print(" Vs. ");
}
}
}
lastButtonState3 = reading3;
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != buttonState2) {
buttonState2 = reading2;
if (buttonState2 == HIGH) {
attacker = roll(2);
Serial.print("attacker: ");
Serial.print(attacker);
Serial.print(" Vs. ");
}
}
}
lastButtonState2 = reading2;
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
attacker = roll(3);
Serial.print("attacker: ");
Serial.print(attacker);
Serial.print(" Vs. ");
}
}
}
lastButtonState1 = reading1;
}
if (x == 1) {
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading3 != buttonState3) {
buttonState3 = reading3;
if (buttonState3 == HIGH) {
opponent = roll(1);
Serial.print("opponent: ");
Serial.print(opponent);
Serial.println();
}
}
}
lastButtonState3 = reading3;
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != buttonState2) {
buttonState2 = reading2;
if (buttonState2 == HIGH) {
opponent = roll(2);
Serial.print("opponent: ");
Serial.print(opponent);
Serial.println();
}
}
}
lastButtonState2 = reading2;
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
opponent = roll(3);
Serial.print("opponent: ");
Serial.print(opponent);
Serial.println();
}
}
}
lastButtonState1 = reading1;
}
}