Advertisement
zer044

updated IR_LED_CUBE code

Aug 12th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. /**
  2. * by zer044 arduino account holder
  3. * 13/08/2011
  4. * adaptions of Ken Sherriff IrLibary Example code.
  5. * http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
  6. **/
  7. //ir reciver bit
  8. #include <IRremote.h>
  9. // define codes
  10. #define OFF 0xFFA25D // ON/OFF button on remote
  11. #define Button_1 0xFF30CF // 1 on remote
  12. #define Button_2 0xFF18E7 // 2 on remote
  13. #define Button_3 0xFF7A85 // 3 on remote
  14. int RECV_PIN = 11;
  15. IRrecv irrecv(RECV_PIN);
  16. decode_results results;
  17.  
  18. //led cube bit
  19. int levelPins[] = {
  20.   22,23,24};
  21. int colPins[] = {
  22.   30,31,32,33,34,35,36,37,38};
  23. int x=0; // A COUNTER
  24. int c=0; // JUST ANOTHER COUNTER
  25. int pressed1=0;
  26.  // check if another is pressed
  27.  
  28. void setup () {
  29.   //ir receiver bit
  30.   Serial.begin(9600);
  31.   irrecv.enableIRIn(); // Start the receiver
  32.  
  33.   //led cube bit
  34.   //set all output
  35.   for (int i=0; i<3; i++) {
  36.     pinMode(levelPins[i], OUTPUT);
  37.   }
  38.   for (int j=0; j<9; j++) {
  39.     pinMode(colPins[j], OUTPUT);
  40.  
  41.   }
  42. }
  43. void loop () {
  44.   //ir receiver bit
  45.   if (irrecv.decode(&results)) {
  46.     if (results.value == Button_1) {
  47.       Serial.write("Button 1 pressed\n");
  48.        pressed1 = 1;
  49.     }
  50.  
  51.     if (results.value == Button_2) {
  52.       Serial.write("Button 2 pressed\n");    
  53.        pressed1 = 2;
  54.     }
  55.    
  56.     if (results.value == Button_3) {
  57.       Serial.write("Button 3 pressed\n");    
  58.        pressed1 = 3;
  59.     }
  60.    
  61.     if (results.value == OFF) {
  62.       Serial.write("ON/OFF pressed\n");    
  63.        pressed1 = 0;
  64.         allOff();
  65.     }
  66.    
  67.     Serial.println(results.value, HEX);
  68.  
  69.     irrecv.resume(); // Receive the next value
  70.  
  71.   }
  72.  
  73.   // led cube bit
  74.  if (pressed1 == 1) {scrollthroughled ();}
  75.  else if (pressed1 == 2) {scrollupanddown ();}
  76.  else if (pressed1 == 3) {spinthroughlayers ();}
  77.  
  78. }
  79.  
  80.  
  81.  
  82. // all functions start here..
  83. /**
  84.  * list of current functions
  85.  * scrollthroughled ();
  86.  * scrollupanddown ();
  87.  * spinthroughlayers ();
  88.  **/
  89.  
  90. // function turns all leds off
  91.  
  92. void allOff () {
  93.   for (int j=0; j<9; j++) {
  94.     digitalWrite(colPins[j], LOW);
  95.   }
  96.   for (int i=0; i<3; i++) {
  97.     digitalWrite(levelPins[i], HIGH);
  98.   }
  99. }
  100. //function to turn an LED on.
  101. int ledOn (int x, int y, int t) {
  102.   digitalWrite(levelPins[x], LOW);
  103.   digitalWrite(colPins[y], HIGH);
  104.   delay(t);
  105.   digitalWrite(colPins[y], LOW);
  106.   digitalWrite(levelPins[x], HIGH);
  107. }
  108.  
  109. void scrollthroughled () {
  110.   for(int i=0; i<3; i++) {
  111.     for(int j=0; j<9; j++) {
  112.       ledOn(i,j,30);
  113.       delay(150);
  114.     }
  115.   }
  116. }
  117.  
  118. void spinthroughlayers () {
  119.   //middle layer
  120.   while(x<30) {
  121.     ledOn(0,3,4);
  122.     ledOn(1,3,4);
  123.     ledOn(2,3,4);
  124.     ledOn(1,4,4);
  125.     ledOn(0,4,4);
  126.     ledOn(2,4,4);
  127.     ledOn(0,5,4);
  128.     ledOn(1,5,4);
  129.     ledOn(2,5,4);
  130.     x++;
  131.   }
  132.   delay(50);
  133.   x=0;
  134.   //second layer
  135.   while(x<30) {
  136.     ledOn(0,6,4);
  137.     ledOn(1,6,4);
  138.     ledOn(2,6,4);
  139.     ledOn(1,4,4);
  140.     ledOn(0,4,4);
  141.     ledOn(2,4,4);
  142.     ledOn(0,2,4);
  143.     ledOn(1,2,4);
  144.     ledOn(2,2,4);
  145.     x++;
  146.   }
  147.   delay(50);
  148.   x=0;
  149.   // third layer
  150.   while(x<30) {
  151.     ledOn(0,7,4);
  152.     ledOn(1,7,4);
  153.     ledOn(2,7,4);
  154.     ledOn(1,4,4);
  155.     ledOn(0,4,4);
  156.     ledOn(2,4,4);
  157.     ledOn(0,1,4);
  158.     ledOn(1,1,4);
  159.     ledOn(2,1,4);
  160.     x++;
  161.   }
  162.   delay(50);
  163.   x=0;
  164.   // forth layer
  165.   while(x<30) {
  166.     ledOn(0,8,4);
  167.     ledOn(1,8,4);
  168.     ledOn(2,8,4);
  169.     ledOn(1,4,4);
  170.     ledOn(0,4,4);
  171.     ledOn(2,4,4);
  172.     ledOn(0,0,4);
  173.     ledOn(1,0,4);
  174.     ledOn(2,0,4);
  175.     x++;
  176.   }
  177.   delay(50);
  178.   x=0;
  179. }
  180.  
  181. void scrollupanddown () {
  182.   for (int d=0; d<3; d++) {
  183.     while(x < 30) {
  184.       ledOn(d,0,4);
  185.       ledOn(d,1,4);
  186.       ledOn(d,2,4);
  187.       ledOn(d,3,4);
  188.       ledOn(d,4,4);
  189.       ledOn(d,5,4);
  190.       ledOn(d,6,4);
  191.       ledOn(d,7,4);
  192.       ledOn(d,8,4);
  193.       x++;
  194.     }
  195.     delay(30);
  196.     x=0;
  197.   }
  198.   for (int d=2; d>-1; d--) {
  199.     while(x < 30) {
  200.       ledOn(d,0,4);
  201.       ledOn(d,1,4);
  202.       ledOn(d,2,4);
  203.       ledOn(d,3,4);
  204.       ledOn(d,4,4);
  205.       ledOn(d,5,4);
  206.       ledOn(d,6,4);
  207.       ledOn(d,7,4);
  208.       ledOn(d,8,4);
  209.       x++;
  210.     }
  211.    
  212.     x=0;
  213.     delay(30);
  214.   }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement