Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /********** Arduino **********
  2.  * Laboratorio 04: 8 LEDs desde el Arduino, a través de un IC 74HC595
  3.  * Autor: Cristian Augusto Restrepo
  4.  * Descripcion:
  5.  *   Controlar 8 LEDs desde el Arduino, a través de un IC 74HC595,
  6.  *   definiendo mínimo 8 patrones de movimiento que son controlados desde una interfaz gráfica
  7.  *   en Processing/ControlP5. Más información sobre el IC 74HC595
  8.  
  9. char val; // Data received from the serial port
  10.  int ledPin = 13; // Set the pin to digital I/O 13
  11.  
  12.   //Pin connected to ST_CP of 74HC595
  13.   int latchPin = 8;
  14.   //Pin connected to SH_CP of 74HC595
  15.   int clockPin = 12;
  16.   ////Pin connected to DS of 74HC595
  17.   int dataPin = 11;
  18.  
  19. byte data;
  20. byte dataArray[10];
  21.  
  22. //movimientos
  23. int Sec1[9] = {0,1, 2,4, 8,16, 32,64, 128}; //UNO
  24. int Sec2[9] = {0,1, 2,4, 8,16, 32,64, 128}; //DOS
  25. int Sec3[5] = {0,3 ,15,63, 255}; //TRES
  26. int Sec4[5] = {0,3 ,51,63, 255}; //CUATRO
  27. int Sec5[5] = {0,129 ,195,231, 255}; //CINCO
  28. int Sec6[8] = {0,24, 36,66, 129,255, 0,255}; //SEIS
  29. int Sec7[10] = {0,170, 0,85, 0,255, 170,255, 85,255}; //SIETE
  30. int Sec8[9] = {0,1, 3,7, 15,31, 63,127, 255}; //OCHO
  31.  
  32. //int Caos[9] = {}; //en desorden// usamos en shift el random 255
  33.  
  34. //int Mitad[5] = { 0, 15, 255};
  35.  
  36. //int Depredador[9] = { 255,63,7,31,51,7,1,3,0};
  37.  
  38. //int Bomba[9] = { 255,127,63,31,15,7,3,1,0};
  39.  
  40. //int Laser[9] = {0,1, 2,4,8,16,32,64,128};
  41.  
  42. void setup()
  43. {
  44.   //set pins to output so you can control the shift register
  45.   pinMode(latchPin, OUTPUT);
  46.   pinMode(clockPin, OUTPUT);
  47.   pinMode(dataPin, OUTPUT);
  48.  
  49.  
  50.     pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  51. //initialize serial communications at a 9600 baud rate
  52. Serial.begin(9600);
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  void loop() {
  62.    if (Serial.available())
  63.    { // If data is available to read,
  64.      val = Serial.read(); // read it and store it in val
  65.      Serial.println(val);
  66.    }
  67.  
  68.   //*********************************** Sec1 **************************************************************
  69.    if (val == '1')
  70.    { // If 1 was received
  71.      digitalWrite(ledPin, HIGH); // turn the LED on
  72.      
  73.      //ir
  74.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  75.           data = Sec2[numberToDisplay];
  76.           // the LEDs don't change while you're sending in bits:
  77.           digitalWrite(latchPin, LOW);
  78.           // shift out the bits:
  79.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  80.           //take the latch pin high so the LEDs will light up:
  81.           digitalWrite(latchPin, HIGH);
  82.           // pause before next value:
  83.           delay(100);
  84.         }//end for
  85.          
  86.    } else {
  87.      digitalWrite(ledPin, LOW); // otherwise turn it off
  88.    }
  89.    //*************************************************************************************************
  90.  
  91. //*********************************** Sec2 **************************************************************
  92.    if (val == '2')
  93.    { // If 1 was received
  94.      digitalWrite(ledPin, HIGH); // turn the LED on
  95.      
  96.      //volver
  97.        for (int numberToDisplay = 8; numberToDisplay > -1; numberToDisplay--) {
  98.           data = Sec2[numberToDisplay];
  99.           // the LEDs don't change while you're sending in bits:
  100.           digitalWrite(latchPin, LOW);
  101.           // shift out the bits:
  102.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  103.           //take the latch pin high so the LEDs will light up:
  104.           digitalWrite(latchPin, HIGH);
  105.           // pause before next value:
  106.           delay(100);
  107.         }//end for    
  108.        
  109.    } else {
  110.      digitalWrite(ledPin, LOW); // otherwise turn it off
  111.    }
  112.    //*************************************************************************************************
  113.  
  114.   //*********************************** Sec3 **************************************************************
  115.    if (val == '3')
  116.    {
  117.      
  118.      digitalWrite(ledPin, LOW);
  119.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  120.      
  121.        for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
  122.          
  123.           data = Sec3[numberToDisplay];
  124.           // the LEDs don't change while you're sending in bits:
  125.           digitalWrite(latchPin, LOW);
  126.           // shift out the bits:
  127.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  128.          
  129.           shiftOut(dataPin, clockPin, MSBFIRST, data);
  130.          
  131.           //take the latch pin high so the LEDs will light up:
  132.           digitalWrite(latchPin, HIGH);
  133.           // pause before next value:
  134.           delay(200);
  135.         }//end for
  136.         //delay(2000);
  137.    
  138.    } else {
  139.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  140.    }
  141.    //*************************************************************************************************
  142.  
  143.    //*********************************** Sec4 **************************************************************
  144.    if (val == '4')
  145.    { // If 1 was received
  146.      digitalWrite(ledPin, HIGH); // turn the LED on
  147.      
  148.      //volver
  149.        for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
  150.           data = Sec4[numberToDisplay];
  151.           // the LEDs don't change while you're sending in bits:
  152.           digitalWrite(latchPin, LOW);
  153.           // shift out the bits:
  154.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  155.           //take the latch pin high so the LEDs will light up:
  156.           digitalWrite(latchPin, HIGH);
  157.           // pause before next value:
  158.           delay(300);
  159.         }//end for
  160.         for (int numberToDisplay = 5; numberToDisplay > -1; numberToDisplay--) {
  161.           data = Sec4[numberToDisplay];
  162.           // the LEDs don't change while you're sending in bits:
  163.           digitalWrite(latchPin, LOW);
  164.           // shift out the bits:
  165.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  166.           //take the latch pin high so the LEDs will light up:
  167.           digitalWrite(latchPin, HIGH);
  168.           // pause before next value:
  169.           delay(300);
  170.         }//end for
  171.        
  172.    } else {
  173.      digitalWrite(ledPin, LOW); // otherwise turn it off
  174.    }
  175.    //*************************************************************************************************
  176.  
  177. //*********************************** Sec5 **************************************************************
  178.    if (val == '5')
  179.    { // If 1 was received
  180.      digitalWrite(ledPin, HIGH); // turn the LED on
  181.      
  182.      //volver
  183.        for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
  184.           data = Sec5[numberToDisplay];
  185.           // the LEDs don't change while you're sending in bits:
  186.           digitalWrite(latchPin, LOW);
  187.           // shift out the bits:
  188.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  189.           //take the latch pin high so the LEDs will light up:
  190.           digitalWrite(latchPin, HIGH);
  191.           // pause before next value:
  192.           delay(300);
  193.         }//end for
  194.        
  195.    } else {
  196.      digitalWrite(ledPin, LOW); // otherwise turn it off
  197.    }
  198.    //*************************************************************************************************
  199.  
  200. //*********************************** Sec6 **************************************************************
  201.    if (val == '6')
  202.    { // If 1 was received
  203.      digitalWrite(ledPin, HIGH); // turn the LED on
  204.      
  205.      //volver
  206.        for (int numberToDisplay = 0; numberToDisplay < 8; numberToDisplay++) {
  207.           data = Sec6[numberToDisplay];
  208.           // the LEDs don't change while you're sending in bits:
  209.           digitalWrite(latchPin, LOW);
  210.           // shift out the bits:
  211.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  212.           //take the latch pin high so the LEDs will light up:
  213.           digitalWrite(latchPin, HIGH);
  214.           // pause before next value:
  215.           delay(300);
  216.         }//end for
  217.        
  218.    } else {
  219.      digitalWrite(ledPin, LOW); // otherwise turn it off
  220.    }
  221.    //*************************************************************************************************
  222.  
  223. //*********************************** Sec7 **************************************************************
  224.    if (val == '7')
  225.    { // If 1 was received
  226.      digitalWrite(ledPin, HIGH); // turn the LED on
  227.      
  228.      //volver
  229.        for (int numberToDisplay = 0; numberToDisplay < 10; numberToDisplay++) {
  230.           data = Sec7[numberToDisplay];
  231.           // the LEDs don't change while you're sending in bits:
  232.           digitalWrite(latchPin, LOW);
  233.           // shift out the bits:
  234.           shiftOut(dataPin, clockPin, MSBFIRST, data);  
  235.           //take the latch pin high so the LEDs will light up:
  236.           digitalWrite(latchPin, HIGH);
  237.           // pause before next value:
  238.           delay(300);
  239.         }//end for
  240.        
  241.    } else {
  242.      digitalWrite(ledPin, LOW); // otherwise turn it off
  243.    }
  244.    //*************************************************************************************************
  245.  
  246.    //************************************ SEC8 *******************************
  247.    if (val == '8')
  248.    { // If 1 was received
  249.      digitalWrite(ledPin, HIGH); // turn the LED on
  250.      
  251.         for (int j = 0; j < 2; j++) {
  252.           //load the light sequence you want from array
  253.           //data = Blink[j];
  254.           //ground latchPin and hold low for as long as you are transmitting
  255.           digitalWrite(latchPin, LOW);
  256.          
  257.           //move 'em out
  258.           // prender todos
  259.          
  260.           if(j==0){
  261.        
  262.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[1]);
  263.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[2]);    
  264.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[3]);
  265.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[4]);  
  266.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[5]);      
  267.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[6]);
  268.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[7]);  
  269.            shiftOut(dataPin, clockPin, MSBFIRST, Sec8[8]);  
  270.          
  271.           }
  272.          
  273.           //no prender
  274.           if(j==1){  
  275.            shiftOut(dataPin, clockPin, MSBFIRST, 0);
  276.           }    
  277.          
  278.           //return the latch pin high to signal chip that it
  279.           //no longer needs to listen for information
  280.           digitalWrite(latchPin, HIGH);
  281.           delay(300);
  282.         }
  283.      
  284.    } else {
  285.      digitalWrite(ledPin, LOW); // otherwise turn it off
  286.    }
  287.    //*************************************************************************************************
  288.  
  289.  /*
  290.    
  291.    //****************************CAOS STORM - RANDOM *********************************************************************
  292.    if (val == '3')
  293.    {
  294.      
  295.      digitalWrite(ledPin, LOW);
  296.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  297.      
  298.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  299.          
  300.           //data = Caos[random(9)];
  301.           // the LEDs don't change while you're sending in bits:
  302.           digitalWrite(latchPin, LOW);
  303.           // shift out the bits:
  304.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  305.          
  306.           shiftOut(dataPin, clockPin, MSBFIRST, random(255));
  307.      
  308.  
  309.           //take the latch pin high so the LEDs will light up:
  310.           digitalWrite(latchPin, HIGH);
  311.           // pause before next value:
  312.           delay(200);
  313.         }//end for
  314.    
  315.    } else {
  316.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  317.    }
  318.    //*************************************************************************************************
  319.  
  320.    //**************************** prender de dos *********************************************************************
  321.    if (val == '4')
  322.    {
  323.      
  324.      digitalWrite(ledPin, LOW);
  325.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  326.      
  327.        for (int numberToDisplay = 0; numberToDisplay < 4; numberToDisplay++) {
  328.          
  329.           data = Par[numberToDisplay];
  330.           // the LEDs don't change while you're sending in bits:
  331.           digitalWrite(latchPin, LOW);
  332.           // shift out the bits:
  333.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  334.          
  335.           shiftOut(dataPin, clockPin, MSBFIRST, data);
  336.      
  337.  
  338.           //take the latch pin high so the LEDs will light up:
  339.           digitalWrite(latchPin, HIGH);
  340.           // pause before next value:
  341.           delay(200);
  342.         }//end for
  343.         //delay(2000);
  344.    
  345.    } else {
  346.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  347.    }
  348.    //*************************************************************************************************
  349.  
  350.    //**************************** secuencia depredador *********************************************************************
  351.    if (val == '5')
  352.    {
  353.      
  354.      digitalWrite(ledPin, LOW);
  355.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  356.    
  357.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  358.          
  359.           data = Depredador[numberToDisplay];
  360.           // the LEDs don't change while you're sending in bits:
  361.           digitalWrite(latchPin, LOW);
  362.           // shift out the bits:
  363.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  364.          
  365.          
  366.             shiftOut(dataPin, clockPin, MSBFIRST, data);
  367.  
  368.          // shiftOut(dataPin, clockPin, MSBFIRST, data);
  369.      
  370.  
  371.           //take the latch pin high so the LEDs will light up:
  372.           digitalWrite(latchPin, HIGH);
  373.           // pause before next value:
  374.           delay(800);
  375.         }//end for
  376.         delay(2000);
  377.    
  378.    } else {
  379.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  380.    }
  381.    //*************************************************************************************************
  382.  
  383.  
  384.  
  385.  //**************************** bomba *********************************************************************
  386.    if (val == '6')
  387.    {
  388.      
  389.      digitalWrite(ledPin, LOW);
  390.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  391.    
  392.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  393.          
  394.           data = Bomba[numberToDisplay];
  395.           // the LEDs don't change while you're sending in bits:
  396.           digitalWrite(latchPin, LOW);
  397.           // shift out the bits:
  398.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  399.          
  400.          
  401.             shiftOut(dataPin, clockPin, MSBFIRST, data);
  402.  
  403.          // shiftOut(dataPin, clockPin, MSBFIRST, data);
  404.      
  405.  
  406.           //take the latch pin high so the LEDs will light up:
  407.           digitalWrite(latchPin, HIGH);
  408.           // pause before next value:
  409.           delay(600);
  410.         }//end for
  411.         delay(2000);
  412.    
  413.    } else {
  414.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  415.    }
  416.    //*************************************************************************************************
  417.  
  418.  //**************************** laser *********************************************************************
  419.    if (val == '7')
  420.    {
  421.      
  422.      digitalWrite(ledPin, LOW);
  423.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  424.    
  425.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  426.          
  427.           data = Laser[numberToDisplay];
  428.           // the LEDs don't change while you're sending in bits:
  429.           digitalWrite(latchPin, LOW);
  430.           // shift out the bits:
  431.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  432.          
  433.          
  434.             shiftOut(dataPin, clockPin, MSBFIRST, data);
  435.  
  436.          // shiftOut(dataPin, clockPin, MSBFIRST, data);
  437.      
  438.  
  439.           //take the latch pin high so the LEDs will light up:
  440.           digitalWrite(latchPin, HIGH);
  441.          
  442.           if(numberToDisplay==1){delay(1000);}//simular recarga
  443.           //if(numberToDisplay==2){delay(1000);}
  444.           // pause before next value:
  445.           delay(10);
  446.         }//end for
  447.        
  448.    
  449.    } else {
  450.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  451.    }
  452.    //*************************************************************************************************
  453.    //****************************fast RANDOM disco*********************************************************************
  454.    if (val == '9')
  455.    {
  456.      
  457.      digitalWrite(ledPin, LOW);
  458.      digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
  459.      
  460.        for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
  461.          
  462.           //data = Caos[random(9)];
  463.           // the LEDs don't change while you're sending in bits:
  464.           digitalWrite(latchPin, LOW);
  465.           // shift out the bits:
  466.           //shiftOut(dataPin, clockPin, MSBFIRST, 0);
  467.          
  468.           shiftOut(dataPin, clockPin, MSBFIRST, random(255));
  469.      
  470.  
  471.           //take the latch pin high so the LEDs will light up:
  472.           digitalWrite(latchPin, HIGH);
  473.           // pause before next value:
  474.           delay(50);
  475.         }//end for
  476.    
  477.    } else {
  478.     // digitalWrite(ledPin, LOW); // otherwise turn it off
  479.    }
  480.    //*************************************************************************************************
  481.  */
  482.  
  483.  
  484.    delay(10); // Wait 10 milliseconds for next reading
  485.  
  486.    
  487.    
  488. }