Advertisement
zer044

IR_LED_Cube code

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