loading

This is the first Arduino project that I've created myself. I had a bunch of LEDS and a pressure sensor, so I thought I'd make a little game. The harder you squeeze the pressure sensor, the more LEDS light up in a row. If you light up the final LED, all of the LEDS flash on and off. It's pretty simple so let's get started!

Step 1: What You Need

For this project, you will need the following:

Arduino Uno

Breadboard

1 1K ohm resistor (or approximate)

6 180 ohm resistor (or approximate)

Force sensitive resistor (like this)

6 LEDs (any size or color)

Jumper Wires

Step 2: Setting Up Your Breadboard

Set up your breadboard as shown. Below is a write up in case you can't see where everything is going in the image.

First place a wire from 5v on the Arduino to the positive side of your breadboard.

Next place a wire from Ground on your Arduino to the negative side of your breadboard.

Force Sensitive Resistor: Connect one leg to positive. For the other leg, place the 1k resistor and connect it to negative.

LEDS: Line up your LEDs in a row on the breadboard. I'm going to list mine by the color that I used, and I will use that color later in the code. You can use different colors than I did, and you can change the color name in the code if you'd like, but you don't have to if you can remember which one is which.

NOTE: When you place the pins to the Arduino, any color can go to any pin BUT the code must be edited. You must use pins that have the ~ next to the number, as those allow for analog use of the pin. We'll be using pulse width modulation to change the brightness of our LEDS dependent on pressure, so you must use the pins with the ~. It's probably easiest to use the pins that I mention here so that you don't have to edit the code.

Red: Negative side of LED (shorter wire) to ground through a 180ohm resistor. Positive side of LED (longer wire) to pin 11 of the Arduino.

Yellow 1: Negative, through 180 ohm resistor, to ground. Positive to pin 10.

Yellow 2: Negative, through 180 ohm resistor, to ground. Positive to pin 9.

Green 1: Negative, through 180 ohm resistor, to ground. Positive to pin 6.

Green 2: Negative, through 180 ohm resistor, to ground. Positive to pin 5.

Blue: Negative, through 180 ohm resistor, to ground. Positive to pin 3.

Step 3: The Code

Now copy and paste the following sketch into Arduino and upload it. If you changed the pins around, then your LEDS might be out of order and you'll have to either reorder the pins to match my code or edit the code. I explain the code in the next step so you can see what's going on. I'm sure this code could be written more elegantly, but this one works for me and it's short enough that it's not too bad.

// LED Pressure Game with Force Sensitive Resistor
const int red = 11; // LEDs connected to each pin

const int yellow1 = 10;

const int yellow2 = 9;

const int green1 = 6;

const int green2 = 5;

const int blue = 3;

const int FSR = 0; // FSR connected to analog pin 0

int level = 0; // variable for the level of force applied to the FSR

void setup() // run once to setup

{

pinMode (red, OUTPUT); // LED set as output

pinMode (yellow1, OUTPUT);

pinMode (yellow2, OUTPUT);

pinMode (green1, OUTPUT);

pinMode (green2, OUTPUT);

pinMode (blue, OUTPUT);

//analog pins are automatically set to input

}

void loop() // repeated routine

{

// first, read the pressure level

int level = analogRead(FSR);

//next, light up the LEDS according to pressure

if (level <200) {

analogWrite (red, level);

}
if (level >200) {

analogWrite (yellow1, level);

}

if (level >400) {

analogWrite (yellow2, level);

}

if (level >600 ) {

analogWrite (green1, level);

}

if (level >700) {

analogWrite (green2, level);

}

if (level >800) {

analogWrite (blue, level);

}

//Next tell it to turn off all lights if level = 0

if(level == 0) {

digitalWrite (red, LOW);

digitalWrite (yellow1, LOW);

digitalWrite (yellow2 ,LOW);

digitalWrite (green1, LOW);

digitalWrite (green2, LOW);

digitalWrite (blue, LOW);

}

//tell it to flash LEDS if pressure is greater than 800

if (level > 800) {

digitalWrite (red, HIGH);

digitalWrite (yellow1, HIGH);

digitalWrite (yellow2 ,HIGH);

digitalWrite (green1, HIGH);

digitalWrite (green2, HIGH);

digitalWrite (blue, HIGH);

delay(100);

digitalWrite (red, LOW);

digitalWrite (yellow1, LOW);

digitalWrite (yellow2 ,LOW);

digitalWrite (green1, LOW);

digitalWrite (green2, LOW);

digitalWrite (blue, LOW);

delay(100);

}

}

Step 4: Explanation of Code

Now let's break down the code. First we see:


// LED Pressure Game with Force Sensitive Resistor
const int red = 11; // LEDs connected to each pin const int yellow1 = 10; const int yellow2 = 9; const int green1 = 6; const int green2 = 5; const int blue = 3; const int FSR = 0; // FSR connected to analog pin 0

I have seen this written as const int or just int, but I went with const int and it worked for me.

We are just telling the arduino which pins we are using, and setting a code for ourselves to make it easier to remember which pin is which. This is where you could rename the colors to match your setup. If you change the color name here, do a Ctrl+F and find all instances of that word and replace it.

Next is

int level = 0; // variable for the level of force applied to the FSR

This just sets up a variable for level, which we will use to measure the force applied to the FSR (Force Sensitive Resistor). We set it to 0 initially.

void setup() // run once to setup { 
  pinMode (red, OUTPUT); // LED set as output
  pinMode (yellow1, OUTPUT);
  pinMode (yellow2, OUTPUT);
  pinMode (green1, OUTPUT);
  pinMode (green2, OUTPUT);
  pinMode (blue, OUTPUT);}

The void setup runs once to setup the sketch. We are just telling the Arduino which pins we want as output. Note that the analog pin where we have the FSR is automatically set as input, so we don't have to put that here.

void loop() // repeated routine {
   // first, read the pressure level 
   int level = analogRead(FSR); 
   

Then the void loop runs over and over again. First, we set up our variable level (which we introduced earlier) to read our FSR, which we sets to pin 0 earlier.

//next, set the LEDs to light based on pressure
if (level <200) { analogWrite (red, level); } if (level >200) { analogWrite (yellow1, level); } if (level >400) { analogWrite (yellow2, level); } if (level >600 ) { analogWrite (green1, level); } if (level >700) { analogWrite (green2, level); } if (level >800) { analogWrite (blue, level); }

Next we tell our LEDs when to light up. You can change these levels, but I have the first one lighting up if level is less than 200. Then each other lights up if it's greater than 400, 600, 700, and finally 800. You can make these numbers higher or lower to make the game harder or easier. I found 800 was a good number for my FSR, it's difficult to reach but not impossible. Your FSR might be different, so you can experiment with numbers to find the right fit to make your game fun.

You want to make sure you do these in the order that your LEDs are on the board. This way they light up in sequence. If they light up randomly it takes away from the visual effect of the game.

//set all LEDs to turn off if pressure is 0 

if(level == 0)
   {
   digitalWrite (red, LOW);
   digitalWrite (yellow1, LOW);
   digitalWrite (yellow2 ,LOW);
   digitalWrite (green1, LOW);
   digitalWrite (green2, LOW);
   digitalWrite (blue, LOW); }

I'm not sure if this part is really necessary, but I couldn't get it to work without it (my LEDS would stay lit after you let go of the FSR). This just says if level is equal to 0, turn all the LEDs off. Note that "equal" for Arduino is not =, but ==. I made that mistake at first.

//tell all LEDs to flash if pressure is greater than 800 if (level > 800)
   {
     digitalWrite (red, HIGH);
digitalWrite (yellow1, HIGH);
   digitalWrite (yellow2 ,HIGH);
   digitalWrite (green1, HIGH);
   digitalWrite (green2, HIGH);
   digitalWrite (blue, HIGH);
   delay(100);
   digitalWrite (red, LOW);
digitalWrite (yellow1, LOW);
   digitalWrite (yellow2 ,LOW);
   digitalWrite (green1, LOW);
   digitalWrite (green2, LOW);
   digitalWrite (blue, LOW);
   delay(100);
     
   }

}

Now for the last bit of code. When you squeeze hard enough to light up all the LEDs, I wanted all of them to flash. This code tells all the LEDs to turn on (HIGH), then wait 1 second, then turn off again (LOW). As long as the level is greater than 800, this will continue over and over again, making them appear to flash. You can alter the delay here to make the flash faster or slower if you wish.

That's it!

Upload your sketch to your Arduino and see if you can light the final LED and make them all flash!

<p>Looks like a fun project. Thanks for sharing your build!</p>

About This Instructable

2,322views

16favorites

License:

More by carlygermann:LED Pressure Game Arduino 3d Printed Ecig Stand 
Add instructable to: