Tfan
Created April 4, 2020 © GPL3+

Arduino LED control pad with Processing

A little test piece to create ON/OFF control pad with Processing to control something connected to an Arduino.

BeginnerFull instructions provided1 hour1
Arduino LED control pad with Processing

Things used in this project

Hardware components

Seeeduino Lite
Seeeduino Lite
Anything Arduino based board would work as long as they have serial link
×1

Software apps and online services

Processing
The Processing Foundation Processing
Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino side code

Arduino
String incomingString; // for incoming serial data
int action;

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingString = Serial.readString();

    // say what you got:
    Serial.print("String received: ");
    Serial.println(incomingString);

    if (incomingString == "ON"){
        action = 1;
    }
    
    else if (incomingString == "OFF"){
        action = 2;
    }
    else 
        action = 0;
        
switch (action) {
 
  case 1:
    //do something when var equals 1
    Serial.println("Turn ON LED");
    digitalWrite(LED_BUILTIN, HIGH);
    break;
  case 2:
    //do something when var equals 2
    Serial.println("Turn OFF LED");
    digitalWrite(LED_BUILTIN, LOW);
    break;
    
  default:
    // if nothing else matches, do the default
    // default is optional
    Serial.println("!! Not sure what you want");
    break;
}
  }
}

Processing code

Processing
1. Create On and Off button
2. Send a "String" over the serial link to Arduino when button is pressed
import processing.serial.*;

import controlP5.*;

PFont pfont;

ControlP5 cp5;

Serial myPort;

int myColor = color(0);


void setup() {
  size(1024,768);
  
  printArray(Serial.list());
  
  myPort=new Serial(this, "COM3",9600);
  cp5 = new ControlP5(this);
  
  // replace the default controlP5 button with an image.
  // button.setImages(defaultImage, rolloverImage, pressedImage);
  // use button.updateSize() to adjust the size of the button and 
  // resize to the dimensions of the defaultImage
  
  
  cp5.addButton("On")
     .setCaptionLabel("LED ON")
     .setPosition(175,275)
     .setSize(250,200)
     .updateSize();
     
   cp5.addButton("Off")
     .setCaptionLabel("LED OFF")
     .setPosition(475,275)
     .setSize(250,200)
     .updateSize();
     
   
     
   
     PFont pfont = createFont("Arial",20,true); // use true/false for smooth/no-smooth
     ControlFont font = new ControlFont(pfont,241);
     
       
     cp5.getController("On")
     .getCaptionLabel()
     .setFont(font)
     .toUpperCase(false)
     .setSize(38);
     
     cp5.getController("Off")
     .getCaptionLabel()
     .setFont(font)
     .toUpperCase(false)
     .setSize(38);
     
          
}

void draw() {
  background(myColor);
  text("LED Control Pad",175,120);
  textSize(42);
  
}

public void controlEvent(ControlEvent theEvent) {
  //println(theEvent.getController().getName());
  print(theEvent.getController().getLabel());
  println(" button has been pressed");
}

void On(){
  myPort.write("ON");
  println("LED ON");
}

void Off(){
  myPort.write("OFF");
  println("LED OFF");
}

Credits

Tfan

Tfan

1 project • 0 followers
IT Engineer, interested in 3D printing, Arduino, RaspberryPi and etc

Comments