Chad - PWM Code

Go Back

PWM Code

Download: PWM.pde

/*

 * PWM - MAX6969

 */

 

int DIN = 4; // Data In

int CLK = 3; // Clock

int LE = 2; // Latch Enable

 

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

 

byte shift1 = B00000000;

byte shift2 = B00000000;

 

int red_count = 0;

int green_count = 0;

int blue_count = 0;

 

int max_bright = 63; // 64 different brightness levels

int red_bright = 0;

int green_bright = 0;

int blue_bright = 0;

 

int color = 9; // Choose color

 

void setup() {               

  pinMode(DIN, OUTPUT);

  pinMode(CLK, OUTPUT);

  pinMode(LE, OUTPUT);

}

 

void loop() {

  calculateColor();

  calculateShift();

 

  digitalWrite(LE, LOW);

  shiftOut(DIN, CLK, LSBFIRST, shift2);

  shiftOut(DIN, CLK, LSBFIRST, shift1);

  digitalWrite(LE, HIGH);

}

void calculateColor() {

  // 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;

  }

}

 

void calculateShift() {

  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++;

  }

}

Go Back