Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MakerTron - MainteneTron
- // LED Pins
- int l_r = 3;
- int l_b = 5;
- int l_g = 6;
- int r_r = 9;
- int r_b = 10;
- int r_g = 11;
- int uv = 4;
- // Color arrays
- int black[3] = { 0, 0, 0 };
- int white[3] = { 100, 100, 100 };
- int red[3] = { 100, 0, 0 };
- int green[3] = { 0, 100, 0 };
- int blue[3] = { 0, 0, 100 };
- int yellow[3] = { 40, 95, 0 };
- int dimWhite[3] = { 30, 30, 30 };
- // etc.
- // Set initial color
- int redVal = black[0];
- int grnVal = black[1];
- int bluVal = black[2];
- int wait = 2; // 10ms internal crossFade delay; increase for slower fades
- int hold = 1000; // Optional hold when a color is complete, before the next crossFade
- int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
- int loopCount = 60; // How often should DEBUG report?
- int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
- int j = 0; // Loop counter for repeat
- // Initialize color variables
- int prevR = redVal;
- int prevG = grnVal;
- int prevB = bluVal;
- void setup() {
- pinMode(l_r, OUTPUT);
- pinMode(l_g, OUTPUT);
- pinMode(l_b, OUTPUT);
- pinMode(r_r, OUTPUT);
- pinMode(r_g, OUTPUT);
- pinMode(r_b, OUTPUT);
- pinMode(uv, OUTPUT);
- analogWrite(l_r, 255);
- analogWrite(l_g, 255);
- analogWrite(l_b, 255);
- analogWrite(r_r, 255);
- analogWrite(r_g, 255);
- analogWrite(r_b, 255);
- digitalWrite(uv, LOW);
- }
- void loop() {
- crossFade(red);
- laserFire();
- crossFade(green);
- laserFire();
- crossFade(blue);
- laserFire();
- crossFade(yellow);
- laserFire();
- }
- void laserFire(){
- for (int i = 0; i < 10; i++){
- digitalWrite(uv, HIGH);
- delay(50);
- digitalWrite(uv, LOW);
- delay(50);
- }
- }
- int calculateStep(int prevValue, int endValue) {
- int step = endValue - prevValue; // What's the overall gap?
- if (step) { // If its non-zero,
- step = 1020/step; // divide by 1020
- }
- return step;
- }
- /* The next function is calculateVal. When the loop value, i,
- * reaches the step size appropriate for one of the
- * colors, it increases or decreases the value of that color by 1.
- * (R, G, and B are each calculated separately.)
- */
- int calculateVal(int step, int val, int i) {
- if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
- if (step > 0) { // increment the value if step is positive...
- val += 1;
- }
- else if (step < 0) { // ...or decrement it if step is negative
- val -= 1;
- }
- }
- // Defensive driving: make sure val stays in the range 0-255
- if (val > 255) {
- val = 255;
- }
- else if (val < 0) {
- val = 0;
- }
- return val;
- }
- /* crossFade() converts the percentage colors to a
- * 0-255 range, then loops 1020 times, checking to see if
- * the value needs to be updated each time, then writing
- * the color values to the correct pins.
- */
- void crossFade(int color[3]) {
- // Convert to 0-255
- int R = (color[0] * 255) / 100;
- int G = (color[1] * 255) / 100;
- int B = (color[2] * 255) / 100;
- int stepR = calculateStep(prevR, R);
- int stepG = calculateStep(prevG, G);
- int stepB = calculateStep(prevB, B);
- for (int i = 0; i <= 1020; i++) {
- redVal = calculateVal(stepR, redVal, i);
- grnVal = calculateVal(stepG, grnVal, i);
- bluVal = calculateVal(stepB, bluVal, i);
- setEyeColorRGB(redVal, grnVal, bluVal);
- delay(wait); // Pause for 'wait' milliseconds before resuming the loop
- }
- // Update current values for next loop
- prevR = redVal;
- prevG = grnVal;
- prevB = bluVal;
- delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
- }
- void setEyeColorRGB(int r, int g, int b){
- r = r / 10;
- g = g / 10;
- b = b / 10;
- analogWrite(l_r, map(r, 0, 255, 255, 0));
- analogWrite(l_g, map(g, 0, 255, 255, 0));
- analogWrite(l_b, map(b, 0, 255, 255, 0));
- analogWrite(r_r, map(r, 0, 255, 255, 0));
- analogWrite(r_g, map(g, 0, 255, 255, 0));
- analogWrite(r_b, map(b, 0, 255, 255, 0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement