Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /********** Proyecto **********
  2.  * Laboratorio 06: El laboratorio consiste en Controlar 8 LEDs
  3.  * desde el Arduino, a través de un IC 74HC595,
  4.  * definiendo mínimo 8 patrones de movimiento que son
  5.  * controlados desde una interfaz gráfica en Processing/ControlP5.
  6.  * Autor: Oscar E. Bustamante R.
  7.  * Version: 1
  8.  */
  9.  
  10. import controlP5.*;
  11. import processing.serial.*;
  12.  
  13. ControlP5 cp5;
  14.  
  15. int colorDeFondo = color (213, 213, 213);
  16. int val; // Data received from the serial port
  17.  
  18. RadioButton radioButton;
  19.  
  20. Serial serial;  // definir la variable serial del tipo Serial
  21. int lf = 10;      // ASCII linefeed
  22.  
  23.  
  24. void setup() {
  25.   size(400, 300);
  26.   noStroke();
  27.  
  28.   // Print a list of the serial ports, for debugging purposes:
  29.   println("Puertos usados:");
  30.   printArray(Serial.list());
  31.  
  32.   cp5 = new ControlP5(this);
  33.   radioButton = cp5.addRadioButton("radioButton")
  34.     .setPosition(20, 50)
  35.       .setSize(40, 20)
  36.         .setColorForeground(color(120))
  37.           .setColorActive(color(255))
  38.             .setColorLabel(color(0))
  39.               .setItemsPerRow(4)
  40.                 .setSpacingColumn(50)
  41.                   .setSpacingRow(20)
  42.                     .addItem("Patron 1", 1)
  43.                       .addItem("Patron 2", 2)
  44.                         .addItem("Patron 3", 3)
  45.                           .addItem("Patron 4", 4)
  46.                             .addItem("Patron 5", 5)
  47.                               .addItem("Patron 6", 6)
  48.                                 .addItem("Patron 7", 7)
  49.                                   .addItem("Patron 8", 8)
  50.                                     ;
  51.  
  52.   for (Toggle t : radioButton.getItems ()) {
  53.     //t.captionLabel().setColorBackground(color(255,80));
  54.     t.captionLabel().style().moveMargin(-7, 0, 0, -3);
  55.     t.captionLabel().style().movePadding(7, 0, 0, 3);
  56.     t.captionLabel().style().backgroundWidth = 45;
  57.     t.captionLabel().style().backgroundHeight = 13;
  58.   }
  59.  
  60.   /*
  61.       cp5.addButton("Enviar")
  62.    .setPosition(100, 320)
  63.    .setSize(80,40)
  64.    ;
  65.    */
  66.  
  67.   serial = new Serial(this, Serial.list()[2], 9600);
  68.   serial.bufferUntil(lf);
  69. }
  70.  
  71. void draw() {
  72.   background(colorDeFondo);
  73. }
  74.  
  75. //Majejando los radioButtons con el teclado
  76. void keyPressed() {
  77.   switch(key) {
  78.     case('0'):
  79.     radioButton.deactivateAll();
  80.     break;
  81.     case('1'):
  82.     radioButton.activate(0);
  83.     break;
  84.     case('2'):
  85.     radioButton.activate(1);
  86.     break;
  87.     case('3'):
  88.     radioButton.activate(2);
  89.     break;
  90.     case('4'):
  91.     radioButton.activate(3);
  92.     break;
  93.     case('5'):
  94.     radioButton.activate(4);
  95.     break;
  96.     case('6'):
  97.     radioButton.activate(5);
  98.     break;
  99.     case('7'):
  100.     radioButton.activate(6);
  101.     break;
  102.     case('8'):
  103.     radioButton.activate(7);
  104.     break;
  105.   }
  106. }
  107.  
  108.  
  109. void controlEvent(ControlEvent evento) {
  110.  
  111.   if (evento.isFrom(radioButton)) {
  112.  
  113.     int total = 0;
  114.  
  115.     total = (int)evento.getValue();
  116.  
  117.     println("\nValor a ser enviado por Serial = " + total);
  118.     //serial.write(total+"");
  119.    
  120.     serial.write("P"+total);
  121.    
  122.   }
  123.   //println(serial.readString());
  124. }