Advertisement
Guest User

arduino pong led strip

a guest
Nov 25th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. // Adafruit_NeoMatrix example for single NeoPixel Shield.
  2. // Scrolls 'Howdy' across the matrix in a portrait (vertical) orientation.
  3.  
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_NeoMatrix.h>
  6. #include <Adafruit_NeoPixel.h>
  7. #ifndef PSTR
  8.  #define PSTR // Make Arduino Due happy
  9. #endif
  10.  
  11. #define PIN 2
  12.  
  13. // MATRIX DECLARATION:
  14. // Parameter 1 = width of NeoPixel matrix
  15. // Parameter 2 = height of matrix
  16. // Parameter 3 = pin number (most are valid)
  17. // Parameter 4 = matrix layout flags, add together as needed:
  18. //   NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
  19. //     Position of the FIRST LED in the matrix; pick two, e.g.
  20. //     NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
  21. //   NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
  22. //     rows or in vertical columns, respectively; pick one or the other.
  23. //   NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
  24. //     in the same order, or alternate lines reverse direction; pick one.
  25. //   See example below for these values in action.
  26. // Parameter 5 = pixel type flags, add together as needed:
  27. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  28. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  29. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  30. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  31.  
  32.  
  33. // Example for NeoPixel Shield.  In this application we'd like to use it
  34. // as a 5x8 tall matrix, with the USB port positioned at the top of the
  35. // Arduino.  When held that way, the first pixel is at the top right, and
  36. // lines are arranged in columns, progressive order.  The shield uses
  37. // 800 KHz (v2) pixels that expect GRB color data.
  38. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(45, 20, PIN,
  39.   NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  40.   NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  41.   NEO_GRB            + NEO_KHZ800);
  42.  
  43.  
  44. const uint16_t colors[] = {
  45.   matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) };
  46.  
  47. //Matriz setup
  48. int width  = matrix.width();
  49. int height  =  matrix.height();
  50. int speedx = -1;
  51. int speedy = 1;
  52. int pass = 0;
  53. int x=width/2;
  54. int y=height/2;
  55.  
  56. int p1,p2;
  57.  
  58. int pote1=0;
  59. //int =0;
  60.  
  61. void barrita(int nump, int play){
  62.  
  63.   if (nump==1) {  
  64.       for (int i=0; i<5; i++) {
  65.         matrix.drawPixel(0,play-1+i,0xFFFF);
  66.         matrix.drawPixel(1,play-1+i,0xFFFF);
  67.         }
  68.       }
  69.       else { for (int i=0; i<5; i++){
  70.                   matrix.drawPixel(width-1 ,play-1+i,0xFFFF);
  71.                   matrix.drawPixel(width-2 ,play-1+i,0xFFFF);
  72.                 }
  73.              }
  74.  
  75. }
  76.      
  77.  
  78. void setup() {
  79.   //Matriz 1
  80.   matrix.begin();
  81.   matrix.setTextWrap(false);
  82.   matrix.setBrightness(40);
  83.   matrix.setTextColor(colors[0]);  
  84.  
  85.   p1=height/2;
  86.   p2=height/2;
  87.   Serial.begin(9600);
  88. }
  89.  
  90.  
  91. void loop() {
  92.           //Pelota
  93.           matrix.fillScreen(0);
  94.           x=x+speedx; y=y+speedy;
  95.           matrix.drawPixel(x,y,0xFFFF);
  96.          
  97.          
  98.           //Borde
  99.           bool cambio=false;
  100.          
  101.           for (int i=0; i<width; i++){
  102.                   matrix.drawPixel(i, 1, 0xFFFF);
  103.                   matrix.drawPixel(i, height-1, 0xFFFF);
  104.                   if ( ( (y==p1-1) || (y==p1) || (y==p1+1) || (y==p1+2) || (y==p1+3) ) &&  ((x==0) || (x==width-3)) ) {
  105.                       matrix.drawPixel(x, y, 0x0000);
  106.                 }
  107.                  
  108.            }  
  109.          
  110.           for (int i=0; i<height; i++){
  111.               if (cambio){
  112.                   matrix.drawPixel(width/2, i, 0xFFFF);
  113.                   cambio=false;
  114.                   }else{
  115.                       cambio=true;
  116.                     }
  117.               }
  118.          
  119.           //Player1&2
  120.           p1=analogRead(A0);
  121.           p2=analogRead(A1);
  122.          
  123.           p1=map(p1,100,900,0,11)+4;
  124.           p2=map(p2,100,900,0,10)+4;
  125.          
  126.           barrita(1,p1);
  127.           barrita(2,p2);
  128.          
  129.           //Player 2 print
  130.           Serial.print(p2-1);
  131.           Serial.print('-');
  132.           Serial.print(p2);
  133.           Serial.print('-');
  134.           Serial.print(p2+1);
  135.           Serial.print('-');
  136.           Serial.print(p2+2);
  137.           Serial.print('-');
  138.           Serial.print(p2+3);
  139.           Serial.print(' ');
  140.          
  141.           //Player 1 print
  142.           Serial.print(p1-1);
  143.           Serial.print('-');
  144.           Serial.print(p1);
  145.           Serial.print('-');
  146.           Serial.print(p1+1);
  147.           Serial.print('-');
  148.           Serial.print(p1+2);
  149.           Serial.print('-');
  150.           Serial.print(p1+3);
  151.           Serial.print(' ');
  152.          
  153.           //Posicion Print
  154.           Serial.print(x);
  155.           Serial.print(' ');
  156.           Serial.print(y);
  157.           Serial.println();
  158.      
  159.           //Colision y
  160.           if (y>=height-2 || y<=2)  speedy=-speedy;
  161.          
  162.           //Colision barras
  163.           int puntaje1=0, puntaje2=0;
  164.           bool colision1=false,colision2=false;
  165.          
  166.           if ( ((y==p1-1) || (y==p1) || (y==p1+1) || (y==p1+2) || (y==p1+3)) && (x==2) ){
  167.               colision1=true;  
  168.           }
  169.           if ( ((y==p2-1) || (y==p2) || (y==p2+1) || (y==p2+2) || (y==p2+3)) && (x==width-3) ){
  170.               //colision2=true;  
  171.           }
  172.          
  173.           if (colision1 || colision2 )  {
  174.               speedx=-speedx;
  175.               colision1=false;
  176.               colision2=false;
  177.           }
  178.           //Colision afuera del juego
  179.           else  if (x>=width-1 || x<=0)  {
  180.               x=width/2;
  181.               y=height/2;
  182.             }
  183.            
  184.           //puntaje
  185.           for (int i=0; i<puntaje1; i++){
  186.               for (int j=0; j<puntaje2; j++){
  187.                   matrix.drawPixel(i+1, 0, 0xFFFF);
  188.                   matrix.drawPixel(width-j,0,0xFFFF);
  189.               }  
  190.           }
  191.          
  192.           matrix.show();
  193.           delay(100);  
  194.          
  195.        
  196. }
  197.  
  198.  
  199.      //Matrix Test
  200.           /*for (int j=0; j<20; j++){
  201.             for (int i=0; i<45; i++){
  202.               //matrix.fillScreen(0);
  203.               matrix.drawPixel(i,j,0xFFFF);
  204.               matrix.show();
  205.               x++; y++;
  206.               delay(10);
  207.             }
  208.           }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement