The Arduino drinking game

The Arduino drinking game

Today, I used the analog input lab to build an Arduino drinking game 🙂 .
Players need to press the pushbutton. If the serial input value is a multiple of 7, the player who pressed the pushbutton last must drink🍹.
Geeky, but I love it.

The Arduino drinking game
If the serial input value is a multiple of 7, the player who pressed the pushbutton last must drink.

This is how it looks like (sorry about the weird ratio) –

And this is the code I used –

void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
}

int play = 1;
boolean drink = false;

void loop() {
if (digitalRead(2) == HIGH) {
play = play + 1;
}

if ((play % 7) == 0) { // detect if the number is a multiple of 7
digitalWrite(3, HIGH); // turn LEDs ON
digitalWrite(4, HIGH);
drink = true; // change 'drink' state
}
else {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
drink = false;
}

if (drink == true) {
Serial.print("play: ");
Serial.print(play);
Serial.print("\t");
Serial.println("DRINK!!! :P ");
} else {
Serial.print("play: ");
Serial.println(play);
}
}

Improvement

I modified the code a little in order to avoid the blinking LEDs phenomenon. Now, I’m checking if the serial value is a multiple of 7 only after a digitalRead(2) == LOW event –

void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
}

int play = 1;
boolean drink = false;

void loop() {
  if (digitalRead(2) == HIGH) {
    play = play + 1;
  }

  // run number check-up on button release 
  if (digitalRead(2) == LOW) {
    // detect if the number is a multiple of 7
    if ((play % 7) == 0) {
      digitalWrite(3, HIGH);      // turn LEDs ON
      digitalWrite(4, HIGH);
      drink = true;               // change 'drink' state
    }
    else {
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      drink = false;
    }
  }

  if (drink == true) {
    Serial.print("play: ");
    Serial.print(play);
    Serial.print("\t");
    Serial.println("DRINK!!! :P ");
  } else {
    Serial.print("play: ");
    Serial.println(play);
  }
}

 

The result is that the LEDs are turned on only in case where the serial value is a multiple of 7, and the pushbutton is not pressed –

Published by

Dror Ayalon

@drorayalon

Leave a Reply

Your email address will not be published. Required fields are marked *