domingo, 30 de mayo de 2021

Arduino y Processing - Encender y apagar un LED

 Diagrama de conexión


Código en Arduino

  1. const int LED = 5;
  2. char dato;
  3. void setup() {
  4. Serial.begin(9600);
  5. pinMode(LED, OUTPUT);
  6. digitalWrite(LED, LOW);
  7. }
  8. void loop() {
  9. if (Serial.available() > 0)
  10. {
  11. dato = Serial.read();
  12. if (dato == 'E') {
  13. digitalWrite(LED, HIGH);
  14. } else {
  15. digitalWrite(LED, LOW);
  16. }
  17. }
  18. }

Código en Processing

  1. import processing.serial.*;
  2. Serial port;
  3. String TITULO="Control de LED - Arduino";
  4. PFont font;
  5. int qx=150;
  6. int qy=150;
  7. int qr=50;
  8. boolean state = false;
  9. String texto;
  10. void setup()
  11. {
  12. //println(Serial.list());
  13. port = new Serial(this, Serial.list()[0], 9600);
  14. size(300, 300);
  15. font = createFont("Arial", 20, true);
  16. frameRate(10);
  17. }
  18. void draw()
  19. {
  20. background(220, 220, 220);
  21. textFont(font, 20);
  22. fill(0);
  23. textAlign(CENTER);
  24. text(TITULO, width/2, 60);
  25. fill(0);
  26. stroke(0, 255, 0);
  27. if (mouseX > qx-qr && mouseX < qx+qr &&
  28. mouseY > qy-qr && mouseY < qy+qr && mousePressed) {
  29. state=!state;
  30. }
  31. noStroke();
  32. if (state) {
  33. fill(0, 0, 255);
  34. texto="ON";
  35. port.write('E');
  36. } else {
  37. fill(255, 0, 0);
  38. texto="OFF";
  39. port.write('A');
  40. }
  41. rectMode(RADIUS);
  42. rect(qx, qy, qr, qr);
  43. fill(0);
  44. text(texto, 150, 150);
  45. }

Interfaz gráfica




No hay comentarios:

Publicar un comentario