Advertisement
zer044

Ir_led_cube_final

Aug 22nd, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. /**
  2. * by zer044 arduino account holder
  3. * 22/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.     switch (results.value) {
  47.    
  48.     case Button_1:
  49.       Serial.write("Button 1 pressed\n");
  50.        scrollthroughled ();
  51.        break;
  52.    
  53.  
  54.     case Button_2:
  55.       Serial.write("Button 2 pressed\n");    
  56.        scrollupanddown ();
  57.     break;
  58.    
  59.     case Button_3:
  60.       Serial.write("Button 3 pressed\n");    
  61.        spinthroughlayers ();
  62.        break;
  63.    
  64.    
  65.        case OFF:
  66.       Serial.write("ON/OFF pressed\n");    
  67.         allOff();
  68.         break;
  69.    
  70.     default :
  71.    Serial.println(results.value, HEX);
  72.     }
  73.     irrecv.resume(); // Receive the next value
  74.  
  75.   }
  76.  
  77. }
  78.  
  79.  
  80.  
  81. // all functions start here..
  82. /**
  83.  * list of current functions
  84.  * scrollthroughled ();
  85.  * scrollupanddown ();
  86.  * spinthroughlayers ();
  87.  **/
  88.  
  89. // function turns all leds off
  90.  
  91. void allOff () {
  92.   for (int j=0; j<9; j++) {
  93.     digitalWrite(colPins[j], LOW);
  94.   }
  95.   for (int i=0; i<3; i++) {
  96.     digitalWrite(levelPins[i], HIGH);
  97.   }
  98. }
  99. //function to turn an LED on.
  100. int ledOn (int x, int y, int t) {
  101.   digitalWrite(levelPins[x], LOW);
  102.   digitalWrite(colPins[y], HIGH);
  103.   delay(t);
  104.   digitalWrite(colPins[y], LOW);
  105.   digitalWrite(levelPins[x], HIGH);
  106. }
  107.  
  108. void scrollthroughled () {
  109.   for(int i=0; i<3; i++) {
  110.     for(int j=0; j<9; j++) {
  111.       ledOn(i,j,30);
  112.       delay(150);
  113.     }
  114.   }
  115. }
  116.  
  117. void spinthroughlayers () {
  118.   //middle layer
  119.   while(x<30) {
  120.     ledOn(0,3,4);
  121.     ledOn(1,3,4);
  122.     ledOn(2,3,4);
  123.     ledOn(1,4,4);
  124.     ledOn(0,4,4);
  125.     ledOn(2,4,4);
  126.     ledOn(0,5,4);
  127.     ledOn(1,5,4);
  128.     ledOn(2,5,4);
  129.     x++;
  130.   }
  131.   delay(50);
  132.   x=0;
  133.   //second layer
  134.   while(x<30) {
  135.     ledOn(0,6,4);
  136.     ledOn(1,6,4);
  137.     ledOn(2,6,4);
  138.     ledOn(1,4,4);
  139.     ledOn(0,4,4);
  140.     ledOn(2,4,4);
  141.     ledOn(0,2,4);
  142.     ledOn(1,2,4);
  143.     ledOn(2,2,4);
  144.     x++;
  145.   }
  146.   delay(50);
  147.   x=0;
  148.   // third layer
  149.   while(x<30) {
  150.     ledOn(0,7,4);
  151.     ledOn(1,7,4);
  152.     ledOn(2,7,4);
  153.     ledOn(1,4,4);
  154.     ledOn(0,4,4);
  155.     ledOn(2,4,4);
  156.     ledOn(0,1,4);
  157.     ledOn(1,1,4);
  158.     ledOn(2,1,4);
  159.     x++;
  160.   }
  161.   delay(50);
  162.   x=0;
  163.   // forth layer
  164.   while(x<30) {
  165.     ledOn(0,8,4);
  166.     ledOn(1,8,4);
  167.     ledOn(2,8,4);
  168.     ledOn(1,4,4);
  169.     ledOn(0,4,4);
  170.     ledOn(2,4,4);
  171.     ledOn(0,0,4);
  172.     ledOn(1,0,4);
  173.     ledOn(2,0,4);
  174.     x++;
  175.   }
  176.   delay(50);
  177.   x=0;
  178. }
  179.  
  180. void scrollupanddown () {
  181.   for (int d=0; d<3; d++) {
  182.     while(x < 30) {
  183.       ledOn(d,0,4);
  184.       ledOn(d,1,4);
  185.       ledOn(d,2,4);
  186.       ledOn(d,3,4);
  187.       ledOn(d,4,4);
  188.       ledOn(d,5,4);
  189.       ledOn(d,6,4);
  190.       ledOn(d,7,4);
  191.       ledOn(d,8,4);
  192.       x++;
  193.     }
  194.     delay(30);
  195.     x=0;
  196.   }
  197.   for (int d=2; d>-1; d--) {
  198.     while(x < 30) {
  199.       ledOn(d,0,4);
  200.       ledOn(d,1,4);
  201.       ledOn(d,2,4);
  202.       ledOn(d,3,4);
  203.       ledOn(d,4,4);
  204.       ledOn(d,5,4);
  205.       ledOn(d,6,4);
  206.       ledOn(d,7,4);
  207.       ledOn(d,8,4);
  208.       x++;
  209.     }
  210.    
  211.     x=0;
  212.     delay(30);
  213.   }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement