Go Down

Topic: My Joystick Color Program (Read 4877 times) previous topic - next topic


I wrote a program that changes the color and intensity of LED based on the joystick position. Just thought I would share the code with everyone who might be interested.


Code: [Select]

Joystick Color

This program will control the color and color intensity of the RGB
LED based on the joystick position of the Esplora. Moving the joystick
left and right will determine the color intensity of blue. Moving up will
determine the color intensity of green. Moving down will determine the
color intensity of red. Pressing switch 1 will calibrate the joystick

Created on 1 Jan 2014
by Scott Nguyen

#include <Esplora.h>

int joyx; // initiates joyx variable for joystick X
int joyy; // initiates joyy variable for joystick Y

void setup() {
  Serial.begin(9600); // begin serial communication baud rate 9600

void loop() {
  int red; // initiates red
  int green; // initials green
  int blue; // initiates blue
  int xpos; // initiates x position
  int ypos; // initiates y position
  int xdrift = Esplora.readJoystickX(); // takes joystick x readings and assigns to xdrift
  int ydrift = Esplora.readJoystickY(); // takes joystick y readings and assigns to ydrift
  do {
    joyx = Esplora.readJoystickX(); // takes readings from joystick x position and assigns to joyx
    xpos = joyx - xdrift; // calibrates xpos by accounting for x drift
    xpos = constrain(xpos, -512, 512); // constrains xpos between -512 and 512 and reassigns ouput value to xpos
    xpos = map(xpos, 512, -512, -255, 255); // maps xpos and reassigns it to xpos
    Serial.print("\nX position: "); // serial prints header for x position
    Serial.print(xpos); // serial prints xpos value
    joyy = Esplora.readJoystickY(); // takes readings from joystick y position and assigns to joyy
    ypos = joyy - ydrift; // calibrates ypos by accounting for y drift
    ypos = constrain(ypos, -512, 512); // constrains ypos between -512 and 512 and reassigns output value to ypos
    ypos = map(ypos, -512, 512, 255, -255); // maps ypos and reassigns it to ypos
    Serial.print("\tY position: "); // serial prints header for y position
    Serial.print(ypos); // serial prints ypos value
    if(ypos < -1) { // if joystick moves down
      red = ypos * -1; // y position value will be assigned to red and made positive
    if(ypos > 1) { // if joystick moves up
      green = ypos; // y position value will be assigned to green
    if(xpos > 1) { // if joystick moves right
      blue = xpos; // x position value will be assigned to blue
    if(xpos < -1) { // if joystick moves left
      blue = xpos * -1; // x position value will be assigned to blue and made positive
    Esplora.writeRGB(red, green, blue); // writes values of the colors to RGB LED
    delay(10); // delay 10 milliseconds
    red = 0; // assigns 0 to red
    blue = 0; // assigns 0 to blue
    green = 0; // assigns 0 to green
  } while(Esplora.readButton(SWITCH_1) == HIGH); //exit loops if switch 1 is pressed

I've been using the explora to practice embedded programming more rather than spending most of my time hooking things on breadboards. Best investment ever.


That's cool!  8)
Thanks for sharing :)


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131