jueves, 24 de septiembre de 2015

LABORATORIO # 4

DESCRIPCIÓN DEL LABORATORIO: 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



MATERIALES:



- Protoboard
- Arduino Uno
- 8 Resistencias
- 8 Leds
- Cable UTP
- IC 74HC595
- Cables


DIAGRAMAS:
MONTAJE PROTOBOARD:



ESQUEMATICO:






FOTOS DEL MONTAJE:












VIDEO:







CODIGO ARDUINO:


Data hosted with ♥ by Pastebin.com - Download Raw - See Origin

  1. // Pin conectado a ST_CP del 74HC595
  2. //Pin connected to ST_CP of 74HC595
  3. int latchPin = 8;
  4.  
  5. // Pin conectado a SH_CP del 74HC595
  6. //Pin connected to SH_CP of 74HC595
  7. int clockPin = 12;
  8.  
  9. // Pin conectado a DS de 74HC595
  10. //Pin connected to DS of 74HC595
  11. int dataPin = 11;
  12.  
  13.  
  14.  
  15. void setup() {
  16.     /*Se inicializa el puerto serial a 9600 baudio*/
  17.     /* The serial port is initialized at 9600 baud */
  18.     Serial.begin(9600);
  19.  
  20.  
  21.     /* Define the pins latchPin, clockPin and dataPin as OUTPUT pins */
  22.     /*Defino los pines latchPin, clockPin and dataPin como pines de SALIDA */
  23.     pinMode(latchPin, OUTPUT);
  24.     pinMode(clockPin, OUTPUT);
  25.     pinMode(dataPin, OUTPUT);
  26. }
  27.  
  28. void loop() {
  29.  
  30.     /*Evaluo si el puerto Serial tiene datos*/
  31.     /* Evaluated whether Serial port has data */
  32.    if (Serial.available()>0){
  33.         /*el dato obtenido del puerto Serial es obtenido como un entero y almacenado en la variable valor*/
  34.         /* Data from the Serial port obtained is obtained as an integer and stored in the variable value */
  35.         int valor = Serial.parseInt();
  36.         /*Se imprime  al monitor Serial el valor*/
  37.         /* The Serial monitor prints the value */
  38.         Serial.println(valor);
  39.         /* tomar el Pin Pestillo para que los led's no cambein mientras se manda el valor */
  40.     /*take the latchPin low so
  41.      the LEDs don't change while you're sending in bits:*/
  42.     digitalWrite(latchPin, LOW);
  43.     // shift out the bits:
  44.     // Cambiar los bits:
  45.     shiftOut(dataPin, clockPin, MSBFIRST, valor);  
  46.  
  47.     //take the latch pin high so the LEDs will light up:
  48.     // Cambia a la alta el pasador por lo que los LEDs se iluminarán:
  49.     digitalWrite(latchPin, HIGH);
  50.     // pause before next value:
  51.     delay(500);
  52.   }
  53. }




CODIGO PROCESSING:




Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. import controlP5.*;
  3. import processing.serial.*;
  4. ControlP5 cp5;
  5.  
  6. CheckBox checkbox;
  7. int []colores={0,0,0};
  8. int [] binar={128, 64, 32, 16, 8, 4, 2, 1};
  9. Serial serial;
  10. void setup() {
  11.   size(700, 400);
  12.   smooth();
  13.   cp5 = new ControlP5(this);
  14.   checkbox = cp5.addCheckBox("checkBox")
  15.                 .setPosition(100, 200)
  16.                 .setColorForeground(color(120))
  17.                 .setColorActive(color(255))
  18.                 .setColorLabel(color(255))
  19.                 .setSize(40, 40)
  20.                 .setItemsPerRow(4)
  21.                 .setSpacingColumn(30)
  22.                 .setSpacingRow(20)
  23.                 .addItem("LED 1",1)
  24.                 .addItem("LED 2",2)
  25.                 .addItem("LED 3",3)
  26.                 .addItem("LED 4",4)
  27.                 .addItem("LED 5",5)
  28.                 .addItem("LED 6",6)
  29.                 .addItem("LED 7",7)
  30.                 .addItem("LED 8",8)
  31.                 ;
  32.                
  33.             cp5.addButton("Encender")
  34.                .setPosition(100, 320)
  35.                .setSize(80,40)
  36.                ;
  37.   serial = new Serial(this, Serial.list()[0], 9600);
  38. }
  39. void draw() {
  40.   background(0);
  41.   pushMatrix();
  42.   translate(width/2 + 200, height/2);
  43.   stroke(255);
  44.   strokeWeight(2);
  45.   fill(colores[0],colores[1],colores[2]);
  46.   ellipse(0,0,200,200);
  47.   popMatrix();
  48. }
  49.  
  50. void controlEvent(ControlEvent theEvent) {
  51.   if (theEvent.isFrom(checkbox)) {  
  52.    
  53.   }else if(theEvent.getController().getName().equals("Encender")){
  54.      colores[0]=0;
  55.     colores[1]=0;
  56.     colores[2]=0;
  57.   int total=0;
  58.   for (int i=0;i<checkbox.getArrayValue().length;i++) {
  59.       int n = (int)checkbox.getArrayValue()[i];
  60.        if (n>0){
  61.          total+=binar[i];
  62.        }  
  63.     }
  64.     serial.write(""+total);
  65.     if (total==224){
  66.     colores[0]=255;
  67.     colores[1]=255;
  68.     colores[2]=0;
  69.     }
  70.     if (total==28){
  71.     colores[0]=0;
  72.     colores[1]=255;
  73.     colores[2]=0;
  74.     }
  75.     if (total==3){
  76.     colores[0]=255;
  77.     colores[1]=0;
  78.     colores[2]=0;
  79.     }
  80.   println(total);  
  81.   }
  82. }

No hay comentarios:

Publicar un comentario