Color fading shiftbrites either from user set colors or a random number generator.

Dependencies:   mbed

Committer:
bh27
Date:
Sun Mar 20 12:34:39 2011 +0000
Revision:
0:e719aa5eed4d
Child:
1:15ed52686283
v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bh27 0:e719aa5eed4d 1 // shiftbrite fading. Color patterns can eiher be user input or a random number generator can pick out values to use.
bh27 0:e719aa5eed4d 2 // Rate of fading can also be changed.
bh27 0:e719aa5eed4d 3 // Note: Watch for current spikes when transitioning between colors with multiple Shiftbrites in a chain.
bh27 0:e719aa5eed4d 4 // Setting the Current Control register to lower values (eg 31 instead of 127) can mediate this effect at the cost of brightness.
bh27 0:e719aa5eed4d 5
bh27 0:e719aa5eed4d 6 // Color fading example code from: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1253503406
bh27 0:e719aa5eed4d 7 // Shift register code from: http://mbed.org/users/ChriX/notebook/shift-register-function/
bh27 0:e719aa5eed4d 8 // Shiftbrite Arduino code from: http://macetech.com/blog/node/54
bh27 0:e719aa5eed4d 9
bh27 0:e719aa5eed4d 10 #include "mbed.h"
bh27 0:e719aa5eed4d 11
bh27 0:e719aa5eed4d 12 DigitalOut datapin(p30);
bh27 0:e719aa5eed4d 13 DigitalOut latchpin(p29);
bh27 0:e719aa5eed4d 14 DigitalOut enablepin(p28);
bh27 0:e719aa5eed4d 15 DigitalOut clockpin(p27);
bh27 0:e719aa5eed4d 16
bh27 0:e719aa5eed4d 17 volatile int colorSelected[3];
bh27 0:e719aa5eed4d 18 bool fadeComplete = true;
bh27 0:e719aa5eed4d 19
bh27 0:e719aa5eed4d 20 unsigned long SB_CommandPacket;
bh27 0:e719aa5eed4d 21 int SB_CommandMode;
bh27 0:e719aa5eed4d 22 int SB_ColorCommand[3];
bh27 0:e719aa5eed4d 23 int No_of_shiftbrites = 4;
bh27 0:e719aa5eed4d 24
bh27 0:e719aa5eed4d 25 // Current Control between 0 and 127
bh27 0:e719aa5eed4d 26 int Cur = 31;
bh27 0:e719aa5eed4d 27
bh27 0:e719aa5eed4d 28 void shiftOut(DigitalOut data, DigitalOut clk, int sodata) {
bh27 0:e719aa5eed4d 29
bh27 0:e719aa5eed4d 30 int i;
bh27 0:e719aa5eed4d 31
bh27 0:e719aa5eed4d 32 for (i = 7; i >= 0; i--) {
bh27 0:e719aa5eed4d 33
bh27 0:e719aa5eed4d 34 clk = 0;
bh27 0:e719aa5eed4d 35
bh27 0:e719aa5eed4d 36 if(sodata & (1 << i)){
bh27 0:e719aa5eed4d 37 data = 1;
bh27 0:e719aa5eed4d 38 } else {
bh27 0:e719aa5eed4d 39 data = 0;
bh27 0:e719aa5eed4d 40 }
bh27 0:e719aa5eed4d 41
bh27 0:e719aa5eed4d 42 clk = 1;
bh27 0:e719aa5eed4d 43 data = 0;
bh27 0:e719aa5eed4d 44 }
bh27 0:e719aa5eed4d 45
bh27 0:e719aa5eed4d 46 return;
bh27 0:e719aa5eed4d 47
bh27 0:e719aa5eed4d 48 }
bh27 0:e719aa5eed4d 49
bh27 0:e719aa5eed4d 50
bh27 0:e719aa5eed4d 51 void SB_SendPacket() {
bh27 0:e719aa5eed4d 52
bh27 0:e719aa5eed4d 53 for (int y = No_of_shiftbrites-1; y >= 0; y--) {
bh27 0:e719aa5eed4d 54 SB_CommandPacket = SB_CommandMode & 0xB11;
bh27 0:e719aa5eed4d 55 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_ColorCommand[0] & 1023);
bh27 0:e719aa5eed4d 56 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_ColorCommand[1] & 1023);
bh27 0:e719aa5eed4d 57 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_ColorCommand[2] & 1023);
bh27 0:e719aa5eed4d 58
bh27 0:e719aa5eed4d 59 shiftOut(datapin, clockpin, SB_CommandPacket >> 24);
bh27 0:e719aa5eed4d 60 shiftOut(datapin, clockpin, SB_CommandPacket >> 16);
bh27 0:e719aa5eed4d 61 shiftOut(datapin, clockpin, SB_CommandPacket >> 8);
bh27 0:e719aa5eed4d 62 shiftOut(datapin, clockpin, SB_CommandPacket);
bh27 0:e719aa5eed4d 63
bh27 0:e719aa5eed4d 64
bh27 0:e719aa5eed4d 65 wait_ms(1);
bh27 0:e719aa5eed4d 66 latchpin = 1;
bh27 0:e719aa5eed4d 67 wait_ms(1);
bh27 0:e719aa5eed4d 68 latchpin = 0;
bh27 0:e719aa5eed4d 69 }
bh27 0:e719aa5eed4d 70
bh27 0:e719aa5eed4d 71 return;
bh27 0:e719aa5eed4d 72
bh27 0:e719aa5eed4d 73 }
bh27 0:e719aa5eed4d 74
bh27 0:e719aa5eed4d 75 void Current_Control() {
bh27 0:e719aa5eed4d 76
bh27 0:e719aa5eed4d 77 int Comm = 0xB01;
bh27 0:e719aa5eed4d 78 int Cont1 = Cur;
bh27 0:e719aa5eed4d 79 int Cont2 = Cur;
bh27 0:e719aa5eed4d 80 int Cont3 = Cur;
bh27 0:e719aa5eed4d 81 unsigned long CommandPacket;
bh27 0:e719aa5eed4d 82
bh27 0:e719aa5eed4d 83 for (int y = No_of_shiftbrites-1; y >= 0; y--) {
bh27 0:e719aa5eed4d 84 CommandPacket = Comm & 0xB11;
bh27 0:e719aa5eed4d 85 CommandPacket = (CommandPacket << 10) | (Cont1 & 1023);
bh27 0:e719aa5eed4d 86 CommandPacket = (CommandPacket << 10) | (Cont2 & 1023);
bh27 0:e719aa5eed4d 87 CommandPacket = (CommandPacket << 10) | (Cont3 & 1023);
bh27 0:e719aa5eed4d 88
bh27 0:e719aa5eed4d 89 shiftOut(datapin, clockpin, CommandPacket >> 24);
bh27 0:e719aa5eed4d 90 shiftOut(datapin, clockpin, CommandPacket >> 16);
bh27 0:e719aa5eed4d 91 shiftOut(datapin, clockpin, CommandPacket >> 8);
bh27 0:e719aa5eed4d 92 shiftOut(datapin, clockpin, CommandPacket);
bh27 0:e719aa5eed4d 93
bh27 0:e719aa5eed4d 94
bh27 0:e719aa5eed4d 95 wait_ms(1);
bh27 0:e719aa5eed4d 96 latchpin = 1;
bh27 0:e719aa5eed4d 97 wait_ms(1);
bh27 0:e719aa5eed4d 98 latchpin = 0;
bh27 0:e719aa5eed4d 99 }
bh27 0:e719aa5eed4d 100
bh27 0:e719aa5eed4d 101 return;
bh27 0:e719aa5eed4d 102
bh27 0:e719aa5eed4d 103 }
bh27 0:e719aa5eed4d 104
bh27 0:e719aa5eed4d 105 void colorSelect()
bh27 0:e719aa5eed4d 106 {
bh27 0:e719aa5eed4d 107 for (int i = 0; i <= 2; i++)
bh27 0:e719aa5eed4d 108 {
bh27 0:e719aa5eed4d 109 colorSelected[i] = (rand() % 1023);
bh27 0:e719aa5eed4d 110 //if (colorSelected[i] > 1023) colorSelected[i] = 1023;
bh27 0:e719aa5eed4d 111 }
bh27 0:e719aa5eed4d 112
bh27 0:e719aa5eed4d 113 return;
bh27 0:e719aa5eed4d 114 }
bh27 0:e719aa5eed4d 115
bh27 0:e719aa5eed4d 116 void fadeall(int rate, int fromred, int fromgreen, int fromblue, int tored, int togreen, int toblue) {
bh27 0:e719aa5eed4d 117
bh27 0:e719aa5eed4d 118 for (int i = 0; i < 33; i++) {
bh27 0:e719aa5eed4d 119 SB_ColorCommand[0] = (fromblue * (32 - i) + toblue * i) / 32;
bh27 0:e719aa5eed4d 120 SB_ColorCommand[1] = (fromred * (32 - i) + tored * i) / 32;
bh27 0:e719aa5eed4d 121 SB_ColorCommand[2] = (fromgreen * (32 - i) + togreen * i) / 32;
bh27 0:e719aa5eed4d 122
bh27 0:e719aa5eed4d 123 SB_CommandMode = 0xB00;
bh27 0:e719aa5eed4d 124 SB_SendPacket();
bh27 0:e719aa5eed4d 125
bh27 0:e719aa5eed4d 126
bh27 0:e719aa5eed4d 127 Current_Control();
bh27 0:e719aa5eed4d 128 wait_ms(rate);
bh27 0:e719aa5eed4d 129 }
bh27 0:e719aa5eed4d 130
bh27 0:e719aa5eed4d 131 return;
bh27 0:e719aa5eed4d 132
bh27 0:e719aa5eed4d 133 }
bh27 0:e719aa5eed4d 134
bh27 0:e719aa5eed4d 135 int main() {
bh27 0:e719aa5eed4d 136 // Initialise previous color to start at Red.
bh27 0:e719aa5eed4d 137 int Prev_color[3] = {1023, 0, 0};
bh27 0:e719aa5eed4d 138
bh27 0:e719aa5eed4d 139 Current_Control();
bh27 0:e719aa5eed4d 140
bh27 0:e719aa5eed4d 141 while(1) {
bh27 0:e719aa5eed4d 142
bh27 0:e719aa5eed4d 143 // Fade using random color generation
bh27 0:e719aa5eed4d 144 colorSelect();
bh27 0:e719aa5eed4d 145 fadeall(50, Prev_color[0], Prev_color[1], Prev_color[2], colorSelected[0], colorSelected[1], colorSelected[2]);
bh27 0:e719aa5eed4d 146 for (int i = 0; i < 3; i++) Prev_color[i] = colorSelected[i];
bh27 0:e719aa5eed4d 147
bh27 0:e719aa5eed4d 148 // Fade using specified pattern
bh27 0:e719aa5eed4d 149 // eg. Red to Blue to Yellow back to Red as below
bh27 0:e719aa5eed4d 150 //fadeall(50, 1023, 0, 0, 0, 0, 1023);
bh27 0:e719aa5eed4d 151 //fadeall(50, 0, 0, 1023, 1023, 1023, 0);
bh27 0:e719aa5eed4d 152 //fadeall(50, 1023, 1023, 0, 1023, 0, 0);
bh27 0:e719aa5eed4d 153
bh27 0:e719aa5eed4d 154 }
bh27 0:e719aa5eed4d 155 }