Este laboratorio consiste en controlar 8 LEDs desde el Arduino, a través de un 74HC545, definiendo mínimo 8 patrones de movimiento que son controlados desde una interfaz gráfica en Processing/ControlP5.
MATERIALES:
- Arduino
- 8 LEDs
- 8 resistencias
- 1 integrado 74HC545
- MONTAJE PASO A PASO:
- montamos los LEDs en la protoboard
- DIAGRAMAS (USANDO FRITZING)
- En protoboard
- Esquemático
- VIDEO DEL FUNCIONAMIENTO:
- CÓDIGO FUENTE EN ARDUINO:
1: /** Laboratorio 6: controlar 8 LEDS desde Processing/ControlP5 2: * utilizando un 74HC525 3: * Autor: FANOR STIWAR ZAPATA 4: */ 5: 6: // definimos la cantidad de pines que vamos a usar como PIN // 7: #define PIN 3 8: 9: // se declaran los pines que utilizaremos 10: // que van a ser usados por el integrado respectivamente 11: 12: const int Latch = 8; 13: const int Clock = 9; //el pin SH_CP osea Clock debe ser PWM(~) // 14: const int Data = 7; 15: 16: int Pins[PIN] = {Data,Latch,Clock}; 17: 18: // Inicializamos las variables que vamos a utilizar 19: int Lect = 0; 20: boolean OnLed = false; 21: int Dato = 0; 22: int i = 0; 23: int Led[]={1,2,4,8,16,32,64,128}; 24: 25: // Ciclo para activar los tres pines como salida 26: void setup() { 27: for (int j=0; j<PIN; j++){ 28: pinMode(Pins[j], OUTPUT); 29: } 30: //Comunicación serial a 9600bps 31: Serial.begin(9600); 32: } 33: 34: // Recibe la información de manera serial del processing 35: void loop() 36: { 37: if (Serial.available()>0) { 38: 39: Lect = Serial.read(); //Leemos el Dato 40: 41: //Se ingresa cuando OnLed es verdadero 42: //osea cuando algun Led esta en On "1" 43: if (OnLed) 44: { 45: Suma(Lect); 46: On(Dato); 47: OnLed = false; 48: } 49: else 50: { 51: //Se recibe la info si el Led esta On u Off "1" o "0" 52: i = Lect; 53: OnLed = true; 54: } 55: } 56: } 57: 58: // Le va adicionando o sustraendo al Dato el valor del 59: // Led encendido o apagado 60: void Suma(int estadoLED){ 61: if(estadoLED==0) 62: { 63: Dato = Dato-Led[i]; 64: }else{ 65: Dato = Dato+Led[i]; 66: } 67: } 68: 69: // Función para enviar los datos al Integrado IC 74HC595 70: // y se usa shiftOut para que el valor se lea en los ocho Leds 71: void On(int Valor) 72: { 73: digitalWrite(Latch, LOW); 74: shiftOut(Data, Clock, MSBFIRST, Valor); 75: digitalWrite(Latch, HIGH); 76: }
- CÓDIGO FUENTE EN PROCESSING:
1: /** Laboratorio 6: controlar 8 LEDS desde Processing/ControlP5
2: * utilizando un 74HC525
3: * Autor: fanor stiwar zapata
4: */
5:
6: // Se utiliza las librerias ControlP5 y Serial del Processing
7: import controlP5.*;
8: import processing.serial.*;
9:
10: // Se define la variable cP5 del tipo ControlP5
11: ControlP5 cP5;
12:
13: // Se le da nombres al Serial
14: Serial serial;
15:
16: int[] led = new int[] {
17: 0, 0, 0, 0, 0, 0, 0, 0
18: };
19:
20: // Configuración inicial
21: void setup() {
22: size(590, 250); //Tamaño de la ventana
23: noStroke();
24:
25: cP5 = new ControlP5(this); //Crea el objeto ControlP5
26:
27: // Crea el Knob del color Rojo
28: for (int i=0; i<led.length; i++)
29: {
30: cP5.addToggle("led"+i, 35+i*70, 140, 30, 30)
31: .setMode(ControlP5.SWITCH);
32: }
33:
34: String puerto = Serial.list()[0];
35: //Comunicación serial a 9600bps
36: serial = new Serial(this, puerto, 9600);
37: }
38:
39: // Cada uno de los circuilos hecho toma el color determinado
40: // del Led que se tiene en la protoboard
41: void draw() {
42: background(0xFF444444);
43: fill(led[0] == 0 ? 0xFF222222 : color(255, 255, 0));
44: ellipse(50, 100, 50, 50);
45: for (int i=1; i<4; i++) {
46: fill(led[i] == 0 ? 0xFF222222 : color(255, 0, 0));
47: ellipse(50+i*70, 100, 50, 50);
48: }
49: for (int i=4; i<led.length; i++) {
50: fill(led[i] == 0 ? 0xFF222222 : color(0, 255, 0));
51: ellipse(50+i*70, 100, 50, 50);
52: }
53: fill(255);
54: textFont(createFont("Gill Sans Ultra Bold", 50));
55: text("Enciende un LED", 40, 50);
56: fill(255);
57: textSize(25);
58: text("Juan Camilo Fernández López", 120, 230);
59: }
60:
61: // Como se va a actuar cuando ocurra un evento con los botones
62: void controlEvent(ControlEvent evento) {
63: String nombre = evento.getController().getName();
64: int valor = int(evento.getController().getValue());
65:
66: // Guarda el valor de cada boton
67: for (int i=0; i<led.length; i++) {
68: if (nombre.equals("led"+i)) {
69: led[i] = valor;
70: serial.write(i);
71: serial.write(valor);
72: println("evento: " + i + " / valor: "+valor);
73: }
74: }
75: }