jueves, 20 de marzo de 2014

Laboratorio 6

 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

Elementos Utilizados

*8 Led's
*8 Resistencias 
*1 Arduino UNO
*Protoboard
*Pelacable*Cables
*Cable USB
*IC 74HC595

Diagrama del montaje en la Protoboard















Diagrama Esquemático del Circuito








Fotos del Montaje
































Vídeo Funcionamiento

















Codigo Fuente Arduino

  1. #define PIN 3
  2. const int Latch = 8;
  3. const int Clock = 9;
  4. const int Data = 7;
  5. int Pins[PIN] = {
  6.   7,8,9};
  7. int Lect = 0;
  8. boolean OnLed = false;
  9. int Dato = 0;
  10. int i = 0;
  11. int Led[]={1,2,4,8,16,32,64,128};
  12. void setup() {
  13.   for (int j=0; j<PIN; j++){
  14.     pinMode(Pins[j], OUTPUT);
  15.   }
  16.   Serial.begin(9600);
  17. }
  18. void loop()
  19. {
  20.   if (Serial.available()>0) {
  21.    
  22.     Lect = Serial.read();  //Leemos el Dato
  23.    
  24.    
  25.     if (OnLed)
  26.     {
  27.       Suma(Lect);
  28.       On(Dato);
  29.       OnLed = false;
  30.     }
  31.     else
  32.     {
  33.      
  34.       i = Lect;
  35.       OnLed = true;
  36.     }
  37.   }
  38. }
  39. void Suma(int estadoLED){
  40.   if(estadoLED==0)
  41.   {
  42.     Dato = Dato-Led[i];
  43.   }else{
  44.     Dato = Dato+Led[i];
  45.   }  
  46. }
  47. void On(int Valor)
  48. {
  49.   digitalWrite(Latch, LOW);
  50.   shiftOut(Data, Clock, MSBFIRST, Valor);
  51.   digitalWrite(Latch, HIGH);
  52. }

Codigo Proccesing

  1. import controlP5.*;
  2. import processing.serial.*;
  3. ControlP5 cP5;
  4. Serial serial;
  5. int[] led = new int[] {
  6.   0, 0, 0, 0, 0, 0, 0, 0
  7. };
  8. void setup() {
  9.   size(590, 250);  //Tamaño de la ventana
  10.   noStroke();
  11.   cP5 = new ControlP5(this);  //Crea el objeto ControlP5
  12.   // Crea el Knob del color Rojo
  13.   for (int i=0; i<led.length; i++)
  14.   {
  15.     cP5.addToggle("led"+i, 35+i*70, 140, 30, 30)
  16.       .setMode(ControlP5.SWITCH);
  17.   }
  18.   String puerto = Serial.list()[0];
  19.   serial = new Serial(this, puerto, 9600);
  20. }
  21. void draw() {
  22.   background(0xFF444444);
  23.   fill(led[0] == 0 ? 0xFF222222 : color(255, 255, 0));
  24.   ellipse(50, 100, 50, 50);
  25.   for (int i=1; i<4; i++) {
  26.     fill(led[i] == 0 ? 0xFF222222 : color(255, 0, 0));
  27.     ellipse(50+i*70, 100, 50, 50);
  28.   }
  29.   for (int i=4; i<led.length; i++) {
  30.     fill(led[i] == 0 ? 0xFF222222 : color(0, 255, 0));
  31.     ellipse(50+i*70, 100, 50, 50);
  32.   }
  33.    fill(255);
  34.    textFont(createFont("Gill Sans Ultra Bold", 50));
  35.    text("Enciende un LED", 40, 50);
  36.    fill(255);
  37.    textSize(25);
  38.    text("", 120, 230);
  39. }
  40. /
  41. void controlEvent(ControlEvent evento) {
  42.   String nombre = evento.getController().getName();
  43.   int valor = int(evento.getController().getValue());
  44.   // Guarda el valor de cada boton
  45.   for (int i=0; i<led.length; i++) {
  46.     if (nombre.equals("led"+i)) {
  47.       led[i] = valor;
  48.       serial.write(i);
  49.       serial.write(valor);
  50.       println("evento: " + i + " / valor: "+valor);
  51.     }
  52.   }
  53. }

No hay comentarios:

Publicar un comentario