Drinking Game – Source code & wiring diagram

Source code and wiring

    #include <LiquidCrystal.h>
    #include <Menu.h>
    #include <LCDMenu2.h>
    #include <avr/pgmspace.h>
    uint8_t * heapptr, * stackptr;
    #define _LCD_rows       4
    #define _LCD_cols       20
    #define _BUTTON_up      7
    #define _BUTTON_down    8
    #define _BUTTON_enter   6
    #define _BUTTON_prestat 0
    #define _BUTTON_press_time  250
    uint8_t arrow_up[8]    = {0x4,0xE,0x15,0x4,0x4,0x4,0x0};
    uint8_t arrow_down[8]  = {0x0,0x4,0x4,0x4,0x15,0xE,0x4};
    LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
    Menu top("Root");
    LCDMenu2 Root(top, lcd , _LCD_rows, _LCD_cols, 0, 1);
    int but[4]= {_BUTTON_up,_BUTTON_down,_BUTTON_enter};
    boolean pbut[4]={_BUTTON_prestat,_BUTTON_prestat,_BUTTON_prestat,_BUTTON_prestat};
    int button_press_enter = 0;
    unsigned long g_button_press_time = millis();
    byte tid = 60;
    byte folsomheit = 100;
    byte clicks = 0;
    int tid1,tid2;
    int folsomheit1,folsomheit2;
      prog_char string_0[] PROGMEM = "Set folsomheit fyri";// 0
      prog_char string_1[] PROGMEM = "       Accelerometer";// 1
      prog_char string_2[] PROGMEM = "Value: ";// 2
      prog_char string_3[] PROGMEM = "X:                  ";// 3
      prog_char string_4[] PROGMEM = "Y:                  ";// 4
      prog_char string_5[] PROGMEM = "Z:                  ";// 5
      prog_char string_6[] PROGMEM = "Accelerometer test  ";// 6
      prog_char string_7[] PROGMEM = "Set tid.";// 7
      prog_char string_8[] PROGMEM = "          MAX 255sek";// 8
      prog_char string_9[] PROGMEM = "DREKK    SMAKKAR";// 9
      prog_char string_10[] PROGMEM = "Drekk! ";// 10
      prog_char string_11[] PROGMEM = "Byrjar um:";//11
      prog_char string_12[] PROGMEM = "BOOM!";//12
      prog_char string_13[] PROGMEM = "10";//13
      prog_char string_14[] PROGMEM = "09";//14
      prog_char string_15[] PROGMEM = "8";//15
      prog_char string_16[] PROGMEM = "7";//16
      prog_char string_17[] PROGMEM = "6";//17
      prog_char string_18[] PROGMEM = "5";//18
      prog_char string_19[] PROGMEM = "4";//19
      prog_char string_20[] PROGMEM = "3";//20
      prog_char string_21[] PROGMEM = "2";//21
      prog_char string_22[] PROGMEM = "1";//22
      prog_char string_23[] PROGMEM = "0";//23
      prog_char string_24[] PROGMEM = "Klarur?";//24
    PROGMEM const char *Texts[] =
    {
      string_0,
      string_1,
      string_2,
      string_3,
      string_4,
      string_5,
      string_6,
      string_7,
      string_8,
      string_9,
      string_10,
      string_11,
      string_12,
      string_13,
      string_14,
      string_15,
      string_16,
      string_17,
      string_18,
      string_19,
      string_20,
      string_21,
      string_22,
      string_23,
      string_24
    };
    char buffer[20];

  void button(int which, char select='z')
  {
      if(which == _BUTTON_up || select == 'w') {
          //UP
          Root.goUp();
          Serial.println("'up'");
      }
      else if(which == _BUTTON_down || select == 's') {
         //DOWN
         Root.goDown();
         Serial.println("'down'");
      }
      else if(which == _BUTTON_enter || select == 'e') {
         //ENTER
         Root.goEnter();
         button_press_enter = 1;
         Serial.println("'enter'");
      }
      else if( select == 'q') {
         //BACK
         Root.goBack();
         Serial.println("'back'");
      }
  }
  void buttoncheck()
  {
      for (int i=0;i<=3;i++)
      {
          if (digitalRead(but[i])) {
              if (pbut[i]==0 && (millis()-g_button_press_time) >= _BUTTON_press_time) {
                  button(but[i]);
                  pbut[i]=1;
                  g_button_press_time = millis();
              }
          }
          else {
              pbut[i]=0;
          }
      }
      if(Serial.available()) {
          char c = Serial.read();
          button(-1,c);
      }
  }
  void funccheck()
  {
      if(button_press_enter == 1) {
          button_press_enter = 0;

          if(Root.curfuncname == "Aftur") {
              Root.goBack();
             // Serial.println("Function: Aftur");
          }
          else if(Root.curfuncname == "Acceleremeter test") {
              //Serial.println("Function: Acceleremeter test");
              accelerometer();
          }
          else if(Root.curfuncname == "Soundtest") {
              //Serial.println("Function: Soundtest");
              testsound();
          }
          else if(Root.curfuncname == "Folsomheit") {
              //Serial.println("Function: setFolsomheit");
              setFolsomheit();
          }
          else if(Root.curfuncname == "Tid") {
              //Serial.println("Function: setTid");
              setTid();
          }
          else if(Root.curfuncname == "Byrja PtB"){
              startptb();
          }
      }
  }

