Daniel

Too many drinks can turn a fun night out into disaster. Stay safe with Daniel, a personalized counter that knows your limits.

BeginnerFull instructions provided1 hour818
Daniel

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Grove Starter Kit for LaunchPad
Seeed Grove Starter Kit for LaunchPad
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
Male/Male Jumper Wires
×9

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Breadboard Schematics

Code

button_press.ino

Arduino
Build the device as instructed by the schematics, then upload this program to the device and you are good to go!
// constants won't change. They're used here to
// set pin numbers:
const int tiltSwitch = 11;     // the number of the tiltSwitch pin


// variables will change:
int button2State = 0; // variable for reading the BB button status
int count = 0; // variable for count the number of times the button has been pressed
int limit = 0; // variable to keep track of the limit
int flash = 0;  // helps with flashing the lights
int switchState = 0; // variable to kep track the tilt switch state

// Define the LED digit patterns for 0 - 9 in a 2 dimensional array.
// The 2D array (an array of arrays or a matrix) has 10 arrays that each
// contain 7 values.
// Note that these patterns are for common cathode displays. For common
// anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:
// Common Anode version
byte seven_segment_digits[10][7] = { { 0,0,0,0,0,0,1 }, // display '0'
                                     { 1,0,0,1,1,1,1 }, // display '1'
                                     { 0,0,1,0,0,1,0 }, // display '2'
                                     { 0,0,0,0,1,1,0  }, // display '3'
                                     { 1,0,0,1,1,0,0 }, // display '4'
                                     { 0,1,0,0,1,0,0 }, // display '5'
                                     { 0,1,0,0,0,0,0 }, // display '6'
                                     { 0,0,0,1,1,1,1 }, // display '7'
                                     { 0,0,0,0,0,0,0 }, // display '8'
                                     { 0,0,0,1,1,0,0 }  // display '9'
                                   };

// Common Cathode version
/* byte seven_segment_digits[10][7] = { { 1,1,1,1,1,1,0 }, // display '0'
                                     { 0,1,1,0,0,0,0 }, // display '1'
                                     { 1,1,0,1,1,0,1 }, // display '2'
                                     { 1,1,1,1,0,0,1 }, // display '3'
                                     { 0,1,1,0,0,1,1 }, // display '4'
                                     { 1,0,1,1,0,1,1 }, // display '5'
                                     { 1,0,1,1,1,1,1 }, // display '6'
                                     { 1,1,1,0,0,0,0 }, // display '7'
                                     { 1,1,1,1,1,1,1 }, // display '8'
                                     { 1,1,1,0,0,1,1 }  // display '9'
                                   };
*/

/* Connect the pins of the display accordingly.
Only one of the VCC (Common Anode) / GND (Common Cathode) pins need to be
connected to work, but it's ok to connect both if you want.
///////////////TOP//////////////
   pin 1 : G (9)  pin 10: A (3)
   pin 2 : F (8)  pin 9 : B (4)
   pin 3 : VCC and Tilt Switch   pin 8 : VCC
   pin 4 : E (7)  pin 7 : C (5)
   pin 5 : D (6)  pin 6 : Dot (10)
/////////////BOTTOM/////////////
*/

/* In the setup function, we set our LED pins as OUTPUT.
 */
void setup() {
  // initialize the onboard pushbutton
  pinMode(PUSH2, INPUT_PULLUP);

  // initialize the onboard LEDs
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);

  // initialize the seven segment display
  pinMode(3, OUTPUT); // set segment A as output
  pinMode(4, OUTPUT); // set segment B as output
  pinMode(5, OUTPUT); // set segment C as output
  pinMode(6, OUTPUT); // set segment D as output
  pinMode(7, OUTPUT); // set segment E as output
  pinMode(8, OUTPUT); // set segment F as output
  pinMode(9, OUTPUT); // set segment G as output
  pinMode(10, OUTPUT); // set dot as output

  // initialize the tilt switch
  pinMode(tiltSwitch, INPUT);
  
  for(int i = 3; i < 10; i++) { // start with segments off
    digitalWrite(i, HIGH);
  }
  digitalWrite(10, HIGH);  // start with the dot off
  Serial.begin(9600);
}

/* In the loop section we will begin displaying the different numbers.
 * Add delay() or sleep() to give some time between the numbers changing.
 */
void loop() {
  int pin = 3;
  delay(150); // this is the same as delay() but saves power

  // read the states of the push button and tilt switch:
  button2State = digitalRead(PUSH2);
  switchState = digitalRead(11);

  // if Daniel is not tilted up then enter selection mode
  if (switchState == 1) {
    // display number of times button two has be pressed on the seven segment
    //display
    for (int segCount = 0; segCount < 7; ++segCount) {
      digitalWrite(pin, seven_segment_digits[limit][segCount]);
      ++pin;
    }
    // only Blue light
    digitalWrite(BLUE_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED, LOW);
    if (button2State == 0) {
      limit++;
      limit = limit%10;
    }
  }
  // otherwise enter drinking mode
  else {
    // display number of times button two has be pressed on the seven segment
    //display
    for (int segCount = 0; segCount < 7; ++segCount) {
      digitalWrite(pin, seven_segment_digits[count][segCount]);
      ++pin;
    }
    digitalWrite(BLUE_LED, LOW);
    if (button2State == 0) {
      count++;
      count = count%10;
    } 
    // if you at least two drinks under your limit then display a green light
    if (count < limit - 1) {
      digitalWrite(GREEN_LED, HIGH);
      digitalWrite(RED_LED, LOW);
    }
    // if you are a drink under your limit, flash a yellow light
    if (count == limit - 1) { // && limitSet
      if (flash == 0) {
        digitalWrite(GREEN_LED, HIGH);
        digitalWrite(RED_LED, HIGH);  
      } 
      else {
        digitalWrite(GREEN_LED, LOW);
        digitalWrite(RED_LED, LOW);
      }
      flash = (flash + 1)%2;
    }
    // if you are at or past your limit, flash a red light
    if (count >= limit) { // && limitSet
      digitalWrite(GREEN_LED, LOW);
      if (flash <= 4) {
        digitalWrite(RED_LED, LOW);  
      }
      else {
        digitalWrite(RED_LED, HIGH);
      }
      flash = (flash + 1)%20;
    }
  }
}

Credits

Audrey Deigaard, acd7

Audrey Deigaard, acd7

1 project • 4 followers
Jennifer Adams, ja60

Jennifer Adams, ja60

1 project • 5 followers
Justin Guilak, jag28

Justin Guilak, jag28

1 project • 5 followers
Ananya Vaidya, akv4

Ananya Vaidya, akv4

0 projects • 5 followers
Ryan Huckleberry, rth7

Ryan Huckleberry, rth7

0 projects • 5 followers
Adrienne Li, al81

Adrienne Li, al81

0 projects • 5 followers
Elena Margolin, erm7

Elena Margolin, erm7

0 projects • 5 followers
Bazin Sineshaw, bs66

Bazin Sineshaw, bs66

0 projects • 5 followers

Comments

Add projectSign up / Login