Chad - LED Moving Head Code

Go Back

LED Moving Head Code

Download: Servo_PWM.pde

/*

 * LED Moving Head Light

 * Wii Nunchuk, Servos, and MAX6969

 */

 

#include <Servo.h>

#include <Wire.h>

#include <WProgram.h>

 

static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

 

int loop_cnt=0;

 

byte joyx,joyy,accx,accy,zbut,cbut;

int posx,posy;

int pressed = 0;

 

Servo servox; // create servo object to control a servo (maximum of eight servo objects)

Servo servoy;

 

int DIN = 4; // Data In

int CLK = 3; // Clock

int LE = 2; // Latch Enable

 

byte shift1 = B00000000;

byte shift2 = B00000000;

 

byte red_shift1 = B10010010; // Red leds at OUT0, OUT3, and OUT6

byte red_shift2 = B01001000; // Red leds at OUT9 and OUT12

byte green_shift1 = B01001001; // Green leds at OUT1, OUT4, and OUT7

byte green_shift2 = B00100100; // Green leds at OUT10 and OUT13

byte blue_shift1 = B00100100; // Blue leds at OUT2 and OUT5

byte blue_shift2 = B10010010; // Blue leds at OUT8, OUT11, and OUT14

 

int max_bright = 63; // 64 different brightness levels

int red_bright = 0;

int green_bright = 0;

int blue_bright = 0;

 

int red_count = 0;

int green_count = 0;

int blue_count = 0;

 

int color = 0;

 

void setup()

{

    Serial.begin(19200);

    nunchuck_setpowerpins();

    nunchuck_init(); // send the initilization handshake

    servox.attach(9);  // attaches the servo on pin 9 to the servo object

    servoy.attach(10);  // attaches the servo on pin 10 to the servo object

 

    pinMode(DIN, OUTPUT);

    pinMode(CLK, OUTPUT);

    pinMode(LE, OUTPUT);

 

    Serial.print("WiiChuckDemo ready\n");

}

 

void loop()

{

    if( loop_cnt > 200 ) { // every 100 msecs get new data

        loop_cnt = 0;

        PWM_MAX();

 

        nunchuck_get_data();

        PWM_MAX();

 

        joyx  = nunchuck_joyx(); // ranges from approx 24 - 224

        PWM_MAX();

        joyy  = nunchuck_joyy(); // ranges from approx 33 - 226

        PWM_MAX();

        accx  = nunchuck_accelx(); // ranges from approx 70 - 182

        PWM_MAX();

        accy  = nunchuck_accely(); // ranges from approx 65 - 173

        PWM_MAX();

        zbut = nunchuck_zbutton();

        PWM_MAX();

        cbut = nunchuck_cbutton();

        PWM_MAX();

 

        //Serial.print("joyx: "); Serial.print((byte)joyx,DEC);

        //Serial.print("\tjoyy: "); Serial.print((byte)joyy,DEC);

        //Serial.print("\taccx: "); Serial.print((byte)accx,DEC);

        //Serial.print("\taccy: "); Serial.print((byte)accy,DEC);

        //Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);

        //Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);

 

        if(zbut == 1){

          posx = map(accx, 70, 182, 7, 179);

          PWM_MAX();

          posy = map(accy, 65, 173, 15, 179);

          PWM_MAX();

        }

        else {

          posx = map(joyx, 24, 224, 7, 179);

          PWM_MAX();

          posy = map(joyy, 33, 226, 15, 179);

          PWM_MAX();

        }

 

        if(cbut == 1 && pressed == 0){

          pressed = 1;

          PWM_MAX();

          if(color == 9){

            color = 0;

            PWM_MAX();

          }

          else {

            color++;

            PWM_MAX();

          }

        }

        else if(cbut == 0 && pressed == 1){

          pressed = 0;

          PWM_MAX();

        }

        PWM_MAX();

 

        servox.write(posx);

        PWM_MAX();

 

        servoy.write(posy);

        PWM_MAX();   

    }

    PWM_MAX();

    loop_cnt++;

    PWM_MAX();

 

}

 

