Tilt It!

If you liked Bop It!, you'll love Tilt It!: a game of fast patterns, even faster reactions, and a large dose of world-tilting fun.

BeginnerFull instructions provided3 hours135
Tilt It!

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Breadboard (generic)
Breadboard (generic)
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
Push Button
×3

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Basic Breadboard

The Whole Setup

Wiring for the 7 Segment Display

Rest of The Wiring

Code

red_light_green_light

C/C++
This code is intended to be compiled in Energia and uploaded to the device.
#include <stdio.h> 
#include <stdlib.h> 

// set up the combinations of segments to display in a 2-D array
byte seven_segment_digits[5][7] = {  { 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,1,0,0,0,1,1 }, // display 'u'
                                     { 1,0,0,0,0,1,0 }  // display 'd'
                                   };

// Stores the states of each of the five inputs.
// 0 means the input is active.
int inputStates[] = {0, 0, 0, 0, 0};

// Set the pins of the LEDs, buttons, tilt switch,
// and the first segment of the 7-segment display.
// These can be modified depending on circuit layout.
int redPin = 17;
int greenPin = 18;
int buttonPins[] = {19, 14, 15, 13};
int segmentPin = 3;

// Reads the inputs from the pins.
void read() {
  for (int i = 0; i < 4; i++) {
    inputStates[i] = digitalRead(buttonPins[i]); 
  }
  inputStates[4] = !inputStates[3];
}

// Runs once at the beginning of the program
void setup() {
  // Sets the pins to input or output
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  for (int i = 0; i < 4; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPin + i, OUTPUT); 
  }
  
  // Turn the segments off to start.
  for(int i = 0; i < 7; i++) {
    digitalWrite(segmentPin + i, HIGH);
  }
  
  // Read the input
  read();
  
  // Loop until a button is pressed.
  int seed = 0;
  while (inputStates[0] != 0 && inputStates[1] != 0 && inputStates[2] != 0) {
    read();
    if (seed / 100000 % 2 == 0) {
      digitalWrite(greenPin, HIGH);
      digitalWrite(redPin, LOW);
    } else {
      digitalWrite(greenPin, LOW);
      digitalWrite(redPin, HIGH);
    }
    int lastOffset = (seed - 1) / 10000 % 6;
    int offset = seed / 10000 % 6;
    int offset2 = (seed / 10000 + 1) % 6;
    digitalWrite(segmentPin + lastOffset, HIGH);
    digitalWrite(segmentPin + offset, LOW);
    digitalWrite(segmentPin + offset2, LOW);
    seed++;
  }
  
  // Seed the RNG.
  srand(seed);
  
  //Turn everything off to start.
  digitalWrite(greenPin, LOW);
  digitalWrite(redPin, LOW);
  for(int i = 0; i < 7; i++) {
    digitalWrite(segmentPin + i, HIGH);
  }
}

// A function that returns which input has been set to 0 since the last read().
int getChange() {
  int oldStates[5];
  for (int i = 0; i < 5; i++) {
    oldStates[i] = inputStates[i];
  }
  read();
  for (int i = 0; i < 5; i++) {
    if (oldStates[i] != inputStates[i]) {
      if (inputStates[i] == 0) {
        return i;
      }
    }
  }
  return -1;
}

// The length of the pattern
int length = 1;

// This function should be repeatedly run after setup.
void loop() {
  // Read the current state.
  read();
  // Generate a pattern
  int pattern[length];
  int tilt = inputStates[3];
  for (int i = 0; i < length; i++) {
    pattern[i] = rand() % 4;
    // Only have an 'u' input if the device is tilted down,
    // and vice versa.
    if (pattern[i] == 3) {
      if (tilt == 0) {
        pattern[i] = 4;
      }
      tilt = !tilt;
    }
  }
  // Show the pattern.
  delay(1000);
  for (int button = 0; button < length; button++) {
    for (int i = 0; i < 7; i++) {
      digitalWrite(segmentPin + i, seven_segment_digits[pattern[button]][i]);
    }
    delay(200);
    for (int i = 0; i < 7; i++) {
      digitalWrite(segmentPin + i, 1);
    }
    delay(100);
  }
  // Read the current state.
  read();
  // Check each input state change against the pattern.
  int button = 0;
  while (button < length) {
    int change = getChange();
    if (change == pattern[button]) {
      // Input was correct.
      button++;
    } else if (change != -1) {
      // Input was incorrect.
      break;
    }
    delay(50);
  }
  // Blink the correct LED, and change the pattern length for difficulty.
  if (button == length) {
    digitalWrite(greenPin, HIGH);
    delay(200);
    digitalWrite(greenPin, LOW);
    length++;
  } else {
    digitalWrite(redPin, HIGH);
    delay(200);
    digitalWrite(redPin, LOW);
    if (length > 1) {
      length--;
    }
  }
}

Credits

Praneel Joshi

Praneel Joshi

1 project • 0 followers
Alexa Thomases

Alexa Thomases

1 project • 0 followers
Liam Garrison

Liam Garrison

1 project • 0 followers
Gedeon Pil

Gedeon Pil

1 project • 0 followers
Elisabeth Torres-Schulte

Elisabeth Torres-Schulte

1 project • 0 followers
Brian St. John

Brian St. John

1 project • 0 followers
Izzy Natchev

Izzy Natchev

1 project • 0 followers
Abby Geigerman

Abby Geigerman

0 projects • 0 followers

Comments