sábado, 12 de noviembre de 2016

LABORATORIO #9

DESCRIPCIÓN

Controlar desde una interfaz en Processing, una línea de 8 LEDs RGB 5050 conectado a un Arduino. Nota: No alimente los +5V desde el Arduino.

MATERIALES DEL LABORATORIO 

Arduino
Protoboard.
Resistencias de 220 Ohmios.
Led RGB.
Kit de cables para arduino.


MATERIALES DEL LABORATORIO 

Computador con el IDE de Arduino y el driver de nuestra placa
- Arduino Uno
- Protoboard
- Línea LED RGB5050 CJMCU-2812-8
- Cables Macho - Macho
- Diagrama Arduino Uno

    DIAGRAMAS

    PROTOBOARD

    FOTOS MATERIALES Y MONTAJE










    CÓDIGO

    ARDUINO

    1.   #define TOTAL 8
    2. int R = 9;
    3. int G = 10;
    4. int B = 11;
    5. int led[TOTAL] = {2, 3, 4, 5, 6, 7, 8, 13};
    6. int pos = 0;
    7. int color = 0;
    8. int estado = 0;
    9. String str = "";
    10. void setup() {
    11.   Serial.begin(9600);
    12.   for (int i = 0; i < TOTAL; i++) {
    13.     pinMode(led[i], OUTPUT);
    14.     digitalWrite(led[i], HIGH);
    15.   }
    16.   pinMode(R, OUTPUT);
    17.   digitalWrite(R, HIGH);
    18.   pinMode(G, OUTPUT);
    19.   digitalWrite(G, HIGH);
    20.   pinMode(B, OUTPUT);
    21.   digitalWrite(B, HIGH);
    22. }
    23. void loop() {
    24.   leerSerial();
    25.   if (estado)
    26.     encender(pos, color);
    27.   else
    28.     apagar(pos, color);
    29.   delay(100);
    30. }
    31. void leerSerial() {
    32.   if (Serial.available() > 0) {
    33.     str = Serial.readString();
    34.     //    Serial.println(str[0]); // RGB
    35.     //    Serial.println(str[1]); // led 0-7
    36.     //    Serial.println(str[2]); // 0=off 1=on
    37.     switch (str[0]) {
    38.       case 'R': color = R; break;;
    39.       case 'G': color = G; break;;
    40.       case 'B': color = B; break;;
    41.     }
    42.     pos = led[ int(str[1] - '0') ];
    43.     estado = int(str[2] - '0');
    44.   }
    45. }
    46. void encender (int led, int c) {
    47.   digitalWrite(led, LOW);
    48.   digitalWrite(c, LOW);
    49. }
    50. void apagar(int led, int c) {
    51.   digitalWrite(led, HIGH);
    52.   digitalWrite(c, HIGH);
    53. }

    PROCESSING


      1. // utilizar la libreria ControlP5
      2. import controlP5.*;
      3. import processing.serial.*;
      4. // definir la variable cp5 del tipo ControlP5
      5. ControlP5 cp5;
      6. Serial serial;
      7. // como deseamos controlar 8 LEDs, usamos un arreglo
      8. int[] led = new int[] {
      9.   0, 0, 0, 0, 0, 0, 0, 0
      10. };  
      11. String[] nm = new String[] {
      12.   "R", "G", "B"
      13. };
      14. // configuración inicial
      15. void setup() {
      16.   size(750, 140); // tamaño de la ventana
      17.   noStroke();     // no dibujar el border de los circulos
      18.   // crear el objeto ControlP5
      19.   cp5 = new ControlP5(this);
      20.   // crear un botón para encender/apagar cada LED
      21.   for (int i=0; i<8; i++){
      22.     for (int l=0; l<3; l++){
      23.     if(nm[l]=="R"){
      24.       cp5.addToggle(nm[l]+i, 30+l*20+i*90, 30, 20, 40)
      25.           .setColorBackground(color(100,0,0))
      26.           .setColorForeground(color(150,0,0))
      27.           .setColorActive(color(255,0,0));
      28.     }
      29.    if(nm[l]=="G"){
      30.       cp5.addToggle(nm[l]+i, 30+l*20+i*90, 30, 20, 40)
      31.           .setColorBackground(color(0,100,0))
      32.           .setColorForeground(color(0,150,0))
      33.           .setColorActive(color(0,255,0));
      34.     }
      35.    if(nm[l]=="B"){
      36.       cp5.addToggle(nm[l]+i, 30+l*20+i*90, 30, 20, 40)
      37.           .setColorBackground(color(0,0,100))
      38.           .setColorForeground(color(0,0,150))
      39.           .setColorActive(color(0,0,255));
      40.     }
      41.     }
      42.   }
      43.   serial = new Serial(this, Serial.list()[1], 9600);
      44. }
      45. // dibujar cada frame
      46. void draw()  
      47. {
      48.   background(0xFF444444);  // color gris del fondo
      49. }  
      50. // actuar cuando ocurra un evento con los Sliders
      51. public void controlEvent(ControlEvent theEvent) {
      52.   // guardar el nombre y valor del evento
      53.   String nombre = theEvent.getController().getName();
      54.   int valor = int(theEvent.getController().getValue());
      55.   println("evento: " + nombre + " / valor: "+valor);
      56.   //serial.write(nombre+valor);
      57.   println(nombre+valor);
      58.   serial.write(nombre+valor);
      59. }

    No hay comentarios.:

    Publicar un comentario