Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Xmas2011

By: UnaClocker on Nov 6th, 2011  |  syntax: C++  |  size: 9.95 KB  |  views: 207  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Ethernet connect christmas light controller
  2. // Written by Brian Schulteis, Creative Commons License, 2011
  3. // For use on an ENC28J60 ethernet chip with thiseldo's Ethershield library
  4. // Written for use on a Teensy++ 2.0, using Arduino 1.0
  5. #include "EtherShield.h"
  6.  
  7. static byte mymac[6] = {
  8.   0x54,0x55,0x58,0x10,0x00,0x25};
  9.  
  10. static byte myip[4] = {
  11.   192,168,1,1};  // This will be the IP address of the Arduino
  12. static byte mynetmask[4] = {
  13.   255,255,255,0}; // Netmask used on my network
  14. static byte gwip[4] = {
  15.   192,168,1,2}; // IP Address of my firewall on my LAN
  16. #define MYNETPORT 6058 // I picked some random port number for this to answer on. Not exactly sure why.
  17. #define BUFFER_SIZE 550 // default setting in the example sketch I grabbed for the ethershield
  18. static byte buf[BUFFER_SIZE+1];
  19.  
  20.  
  21. const int speeds[8] = {
  22.   64,96,128,192,256,320,400,512 }; // Array containing the speed settings.
  23. byte currentMode = 1; // Start in mode 1
  24. int currentSpeed = 64; // default to slow speed
  25. byte lightVariable = 1; // initialize the counters
  26. byte tempVariable = 1;
  27.  
  28.  
  29. EtherShield es=EtherShield();  // Object oriented programming FTW.. I love libraries.
  30.  
  31. void changeMode(char* modeRequested) {
  32.   currentMode = atoi(modeRequested);
  33.   if ((currentMode > 8) or (currentMode < 1)) {
  34.     currentMode=1;
  35.   }
  36.   for (int i=9; i < 20; i++) {
  37.     digitalWrite(i, LOW); // It's best to turn all the lights off before starting a different mode.
  38.   }  
  39.   lightVariable = 1;
  40.   tempVariable = 1;
  41.   tone(7, (100 * currentMode), 200); // Give a beep when the mode is changed, vary frequency based on requested mode.
  42. }
  43.  
  44. void changeSpeed(char* speedRequested) {
  45.   if ((atoi(speedRequested) > 0) and (atoi(speedRequested) < 9)) {
  46.     currentSpeed = speeds[(atoi(speedRequested)-1)];
  47.  
  48.     tone(7, (1200 * atoi(speedRequested)), 200); // Same deal, beep for speed changes, varied based on requested speed.
  49.   }
  50. }
  51.  
  52. boolean checkEthernet() {
  53.   int plen, dat_p;
  54.   // read packet, handle ping and wait for a tcp packet:
  55.   dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
  56.  
  57.   /* dat_p will be unequal to zero if there is a valid
  58.    * http get */
  59.   if (dat_p) {
  60.     if (buf[dat_p+5] == 'm') {
  61.       char temp = buf[dat_p+6];
  62.       changeMode(((char *)&temp));
  63.       dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR(""));
  64.       es.ES_www_server_reply(buf,dat_p); // send web page data
  65.       dat_p=0;
  66.       return(true); // True meaning "yes, there was a mode change"
  67.     }
  68.     if (buf[dat_p+5] == 's') {
  69.       char temp = buf[dat_p+6];
  70.       changeSpeed(((char *)&temp));
  71.       dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR(""));
  72.       es.ES_www_server_reply(buf,dat_p); // send web page data
  73.       dat_p=0;
  74.       return(false); // False, no mode change, speed change will get automatically rolled in to the main delay loop.
  75.     }
  76.   }
  77.   return(false); // False, nothing received from the ethernet chip.
  78. }
  79.  
  80. void modeOne() {  // This is a simple chasing pattern.
  81.   digitalWrite((lightVariable + 9), HIGH);
  82.   if (lightVariable < 10) {  // there are actually two on at any one time, if we're at the end, we need to go back to the beginning.
  83.     digitalWrite((lightVariable + 10), HIGH);
  84.   }
  85.   else { // Pin 10 being the beginning.
  86.     digitalWrite(10, HIGH);
  87.   }
  88.   if (lightVariable == 1) { // Same kind of deal with turning them off, if we're at 1, the last light may still be on
  89.     digitalWrite(19, LOW);  // so turn it off.
  90.   }
  91.   else {
  92.     digitalWrite((lightVariable + 8), LOW);
  93.   }
  94.   if (lightVariable == 10) {
  95.     lightVariable= 1;
  96.   }
  97.   else {
  98.     lightVariable++;
  99.   }
  100. }
  101.  
  102. void modeTwo() { // Start at ends, light up towards middle, then start in middle and turn off towards ends.
  103.   if (lightVariable < 6) { // 1 - 5 turns them on
  104.     digitalWrite(lightVariable + 9, HIGH);
  105.     digitalWrite(20 - lightVariable, HIGH);
  106.   }
  107.   else { // 6 through 10 turns them off, like ships passing in the night.
  108.     digitalWrite(lightVariable + 9, LOW);
  109.     digitalWrite(20 - lightVariable, LOW);
  110.   }
  111.   if (lightVariable == 10) {
  112.     lightVariable= 1;
  113.   }
  114.   else {
  115.     lightVariable++;
  116.   }
  117. }
  118.  
  119. void modeThree() { // The ever popular Knight Rider
  120.   if (lightVariable > 10) { // Go left
  121.     if (lightVariable == 11) {
  122.       tempVariable = 10;  // I think this will get rewritten and not use tempVariable.
  123.     }
  124.     else {
  125.       tempVariable--;
  126.     }
  127.     if (tempVariable > 1) {
  128.       digitalWrite(tempVariable + 8, HIGH);
  129.       digitalWrite(tempVariable + 9, LOW);
  130.     }
  131.     else {
  132.       digitalWrite(tempVariable + 8, LOW);
  133.     }
  134.     if (tempVariable == 10) {
  135.       digitalWrite(tempVariable + 9, LOW);
  136.     }
  137.   }
  138.   else { // Go right
  139.     digitalWrite(lightVariable +9, HIGH);
  140.     if (lightVariable > 1) digitalWrite(lightVariable + 8, LOW);
  141.   }
  142.   if (lightVariable == 20) {
  143.     lightVariable= 2;
  144.   }
  145.   else {
  146.     lightVariable++;
  147.   }
  148.  
  149. }
  150.  
  151. void modeFour() {  // Police light bar style
  152.   if (lightVariable < 6) {
  153.     if (lightVariable == 1) {
  154.       for (int h = 15; h < 20; h++) {
  155.         digitalWrite(h, LOW);
  156.       }
  157.     }
  158.     if ((lightVariable % 2) == 0) {
  159.       for (int h = 9; h < 15; h++) {
  160.         digitalWrite(h, HIGH);
  161.       }
  162.     }
  163.     else {
  164.       for (int h = 9; h < 15; h++) {
  165.         digitalWrite(h, LOW);
  166.       }
  167.     }
  168.   }
  169.   else {
  170.     if ((lightVariable % 2) == 0) {
  171.       for (int h = 15; h < 20; h++) {
  172.         digitalWrite(h, HIGH);
  173.       }
  174.     }
  175.     else {
  176.       for (int h = 15; h < 20; h++) {
  177.         digitalWrite(h, LOW);
  178.       }
  179.     }
  180.   }
  181.   if (lightVariable == 10) {
  182.     lightVariable= 1;
  183.   }
  184.   else {
  185.     lightVariable++;
  186.   }
  187. }
  188.  
  189. void modeFive() { // All on, then all back off in a VU meter kind of way
  190.   if (lightVariable < 11) { // Turning them on
  191.     digitalWrite(lightVariable + 9, HIGH);
  192.   }
  193.   else { // Turn them back off
  194.     digitalWrite(30 - lightVariable, LOW);
  195.   }
  196.   if (lightVariable == 20) {
  197.     lightVariable= 1;
  198.   }
  199.   else {
  200.     lightVariable++;
  201.   }
  202. }
  203.  
  204. void modeSix() { // double chaser mode
  205.   if (tempVariable == lightVariable) { // This can only happen when this mode is first engaged.
  206.     tempVariable = 5;
  207.   }
  208.   digitalWrite(lightVariable + 9, HIGH);
  209.   digitalWrite(tempVariable + 9, HIGH);
  210.   if (lightVariable > 1) {
  211.     digitalWrite(lightVariable + 8, LOW);
  212.   }
  213.   else {
  214.     digitalWrite(19, LOW);
  215.   }
  216.   if (tempVariable > 1) {
  217.     digitalWrite(tempVariable + 8, LOW);
  218.   }
  219.   else {
  220.     digitalWrite(19, LOW);
  221.   }
  222.   if (lightVariable == 10) {
  223.     lightVariable= 1;
  224.   }
  225.   else {
  226.     lightVariable++;
  227.   }
  228.   if (tempVariable == 10) {
  229.     tempVariable= 1;
  230.   }
  231.   else {
  232.     tempVariable++;
  233.   }
  234. }
  235.  
  236. void modeSeven() { // Start at ends, light up towards middle, then start at ends and turn off towards middle
  237.   if (lightVariable < 6) { // 1 - 5 turns them on
  238.     digitalWrite(lightVariable + 9, HIGH);
  239.     digitalWrite(20 - lightVariable, HIGH);
  240.   }
  241.   else { // 6 through 10 turns them off, like ships passing in the night.
  242.     tempVariable=lightVariable-5;
  243.     digitalWrite(tempVariable + 9, LOW);
  244.     digitalWrite(20 - tempVariable, LOW);
  245.   }
  246.   if (lightVariable == 10) {
  247.     lightVariable= 1;
  248.   }
  249.   else {
  250.     lightVariable++;
  251.   }
  252. }
  253.  
  254.  
  255. void modeSevenb() { // Odd man out mode - No longer used
  256.   digitalWrite(lightVariable + 9, HIGH);
  257.   if (lightVariable<10) {
  258.     digitalWrite(lightVariable + 10, LOW);
  259.   }
  260.   if (lightVariable == 2) {
  261.     digitalWrite(lightVariable + 8, LOW); // When we're starting the evens, we need to make sure to turn the first one off.
  262.   }
  263.   if ((lightVariable==9) or (lightVariable==10)) {
  264.     if (lightVariable==9) { // We're either doing all odd
  265.       lightVariable = 2;
  266.     }
  267.     if (lightVariable==10) { // or all even
  268.       lightVariable = 1; // Do the opposite the next time around.
  269.     }
  270.   }
  271.   else {
  272.     lightVariable += 2;
  273.   }
  274. }
  275.  
  276.  
  277. void modeEight() { // binary mode 10 bit, 2047 total
  278.   if (!lightVariable) { // See what I did there? -- if we've come back around to 0
  279.     if (digitalRead(18)) { // if bit 9 is already on
  280.       if (digitalRead(19)) { // and if bit 10 is already on
  281.         digitalWrite(18, LOW); // we got here because they're both on. We've gone all the way around
  282.         digitalWrite(19, LOW);
  283.       }
  284.       else {
  285.         digitalWrite(19, HIGH); // we got here because only 18 was on, this is the first lap
  286.         digitalWrite(18, LOW);
  287.       }
  288.     }
  289.     else {
  290.       digitalWrite(18, HIGH); // if 9 wasn't on, it's time to turn it on, we're at 511 or 1535, doesn't really matter
  291.     }
  292.     PORTC = lightVariable; // this turns off all the other bits (1-8)
  293.     lightVariable++;
  294.   }
  295.   else {
  296.     PORTC = lightVariable;
  297.     lightVariable++; // lightVariable is a byte, so it goes 0-255, with automatic rollover..
  298.   }
  299. }
  300.  
  301.  
  302. void setup(){
  303.   for (int i=9; i < 20; i++) {
  304.     pinMode(i, OUTPUT);
  305.   }
  306.   es.ES_enc28j60SpiInit();
  307.   // initialize enc28j60
  308.   es.ES_enc28j60Init(mymac,20 );
  309.   // init the ethernet/ip layer:
  310.   es.ES_init_ip_arp_udp_tcp(mymac,myip, MYNETPORT);
  311.   //  Serial.begin(9600);
  312. }
  313.  
  314. void loop(){
  315.   unsigned long currentTime = millis();
  316.   while ((!checkEthernet()) and ((millis()-currentTime) < currentSpeed)) {
  317.     /* I think this is an elegant way to delay, while checking for incoming commands over ethernet.
  318.      any speed changes automatically get rolled it, a mode change will break it out of this delay
  319.      automatically without any more delay. After the appropriate delay has elapsed, or it otherwise
  320.      breaks out of this delay loop, it calls the proper routine which makes one step forward along
  321.      it's pattern's progression. It then returns to this delay. Totally elegant. */
  322.   }
  323.   switch (currentMode) {
  324.   case 1:
  325.     modeOne();
  326.     break;
  327.   case 2:
  328.     modeTwo();
  329.     break;
  330.   case 3:
  331.     modeThree();
  332.     break;
  333.   case 4:
  334.     modeFour();
  335.     break;
  336.   case 5:
  337.     modeFive();
  338.     break;
  339.   case 6:
  340.     modeSix();
  341.     break;
  342.   case 7:
  343.     modeSeven();
  344.     break;
  345.   case 8:
  346.     modeEight();
  347.     break;
  348.   }
  349. }