Components and supplies
Arduino Micro
Apps and platforms
Arduino IDE
Project description
Code
LightHouseControl2016.ino
c_cpp
Interfaces the house to the computer running VixenLights. On VixenLights (free) I use the Renard plugin and select the serial port the Arduino is using.
1//Light House Control 2016 2//Warner King 3//RoboWarner.com 4 5//MM5450 code from l.e. hughes 6// Date.....: 27 May 2011 7// His Notes....: Basic, simple example of using shift registers to control the MM5450/5451 8// LED driver. This program was specifically written for the 5450, but should 9// also work with the 5451 and be easily modified for others. The 5450/5451 10// is latching and requires 36 signal databits to send all of the information 11// to control the LEDs (34 for the 5450; 35 for the 5451). There are minor 12// differences in the chip between the manufacturers, please read the appropriate 13// datasheet(s) before connecting power. 14// 15 16#define BITSB 8 // number of bits per byte, used for code clarity 17#define DATABITS 36 // what we must send to the chip in order to control the lights 18#define STARTBIT 1 // value of the starting bit; 19 20const int ledDim = 5; //LED common pin, used with PWM for a dimming effect 21const int ELpower = 3; //EL power supply power, also used with PWM for dimming 22const int ELblink = 2; //Used to fix EL wires getting stuck on, is pulsed after each refresh to reset stuck triacs. 23const int clockPin = 4; // connect Arduino pin 3 to clock pin (21) on the 5450 24const int dataPin = 6; // connect Arduino pin 6 to data pin (22) on the 5450 25 26const int numOfLightChannels = 32; 27const int numDimChannels = 2; 28const int startBytes = 2; 29 30// The following line just computes the number of bytes we will need in the ledArray to hold all of 31// bits of data for the signal; it could be declared statically. 32// 33const int arrayLen = (int)((DATABITS-1)/BITSB) + 1; 34 35// This is the actual array that will hold the signal bits. This program, for the 5450/5451, will 36// need 5 bytes for a total of 40 bits. 37// 38byte ledArray[arrayLen]; // for this chip, length is 5 and that could hold 40 values 39 40int myInts[40]; //Holds the data received from Vixen 41int errorReset = 0; //Used to reset the serial input code if bad data is gathered. 42bool ELpowerflag = false; //Used to automatically turn off EL power supply if not in use. 43 44 45typedef enum { // this exists primarily for code clarity 46 OFF, ON 47} ledState; 48 49void setup() { 50 Serial.begin(115200); 51 pinMode(LED_BUILTIN, OUTPUT); 52 pinMode(clockPin, OUTPUT); // we don't need a latch pin since the 5450/5451 latched after 53 pinMode(dataPin, OUTPUT); 54 pinMode(ledDim, OUTPUT); 55 pinMode(ELpower, OUTPUT); 56 pinMode(ELblink, OUTPUT); 57 digitalWrite(ledDim, LOW); 58 digitalWrite(ELpower, LOW); 59 digitalWrite(ELblink, LOW); 60 61 allOff(); 62 sendDatabits(); 63// allOn(); 64// sendDatabits(); 65 delay(1000); 66 } 67 68// the loop function runs over and over again forever 69void loop() { 70 71 if (Serial.available() >= (numOfLightChannels + startBytes + numDimChannels)) { 72 for(int y = 0; y <= (numOfLightChannels + startBytes + numDimChannels); y++) 73 { 74 myInts[y]= Serial.read(); //Gather data and store it in the array 75 } 76 if (myInts[0] == 126) //First two bytes transmitted each refresh by the Renard Plugin version 1 77 { 78 if (myInts[1] == 128) 79 { 80 errorReset = 0; //Valid signal, reset error counter 81 ELpowerflag = false; 82 for (int p = 0; p <= (numOfLightChannels); p++) //Translate data gathered from Vixen to the MM5450 array 83 { 84 if (myInts[p+1] == 255) //255 is full intensity on in Vixen 85 { 86 setLight(p, ON); 87 if(p <= (startBytes + 21)) //If any EL wire channels are on, let the EL power supply be activated 88 { 89 ELpowerflag = true; 90 } 91 } 92 else 93 { 94 setLight(p, OFF); 95 } 96 } 97 int ledMap = map(myInts[34], 0, 255, 255, 50); //Mapping for dimming. 98 99 int elMap = map(myInts[35], 0, 255, 40, 255); 100 if (ELpowerflag == false || myInts[35] == 0) //If no EL wire is turned on, turn off EL power supply 101 { 102 elMap = 0; 103 } 104 delay(90); //Timing to get lights in sync with Vixen, as the program outputs data before the actual point it's needed in the song. 105 analogWrite(ledDim, ledMap); //PWM for the dimming on the LEDs and EL power supply 106 analogWrite(ELpower, elMap ); 107 sendDatabits(); //Send data out to MM5450 108 if (myInts[20] == 255) //Activates built in LED, used for timing. 109 { 110 digitalWrite(LED_BUILTIN, ON); 111 } 112 else 113 { 114 digitalWrite(LED_BUILTIN, OFF); 115 } 116 digitalWrite(ELblink, HIGH); //Dirty fix for EL wire triacs getting stuck on, after each light refresh this will activate a transister to short out the EL AC output 117 delayMicroseconds(250); //thus allowing the stuck triacs to turn off. The delay is small enough it's not noticable to the eye. 118 digitalWrite(ELblink, LOW); 119 } 120 } 121 } 122 //Used to reset the serial input code if bad input is sent. 123 //Otherwise the above code will constantly loop and never find the proper data in the first 2 bytes. 124 else if (Serial.available() > 1) 125 { 126 errorReset++; 127 if (errorReset > 30) 128 { 129 while(Serial.available() > 0) 130 { 131 int trash = Serial.read(); 132 } 133 errorReset = 0; 134 135 } 136 } 137 delay(1); //Delay to prevent chaos 138} 139 140 141 142// Subroutine that sends all of the DATABITS to the chip. It begins by first sending the startbit, then it 143// uses the Arduino shiftOut function to send the bits in each byte of the ledArray. I could have put the 144// STARTBIT into the ledArray but decided that I liked it better outside of the array. Any time you want 145// to turn lights on or off, this routine must be called after setting the appropriate bits in the ledArray. 146// 147void sendDatabits() { 148 digitalWrite(clockPin, LOW); 149 delay(2); 150 digitalWrite(dataPin, STARTBIT); 151 delay(2); 152 digitalWrite(clockPin, HIGH); 153 delay(5); 154 digitalWrite(clockPin, LOW); 155 delay(2); 156 for(int i = 0; i < arrayLen; i++) { 157 shiftOut(dataPin, clockPin, LSBFIRST, ledArray[i]); 158 } 159 160} 161 162// Subroutine that takes a light (output pin) as a sole argument and sets the bit value for that pin 163// to the opposite of its current setting (toggle). 164// 165void toggleLight(int pin) { 166 byte arrayElem = int((pin-1)/BITSB); // which element of the ledArray is pin in 167 byte byteElem = (pin - (arrayElem * BITSB)) - 1; // and which bit in that byte is the pin 168 ledArray[arrayElem] ^= (1 << byteElem); // toggle byteElem 169} 170 171 172//This is modified from the original code, I added the bitWrite instead of using the original shift (<<) method. 173//It sets the selected pin to ON or OFF 174void setLight(int pin, byte val) { 175 176 byte arrayElem = int((pin-1)/BITSB); // which element of the ledArray is pin in 177 byte byteElem = (pin - (arrayElem * BITSB)) - 1; // and which bit in that byte is the pin 178byte temp1 = ledArray[arrayElem]; 179 bitWrite(temp1,byteElem , val) ; 180ledArray[arrayElem] = temp1; 181} 182 183// Subroutine that turns all lights on. Because the STARTBIT is 1 or ON, we don't want to set any 184// bits above 35 to ON, lest it be interpreted as the start of another set of databits. Better error 185// checking would be to make sure that no bit > 35 was ever set to 1. 186// 187void allOn() { 188 for(int i = 1; i < DATABITS; i++) { 189 setLight(i, ON); 190 } 191} 192 193// Subroutine that turns all lights off 194// 195void allOff() { 196 for(int i = 0; i < arrayLen; i++) { 197 ledArray[i] = 0; 198 } 199} 200
Downloadable files
Light Control Schematic
Light control schematic, connections for Arduino are shown.
Light Control Schematic
Light Control Schematic
Light control schematic, connections for Arduino are shown.
Light Control Schematic
Comments
Only logged in users can leave comments
robowarner
0 Followers
•0 Projects
Table of contents
Intro
4
0