void setup()
{
  lcd.begin(_LCD_rows,_LCD_cols);
  lcd.createChar(0, arrow_up);
  lcd.createChar(1, arrow_down);
  menuint();
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.begin(9600);
  Serial.println("DEBUG:");
}

void loop()
{
  buttoncheck();
  funccheck();
}
  Menu aftur1("Aftur");
  Menu aftur2("Aftur");
  Menu aftur3("Aftur");
  Menu aftur4("Aftur");
  Menu spal("Byrja Spael");
  Menu vs("Vel Spael");
  Menu sp1("0l roulette");
  Menu sp2("Pass the bomb");
  Menu sp21("Byrja PtB");
  Menu sp22("Tid");
  Menu sp23("Folsomheit");
  Menu sp24("Navn");
  Menu uppseting("Uppseting");
  Menu up1("Acceleremeter test");
  Menu up2("Backlight toggle");
  Menu up3("Soundtest");
void menuint()
{
//  top.addChild(spal);
  top.addChild(vs);
  top.addChild(uppseting);
  vs.addChild(aftur1);
  //vs.addChild(sp1);
  vs.addChild(sp2);
  sp2.addChild(aftur2);
  sp2.addChild(sp21);
  sp2.addChild(sp22);
  sp2.addChild(sp23);
  sp2.addChild(sp24);
  uppseting.addChild(aftur3);
  uppseting.addChild(up1);
 // uppseting.addChild(up2);
  uppseting.addChild(up3);
  Root.display();
}

boolean breakloop = false;
int x;
byte y;
void accelerometer()
{
  breakloop = false;
  lcd.setCursor(0, 0);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[6])));
  lcd.print(buffer);
  delay(1000);
  while(!breakloop)
  {
    lcd.setCursor(0, 1);
    strcpy_P(buffer, (char*)pgm_read_word(&(Texts[3])));
    lcd.print(buffer);
    x = analogRead(0);
    lcd.setCursor(17, 1);
    lcd.print(x);
    x = map(x,0,1023,0,18);
    lcd.setCursor(2, 1);
    for(y=0;y<x;y++)
      lcd.print("#");
    lcd.setCursor(0, 2);
    strcpy_P(buffer, (char*)pgm_read_word(&(Texts[4])));
    lcd.print(buffer);
    x = analogRead(1);
    lcd.setCursor(17, 2);
    lcd.print(x);
    x = map(x,0,1023,0,18);
    lcd.setCursor(2, 2);
    for(y=0;y<x;y++)
      lcd.print("#");
    lcd.setCursor(0, 3);
    strcpy_P(buffer, (char*)pgm_read_word(&(Texts[5])));
    lcd.print(buffer);
    x = analogRead(2);
    lcd.setCursor(17, 3);
    lcd.print(x);
    x = map(x,0,1023,0,18);
    lcd.setCursor(2, 3);
    for(y=0;y<x;y++)
      lcd.print("#");
    delay(200);
    if(digitalRead(6)==HIGH)
    {
      breakloop = true;
      Root.display();
      delay(1000);
    }
  }
}
void testsound()
{
  lcd.setCursor(2, 2);
  digitalWrite(9, HIGH);
  lcd.print("BEEP");
  delay(250);
  digitalWrite(9, LOW);
  delay(250);
  digitalWrite(9, HIGH);
  lcd.setCursor(3, 7);
  lcd.print("BEEP");
  delay(250);
  digitalWrite(9, LOW);
  delay(250);
  digitalWrite(9, HIGH);
  lcd.setCursor(1, 15);
  lcd.print("BEEP");
  delay(250);
  digitalWrite(9, LOW);
  delay(250);
  Root.display();
}
void setFolsomheit()
{
  breakloop = false;
  lcd.clear();
  lcd.setCursor(0, 0);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[0])));
  lcd.print(buffer);
  lcd.setCursor(0, 1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[1])));
  lcd.print(buffer);
  delay(1000);
  while(!breakloop)
  {
    delay(50);
    lcd.setCursor(0, 2);
    strcpy_P(buffer, (char*)pgm_read_word(&(Texts[2])));
    lcd.print(buffer);
    lcd.print(folsomheit,DEC);
    lcd.print("   ");
    lcd.setCursor(0, 3);
    x = map(folsomheit,0,255,0,20);
    for(y=0;y<x;y++)
      lcd.print("#");
    for(y=x;y<20;y++)
      lcd.print(" ");
    if(digitalRead(7)==HIGH)
    {
      folsomheit++;
    }
    if(digitalRead(8)==HIGH)
    {
      folsomheit--;
    }
    if(digitalRead(6)==HIGH)
    {
      breakloop = true;
      Root.display();
      delay(1000);
    }
  }
}

