Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********** Arduino **********
- * Laboratorio 04: 8 LEDs desde el Arduino, a través de un IC 74HC595
- * Autor: Cristian Augusto Restrepo
- * Descripcion:
- * Controlar 8 LEDs desde el Arduino, a través de un IC 74HC595,
- * definiendo mínimo 8 patrones de movimiento que son controlados desde una interfaz gráfica
- * en Processing/ControlP5. Más información sobre el IC 74HC595
- char val; // Data received from the serial port
- int ledPin = 13; // Set the pin to digital I/O 13
- //Pin connected to ST_CP of 74HC595
- int latchPin = 8;
- //Pin connected to SH_CP of 74HC595
- int clockPin = 12;
- ////Pin connected to DS of 74HC595
- int dataPin = 11;
- byte data;
- byte dataArray[10];
- //movimientos
- int Sec1[9] = {0,1, 2,4, 8,16, 32,64, 128}; //UNO
- int Sec2[9] = {0,1, 2,4, 8,16, 32,64, 128}; //DOS
- int Sec3[5] = {0,3 ,15,63, 255}; //TRES
- int Sec4[5] = {0,3 ,51,63, 255}; //CUATRO
- int Sec5[5] = {0,129 ,195,231, 255}; //CINCO
- int Sec6[8] = {0,24, 36,66, 129,255, 0,255}; //SEIS
- int Sec7[10] = {0,170, 0,85, 0,255, 170,255, 85,255}; //SIETE
- int Sec8[9] = {0,1, 3,7, 15,31, 63,127, 255}; //OCHO
- //int Caos[9] = {}; //en desorden// usamos en shift el random 255
- //int Mitad[5] = { 0, 15, 255};
- //int Depredador[9] = { 255,63,7,31,51,7,1,3,0};
- //int Bomba[9] = { 255,127,63,31,15,7,3,1,0};
- //int Laser[9] = {0,1, 2,4,8,16,32,64,128};
- void setup()
- {
- //set pins to output so you can control the shift register
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
- //initialize serial communications at a 9600 baud rate
- Serial.begin(9600);
- }
- void loop() {
- if (Serial.available())
- { // If data is available to read,
- val = Serial.read(); // read it and store it in val
- Serial.println(val);
- }
- //*********************************** Sec1 **************************************************************
- if (val == '1')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //ir
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- data = Sec2[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(100);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec2 **************************************************************
- if (val == '2')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //volver
- for (int numberToDisplay = 8; numberToDisplay > -1; numberToDisplay--) {
- data = Sec2[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(100);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec3 **************************************************************
- if (val == '3')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
- data = Sec3[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(200);
- }//end for
- //delay(2000);
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec4 **************************************************************
- if (val == '4')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //volver
- for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
- data = Sec4[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(300);
- }//end for
- for (int numberToDisplay = 5; numberToDisplay > -1; numberToDisplay--) {
- data = Sec4[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(300);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec5 **************************************************************
- if (val == '5')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //volver
- for (int numberToDisplay = 0; numberToDisplay < 5; numberToDisplay++) {
- data = Sec5[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(300);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec6 **************************************************************
- if (val == '6')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //volver
- for (int numberToDisplay = 0; numberToDisplay < 8; numberToDisplay++) {
- data = Sec6[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(300);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //*********************************** Sec7 **************************************************************
- if (val == '7')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- //volver
- for (int numberToDisplay = 0; numberToDisplay < 10; numberToDisplay++) {
- data = Sec7[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(300);
- }//end for
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //************************************ SEC8 *******************************
- if (val == '8')
- { // If 1 was received
- digitalWrite(ledPin, HIGH); // turn the LED on
- for (int j = 0; j < 2; j++) {
- //load the light sequence you want from array
- //data = Blink[j];
- //ground latchPin and hold low for as long as you are transmitting
- digitalWrite(latchPin, LOW);
- //move 'em out
- // prender todos
- if(j==0){
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[1]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[2]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[3]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[4]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[5]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[6]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[7]);
- shiftOut(dataPin, clockPin, MSBFIRST, Sec8[8]);
- }
- //no prender
- if(j==1){
- shiftOut(dataPin, clockPin, MSBFIRST, 0);
- }
- //return the latch pin high to signal chip that it
- //no longer needs to listen for information
- digitalWrite(latchPin, HIGH);
- delay(300);
- }
- } else {
- digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- /*
- //****************************CAOS STORM - RANDOM *********************************************************************
- if (val == '3')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- //data = Caos[random(9)];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, random(255));
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(200);
- }//end for
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //**************************** prender de dos *********************************************************************
- if (val == '4')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 4; numberToDisplay++) {
- data = Par[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(200);
- }//end for
- //delay(2000);
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //**************************** secuencia depredador *********************************************************************
- if (val == '5')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- data = Depredador[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- // shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(800);
- }//end for
- delay(2000);
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //**************************** bomba *********************************************************************
- if (val == '6')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- data = Bomba[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- // shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(600);
- }//end for
- delay(2000);
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //**************************** laser *********************************************************************
- if (val == '7')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- data = Laser[numberToDisplay];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, data);
- // shiftOut(dataPin, clockPin, MSBFIRST, data);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- if(numberToDisplay==1){delay(1000);}//simular recarga
- //if(numberToDisplay==2){delay(1000);}
- // pause before next value:
- delay(10);
- }//end for
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- //****************************fast RANDOM disco*********************************************************************
- if (val == '9')
- {
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin, HIGH); // turn the LED on, prueba de comunicaci'on
- for (int numberToDisplay = 0; numberToDisplay < 9; numberToDisplay++) {
- //data = Caos[random(9)];
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- //shiftOut(dataPin, clockPin, MSBFIRST, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, random(255));
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(50);
- }//end for
- } else {
- // digitalWrite(ledPin, LOW); // otherwise turn it off
- }
- //*************************************************************************************************
- */
- delay(10); // Wait 10 milliseconds for next reading
- }
Advertisement
Add Comment
Please, Sign In to add comment