Project 2- Crank it Up to Light it Up

For our second Physical Computing project, we were required to demonstrate mastery of the use of analog inputs and outputs, through the creation of a competitive game from which the winner is clearly indicated in some way.

Design

Coming up with a design for this project proved to be a little more difficult for me than coming up with an idea for project 1 had been. I always strive to create something unique, but I was having a hard time thinking outside the box his time. I felt like the potential uses for the majority of the analog sensors we were given were either too hard to integrate into a game or the ways in which they could be integrated were too obvious. However, after some thinking and some questions, I decided that I wanted to use DC motors, but I wanted to use it as an input instead of an output. I was warned that it might be hard, but lo and behold, the motors worked quite nicely as an input. And finally, I decided that I would use LEDs as my analog outputs to indicate which player was able to crank their motor faster.

One thing I wanted to do but wasn’t able was to make the lights sort of light up at different levels. For example when the input value was between 0 and some arbitrary x, I wanted the brightness of light 1 to correspond with that value, and when the value was greater than x, I wanted it to stay on fully lit. Therefore when the input value was between x and some y, I wanted light 1 to be fully lit and the brightness of light 2 to correspond to the input value. The same idea applies for light three. I tried to change my ranges in the manual tune function and add some if statements, but neither seemed to work. If I figure it out after some more tinkering, I’ll be sure to let you all know!

Parts

To create my crank game, I used the following equipment:

  • RedBoard by Sparkfun
  • Half-breadboard
  • Wires x15
  • Blue LED x3
  • Red LED x3
  • 330 Ohm Resister x6
  • DC Motor x2 (with handles for cranking. I used some wooden sticks and wooden spools from the craft store plus some hot glue and electric tape.)

Circuit DiagramProject 2 Circuit Diagram

See the circuit diagram to the right for reference. The circuit diagram was created with Fritzing.

To construct the crank game, I first had to figure out how to make DC motors, which are typically outputs, into inputs. I hooked it up in the most obvious way, with the motors connected to ground and analog input pins, and hoped for the best. Surprisingly enough, my setup worked! I then connected my LED lights (in series with a resister) to their own output pins. I did this because, as I mentioned earlier, my intention was to make the brightness of each light dependent upon the input values from the motor but independent from one another.

Code

The code I used was pretty simple and is as follows:

/*Project 2- Crank it Up to Light it Up
Playing with Analog Inputs and Outputs*/
// assigns variables to crank (DC motor) inputs
const int crank1 = A0;
const int crank2 = A5;

//assigns varaibles to LED outputs
const int player1LED1 = 11;
const int player1LED2 = 10;
const int player1LED3 = 9;
const int player2LED1 = 3;
const int player2LED2 = 5;
const int player2LED3 = 6;

//assigns variables to be used later
int brightness11;
int brightness12;
int brightness13;
int brightness21;
int brightness22;
int brightness23;

void setup()
{ 

  //assigns player one's output pins
  pinMode(player1LED1, OUTPUT);
  pinMode(player1LED2, OUTPUT);
  pinMode(player1LED3, OUTPUT);

  //assigns player two's output pins
  pinMode(player2LED1, OUTPUT);
  pinMode(player2LED2, OUTPUT);
  pinMode(player2LED3, OUTPUT);
}

void loop()
{
  //the brightness of these three lights is dependent on input from crank 1
  brightness11 = analogRead(crank1);
  brightness12 = analogRead(crank1);
  brightness13 = analogRead(crank1);

  //the brightness of these three lights is dependent on input from crank 2
  brightness21 = analogRead(crank2);
  brightness22 = analogRead(crank2);
  brightness23 = analogRead(crank2);

  manualTune(); //"activates" manual tune function

  analogWrite(player1LED1, brightness11);
  analogWrite(player1LED2, brightness12);
  analogWrite(player1LED3, brightness13);

  analogWrite(player2LED1, brightness21);
  analogWrite(player2LED2, brightness22);
  analogWrite(player2LED3, brightness23);

}

void manualTune()
{

  //map() changes the input range of values to the output range of vlaues
  //constrain() will force numbers outside range into range

  brightness11 = map(brightness11, 0, 256, 0, 255);
  brightness11 = constrain(brightness11, 0, 255);

  brightness12 = map(brightness12, 0, 512, 0, 255);
  brightness12 = constrain(brightness12, 0, 255);

  brightness13 = map(brightness13, 0, 1023, 0, 255);
  brightness13 = constrain(brightness13, 0, 255);

  brightness21 = map(brightness21, 0, 256, 0, 255);
  brightness21 = constrain(brightness21, 0, 255);

  brightness22 = map(brightness22, 0, 512, 0, 255);
  brightness22 = constrain(brightness22, 0, 255);

  brightness23 = map(brightness23, 0, 1023, 0, 255);
  brightness23 = constrain(brightness23, 0, 255);

}

In the beginning, I assigned global variables for the LEDs, the motors, and the brightness levels of the LEDs to be used throughout the program. In the setup function, I assigned the LED pins to be outputs. In the loop function I made the brightnesses of the LEDs dependent upon the input values coming from the motors. I then introduced the manual tune function into the loop. The manual tune function contains the map function, which takes values from the input range and converts them to values for the output range, and the constrain function forces all the values that come in outside the range back into the range. The map function is also what allowed me to make the light closest to the player get the brightest the fastest and the one farthest away to brighten the slowest.

Improvements/Modifications

There are a couple improvements I think this game would benefit having. The first is that it can be more sensitive. When using the lights to judge, it wasn’t always obvious who was cranking faster. I also think the game can use a timer of sorts. Playing a game with no declared winner isn’t as much fun. Therefore, I think there should be a time limit in the game, and it will tell the player who had the best average crank speed or the player who had the highest speed overall that they are the winner.

 

 

Leave a Reply

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