void PWM_MAX() {

  // Red

  if (color == 1) {

    red_bright = 63;

    green_bright = 0;

    blue_bright = 0;

  }

  // Green

  else if (color == 2) {

    red_bright = 0;

    green_bright = 63;

    blue_bright = 0;

  }

  // Blue

  else if (color == 3) {

    red_bright = 0;

    green_bright = 0;

    blue_bright = 63;

  }

  // Yellow

  else if (color == 4) {

    red_bright = 63;

    green_bright = 54;

    blue_bright = 0;

  }

  // Orange

  else if (color == 5) {

    red_bright = 63;

    green_bright = 36;

    blue_bright = 0;

  }

  // Pink

  else if (color == 6) {

    red_bright = 63;

    green_bright = 0;

    blue_bright = 38;

  }

  // Purple

  else if (color == 7) {

    red_bright = 63;

    green_bright = 0;

    blue_bright = 60;

  }

  // Cyan

  else if (color == 8) {

    red_bright = 0;

    green_bright = 62;

    blue_bright = 63;

  }

  // White

  else if (color == 9) {

    red_bright = 63;

    green_bright = 56;

    blue_bright = 56;

  }

  // Black

  else {

    red_bright = 0;

    green_bright = 0;

    blue_bright = 0;

  }

 

  shift1 = B00000000;

  shift2 = B00000000;

 

  if(red_bright == 0){

    red_count = 0;

  }

  else if(red_count >= max_bright-red_bright) {

    shift1 = shift1 ^ red_shift1;

    shift2 = shift2 ^ red_shift2;

    red_count = 0;

  }

  else {

    red_count++;

  }

 

  if(green_bright == 0){

    green_count = 0;

  }

  else if(green_count >= max_bright-green_bright) {

    shift1 = shift1 ^ green_shift1;

    shift2 = shift2 ^ green_shift2;

    green_count = 0;

  }

  else {

    green_count++;

  }

 

  if(blue_bright == 0){

    blue_count = 0;

  }

  else if(blue_count >= max_bright-blue_bright) {

    shift1 = shift1 ^ blue_shift1;

    shift2 = shift2 ^ blue_shift2;

    blue_count = 0;

  }

  else {

    blue_count++;

  }

 

  digitalWrite(LE, LOW);

  shiftOut(DIN, CLK, LSBFIRST, shift2);

  shiftOut(DIN, CLK, LSBFIRST, shift1);

  digitalWrite(LE, HIGH);

}

 

// Uses port C (analog in) pins as power & ground for Nunchuck

static void nunchuck_setpowerpins()

{

#define pwrpin PORTC3

#define gndpin PORTC2

    DDRC |= _BV(pwrpin) | _BV(gndpin);

    PORTC &=~ _BV(gndpin);

    PORTC |=  _BV(pwrpin);

    delay(100);  // wait for things to stabilize       

}

 

// initialize the I2C system, join the I2C bus,

// and tell the nunchuck we're talking to it

static void nunchuck_init()

{

    Wire.begin();                // join i2c bus as master

    Wire.beginTransmission(0x52);// transmit to device 0x52

    Wire.send(0x40);// sends memory address

    Wire.send(0x00);// sends sent a zero. 

    Wire.endTransmission();// stop transmitting

}

 

// Send a request for data to the nunchuck

// was "send_zero()"

static void nunchuck_send_request()

{

    Wire.beginTransmission(0x52);// transmit to device 0x52

    PWM_MAX();

    Wire.send(0x00);// sends one byte

    PWM_MAX();

    Wire.endTransmission();// stop transmitting

    PWM_MAX();

}

 

// Encode data to format that most wiimote drivers except

// only needed if you use one of the regular wiimote drivers

static char nunchuk_decode_byte (char x)

{

    x = (x ^ 0x17) + 0x17;

    PWM_MAX();

    return x;

}

 

// Receive data back from the nunchuck,

// returns 1 on successful read. returns 0 on failure

static int nunchuck_get_data()

{

    int cnt=0;

    PWM_MAX();

    Wire.requestFrom (0x52, 6);// request data from nunchuck

    PWM_MAX();

    while (Wire.available ()) {

        // receive byte as an integer 

        nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.receive());

        PWM_MAX();

        cnt++;

        PWM_MAX();

    }

    nunchuck_send_request();  // send request for next data payload

    PWM_MAX();

    // If we recieved the 6 bytes, then go print them

    if (cnt >= 5) {

        return 1;   // success

    }

    PWM_MAX();

    return 0; //failure

}

 

// returns zbutton state: 1=pressed, 0=notpressed

static int nunchuck_zbutton()

{

    return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1;  // voodoo

}

 

// returns zbutton state: 1=pressed, 0=notpressed

static int nunchuck_cbutton()

{

    return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1;  // voodoo

}

 

// returns value of x-axis joystick

static int nunchuck_joyx()

{

    return nunchuck_buf[0];

}

 

// returns value of y-axis joystick

static int nunchuck_joyy()

{

    return nunchuck_buf[1];

}

 

// returns value of x-axis accelerometer

static int nunchuck_accelx()

{

    return nunchuck_buf[2];   // FIXME: this leaves out 2-bits of the data

}

 

// returns value of y-axis accelerometer

static int nunchuck_accely()

{

    return nunchuck_buf[3];   // FIXME: this leaves out 2-bits of the data

}

 

// returns value of z-axis accelerometer

static int nunchuck_accelz()

{

    return nunchuck_buf[4];   // FIXME: this leaves out 2-bits of the data

}

Go Back