Project 35 – Touch Screen Light Controller

In this project, you will use the touch screen to turn an RGB LED lamp on and off and to control the color of the LED.

Parts Required

You will be using the exact same parts and circuit as in Project 33 with the addition of an RGB LED. The RGB LED needs to be of the common cathode type. This means that one of the pins is connected to ground (the cathode) and the other three pins go separately to the control pins for the red, green, and blue voltages.

Nintendo DS touch screen images
Touch screen breakout images
RGB LED (common cathode) images
Current Limiting Resistor* images

*if needed

You will also need a keypad template as in Project 34. This time it needs to have areas for the colour sliders and the on/off buttons. Feel free to use the image in Figure 12-5.

images

Figure 12-5. The keypad diagram for Project 35 (see insert for color version)

Connect It Up

Connect everything up as in Figure 12-6.

images

Figure 12-6. The circuit for Project 35 – Touch Screen Light Controller (see insert for color version)

The ground wire goes to the common cathode pin of the LED. PWM Pin 3 goes to the red anode, PWM Pin 5 to the green anode, and PWM Pin 6 to the blue anode. Place current limiting resistors on the color pins if necessary. I used a Piranha RGB LED which is rated at 5v so it did not need any resistors, in my opinion. However, this is considered bad practice. Using LEDs without current limiting resistors will reduce their life.

Enter the Code

Enter the code in Listing 12-3.

Listing 12-3. Code for Project 35

// Project 35

// Power connections
#define Left 8     // Left (X1) to digital pin 8
#define Bottom 9   // Bottom (Y2) to digital pin 9
#define Right 10   // Right (X2) to digital pin 10
#define Top 11     // Top (Y1) to digital pin 11

// Analog connections
#define topInput 0   // Top (Y1) to analog pin 0
#define rightInput 1 // Right (X2) to analog pin 1

// RGB pins
#define pinR 3
#define pinG 5
#define pinB 6

int coordX = 0, coordY = 0;
boolean ledState = true;
int red = 100, green = 100, blue = 100;

void setup()
{
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinB, OUTPUT);
}

void loop()
{    
        if (touch()) {
                if ((coordX>0 && coordX<270) && (coordY>0 && coordY<460)) {ledState =images
 true; delay(50);}
                if ((coordX>0 && coordX<270) && (coordY>510 && coordY< 880)) {ledState =images
 false; delay(50);}
                if ((coordX>380 && coordX<930) && (coordY>0 && coordY<300)) {red=images
map(coordX, 380, 930, 0, 255);}
                if ((coordX>380 && coordX<930) && (coordY>350 && coordY<590))images
 {green=map(coordX, 380, 930, 0, 255);}
                if ((coordX>380 && coordX<930) && (coordY>640 && coordY<880))images
 {blue=map(coordX, 380, 930, 0, 255);}
                delay(10);
}

        if (ledState) {
                analogWrite(pinR, red);
                analogWrite(pinG, green);
                analogWrite(pinB, blue);
        }
        else {
                analogWrite(pinR, 0);
                analogWrite(pinG, 0);
                analogWrite(pinB, 0);
        }
}

// return TRUE if touched, and set coordinates to touchX and touchY
boolean touch()
{
  boolean touch = false;

  // get horizontal co-ordinates
  pinMode(Left, OUTPUT);
  digitalWrite(Left, LOW); // Set Left to Gnd

  pinMode(Right, OUTPUT); // Set right to +5v
  digitalWrite(Right, HIGH);

  pinMode(Top, INPUT); // Top and Bottom to high impedance
  pinMode(Bottom, INPUT);

  delay(3); // short delay
  coordX = analogRead(topInput);

  // get vertical co-ordinates
  pinMode(Bottom, OUTPUT); // set Bottom to Gnd
  digitalWrite(Bottom, LOW);

  pinMode(Top, OUTPUT); // set Top to +5v
  digitalWrite(Top, HIGH);

  pinMode(Right, INPUT); // left and right to high impedance
  pinMode(Left, INPUT);

  delay(3); // short delay
  coordY = analogRead(rightInput);

  // if co-ordinates read are less than 1000 and greater than 0 then the screen hasimages
been touched
  if(coordX < 1000 && coordX > 0 && coordY < 1000 && coordY > 0) {touch = true;}

    return touch;
}

Project 35 – Touch Screen Controller – Code Overview

The initial defines are the same as in Projects 33 and 34 with the addition of a set of defines for the three PWM pins used to control the R, G, and B components of the RGB LED:

// RGB pins
#define pinR 3
#define pinG 5
#define pinB 6

You add a Boolean called ledState and set it to true. This Boolean will hold the state of the LEDs, i.e. true = on and false = off.

boolean ledState = true;

A set of three integers are declared and initialized with the value of 100 each:

int red = 100, green = 100, blue = 100;

These three integers will hold the separate colour values for the LED. These will equate to the PWM values output from pins 3, 5, and 6.

In the main setup routine, the three LED pins you have defined are all set to outputs:

pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);

In the main loop, you have an if statement again to check if the value returned from touch() is true. Inside it are more if statements to decide which parts of the touch screen have been pressed. The first two define the borders of the ON and OFF buttons and change the ledState to true if a touch is detected within the border of the ON button and to false if it is within the borders of the OFF button. A short delay is included after this to prevent false readings from the buttons.

if ((coordX>0 && coordX<270) && (coordY>0 && coordY<460)) {ledState = true; delay(50);}
if ((coordX>0 && coordX<270) && (coordY>510 && coordY< 880)) {ledState = false; delay(50);}

Next, you check if a touch has been detected within the borders of the slider areas for the red, green, and blue controls. If a touch has been detected, then the value in the red, green, or blue integer is changed to match which part of the slider has been touched.

if ((coordX>380 && coordX<930) && (coordY>0 && coordY<300)) {red=map(coordX, 380, 930,images
 0, 255);}
if ((coordX>380 && coordX<930) && (coordY>350 && coordY<590)) {green=map(coordX, 380, 930,images
 0, 255);}
if ((coordX>380 && coordX<930) && (coordY>640 && coordY<880)) {blue=map(coordX, 380, 930,images
 0, 255);}

You accomplish this using a map() function, which takes five parameters. The first is the variable you are checking followed by the upper and lower ranges of the variable (all others are ignored). The final two parameters are the upper and lower ranges you wish to map the values to. In other words, you take the X coordinates within the slider area and map that value to go from 0 at the far left of the slider to 255 at the far right. By sliding your finger from left to right, you can make the relevant colour component change from 0 at its dimmest, which is off, to 255 at its maximum brightness.

Finally, you have another if statement to set the PWM values of the R, G, and B pins to the appropriate values stored in red, green, and blue, but only if ledState is true. An else statement sets the PWM values all to 0, or off, if ledState is false.

if (ledState) {
        analogWrite(pinR, red);
        analogWrite(pinG, green);
        analogWrite(pinB, blue);
}

else {
        analogWrite(pinR, 0);
        analogWrite(pinG, 0);
        analogWrite(pinB, 0);
}

The remainder of the program is the touch() function which has already been covered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.177.14