Ron Dagdag
Published © GPL3+

StormTrooper Laser Game

I made a game for my 5 year old kid that likes Star Wars with a Laser Pointer and a Light Sensor with a littlebit Arduino to collect points.

BeginnerShowcase (no instructions)786

Things used in this project

Hardware components

Gizmos & Gadgets
littleBits Gizmos & Gadgets
×1
littlebits servo
×1
littlebits arduino
×1
littlebits light sensor
×1
littlebits number
×1
laser pointer
×1
toy gun
×1
papercraft model
×1

Story

Read more

Schematics

littlebits blocks

Code

Counter.ino

Arduino
/*
littleBits Arduino Counter
Richard Born
May 7, 2014
*/

const int resetButton = A0; // this button will reset counter to zero
const int countButton = A1; // this button counts up and down presses of the count button
const int hundredsNumber = 5; // displays hundreds of counts
const int unitsNumber = 9; // displays count units
// The following array converts PWM values to integer values from 0 to 99
// For example: numbers[0] = 0; numbers[1] = 3; numbers[2] = 6; numbers[3] = 8, etc.
// This allows us to display a number on the number module based upon the corresponding PWM value
int numbers [] = {0,3,6,8,10,13,15,18,20,23,26,28,30,33,35,38,40,43,45,48,50,53,55,58,61,
                  64,66,69,71,74,76,78,81,84,86,89,91,94,96,99,102,104,106,108,111,114,116,
                  119,121,124,127,129,132,134,136,139,141,143,146,149,151,154,156,159,160,163,
                  166,169,171,173,176,179,181,184,186,188,190,193,196,199,202,205,208,210,212,
                  214,217,218,221,224,227,230,232,235,237,239,242,245,247,252,255};
int resetButtonState = 0;        // current state of the reset button
int countButtonState = 0;        // current state of the count button
int countOldButtonState = 0;     // previous state of the count button
int count = 0; // counts the number of presses of the count button
int upsAndDowns = 0; // counts presses and releases of the count button



void setup() {
	pinMode(resetButton, INPUT);     // sets pin to INPUT mode
	pinMode(countButton, INPUT);     // sets pin to INPUT mode
	pinMode(hundredsNumber, OUTPUT); // sets pin to OUTPUT mode
	pinMode(unitsNumber, OUTPUT);    // sets pin to OUTPUT mode
}

void loop() {
	
  resetButtonState = digitalRead(resetButton); // get the state of the reset button
  countButtonState = digitalRead(countButton); // get the state of the counter button
  
  if (resetButtonState == HIGH) {
	// reset both numbers and up and down counter to zero
        upsAndDowns = 0;
	analogWrite(hundredsNumber, numbers[0]);
	analogWrite(unitsNumber, numbers[0]);
  }
  if ((countButtonState == HIGH && countOldButtonState == LOW) || (countButtonState == LOW && countOldButtonState == HIGH)) {
        // counter button has been pressed down or has been released
        upsAndDowns += 1;
        count = upsAndDowns/2; // actual count is half the ups and downs
        if (count == 10000) {
          upsAndDowns = 0;
          count = 0;
        }
        countOldButtonState = countButtonState; // saves the old state of the count button
        analogWrite(hundredsNumber, numbers[count/100]); // displays the hundreds count
	analogWrite(unitsNumber, numbers[count % 100]);  // displays the units counts (remainder when divided by 100)
    
  }
	delay(20); // debounce time for buttons
}

Credits

Ron Dagdag

Ron Dagdag

47 projects • 436 followers
Microsoft MVP award / Lead Software Engineer / Augmented Reality. Developer Passionate to learn about Robotics, VR, AR, ML, IOT

Comments