void setTid()
{
  breakloop = false;
  lcd.clear();
  lcd.setCursor(0, 0);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[7])));
  lcd.print(buffer);
  lcd.setCursor(0, 1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[8])));
  lcd.print(buffer);
  delay(1000);
  while(!breakloop)
  {
    delay(50);
    lcd.setCursor(1, 2);
    lcd.print(tid,DEC);
    lcd.print(" sek   ");
    lcd.setCursor(0, 3);
    x = map(tid,0,255,0,20);
    for(y=0;y<x;y++)
      lcd.print("#");
    for(y=x;y<20;y++)
      lcd.print(" ");
    if(digitalRead(7)==HIGH)
    {
      tid++;
    }
    if(digitalRead(8)==HIGH)
    {
      tid--;
    }
    if(digitalRead(6)==HIGH)
    {
      breakloop = true;
      Root.display();
      delay(1000);
    }
  }
}
void startptb()
{
  breakloop = false;
  lcd.clear();
  lcd.setCursor(2,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[11])));
  lcd.print(buffer);
  lcd.setCursor(13,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[13])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(13,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[14])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[15])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[16])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[17])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[18])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[19])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[20])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[21])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[22])));
  lcd.print(buffer);
  delay(500);
  lcd.setCursor(14,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[23])));
  lcd.print(buffer);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Accelerometer:");
  lcd.setCursor(0, 2);
  lcd.print("Tid:");
  tid1 = millis()/1000;
//  clicks = 0;
  folsomheit1 = analogRead(0)+analogRead(1)+analogRead(3);
  delay(250);
  while(!breakloop)
  {
    folsomheit2 = analogRead(0)+analogRead(1)+analogRead(3);
    if(folsomheit2>folsomheit1)
      x = folsomheit2-folsomheit1;
    else
      x = folsomheit1-folsomheit2;
    if(x!=0)
      x = x/3;
    tid2 = millis()/1000;
    tid2 = tid2-tid1;
    if(x>folsomheit||tid2>tid)
    {
      breakloop = true;
      lcd.clear();
      lcd.setCursor(0,0);
      if(x>folsomheit)
        lcd.print("A");
      else
        lcd.print("T");
      btb_boom();

    }else{
      lcd.setCursor(0, 1);
      x = map(x,0,folsomheit,0,20);
      for(y=0;y<x;y++)
        lcd.print("#");
      for(y=x;y<20;y++)
        lcd.print(" ");
      lcd.setCursor(0, 3);
      x = map(tid2,0,tid,0,20);
      for(y=0;y<x;y++)
        lcd.print("#");
      for(y=x;y<20;y++)
        lcd.print(" ");
      folsomheit1 = analogRead(0)+analogRead(1)+analogRead(3);
      delay(100);
    }
  }
}
void btb_boom()
{
  lcd.setCursor(7,1);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[12])));
  lcd.print(buffer);
  digitalWrite(9, HIGH);
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, LOW);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, HIGH);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, LOW);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, HIGH);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, LOW);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, HIGH);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, LOW);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, HIGH);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, LOW);
    delay(1000);
  }
  if(digitalRead(6)==LOW)
  {
    digitalWrite(13, HIGH);
    delay(1000);
  }
  digitalWrite(9, LOW);
  lcd.setCursor(2,2);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[9])));
  lcd.print(buffer);
  randomSeed(analogRead(0));
  x = random(2,10);
  for(y=0;y<x;y++)
  {
    lcd.setCursor(8,2);
    lcd.print(x);
    delay(750);
  }
  lcd.setCursor(0,3);
  strcpy_P(buffer, (char*)pgm_read_word(&(Texts[10])));
  for(y=0;y<3;y++)
  {
    lcd.print(buffer);
    delay(1000);
  }
  lcd.setCursor(0,0);
  for(y=0;y<3;y++)
  {
    lcd.print(buffer);
    delay(1000);
  }
  delay(5000);
  Root.display();
}

I found LCDmenu2 here
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242443739
 or direct link http://nilsfeld.de/r/arduino/lib/LCDMenu2.rar

Wiring

0. TX
1. RX
2. LCD DATA
3. LCD DATA
4. LCD DATA
5. LCD DATA
6. Button 1
7. Button 2
8.  Button 3
9. Annoying pezio buzzer
10. LCD Enable
11. LCD rw
12. LCD RS
13. LCD Backlight

The accelerometer is connected to analog input

0. X-axis
1. Y-axis
2.  Z-axis

Advertisements

About havanissa

Being in Faroe Island(google it), I live a double life, during the day i'm a codemonkey but at nights I dress up as havanissa(garden gnome) and work on electronics and other funny stuff

Posted on July 24, 2011, in Uncategorized. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: