12. Output devices

Since I’ve worked with some of the output devices, such as servomotors or LEDs, I will try and put together a small project using different in- and output devices.

Project Idea

Lately, a friend of mine told me about some hot potato-like game, where you have to solve different problems or answer question and if you were correct, you throw the potato (which is a bomb in this game) to your neighbor. When the bomb explodes while you hold it, you lost.

I’ll use a regular tennisball as the “case”.

The bomb has to make a sound implicating the time until it explodes.

I want to use an accelerometer to track the motion while being thrown, adapting the sound to the height of the ball, maybe.

An multicolor LED that is used to indicate the current mode the ballbomb is in.

All of this may sound rather weird firstly, but I hope will make sense in the end. =)

Programming

I use a rebuilt Arduino Nano I ordered online for experimenting and testing purposes.

It is very cheap and very easy to use.

In addition to that, I ordered 37 different sensors from the same seller to have some more possibilities.

I keep using the Arduino IDE for programming and can use the controller as any other Arduino.

I will start with a simple sketch for each of the single components I’ll use to introduce them and will later on bring them together in a united project!

RGB LED

IMAGE!!

I connect the four pins of my RGB LED with the Arduino.

Pins 1, 2 and 3 are for red green and blue each. I connect them with the pins 9, 10 and 11 of my Arduino.

In addition, pin 4 of the LED is connected to GND of my board.

VIDEO !!

I then programmed the code you can see below to the arduino and gave it a try.

As you can see it works just fine. I can write a number between 0 and 255 to each of the pins to manipulate each of them and create any color.

Code

#define BLUE 11
#define GREEN 10
#define RED 9

void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}

int redValue;
int greenValue;
int blueValue;

void loop()
{
  setRed();
  delay(500);
  setGreen();
  delay(500);
  setBlue();
  delay(500);
}

void setRed(){
  redValue = 255;
  greenValue = 0;
  blueValue = 0;
  setColor();
}

void setGreen(){
  redValue = 0;
  greenValue = 255;
  blueValue = 0;
  setColor();
}

void setBlue(){
  redValue = 0;
  greenValue = 0;
  blueValue = 255;
  setColor();
}

void setColor(){
  analogWrite(RED, redValue);
  analogWrite(GREEN, greenValue);
  analogWrite(BLUE, blueValue);
}