Tfan
Published © GPL3+

Bluetooth + Arduino LED control pad with Processing

After my previous project I thought it would be great if we can control it with blue tooth and I just happen to have a spare HC-05 module

BeginnerFull instructions provided1 hour2,048
Bluetooth + Arduino LED control pad with Processing

Things used in this project

Hardware components

Seeeduino Lite
Seeed Studio Seeeduino Lite
Any Arduino compatible board is OK
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1

Software apps and online services

Processing
The Processing Foundation Processing
Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino code

Arduino
#include <SoftwareSerial.h>
//Using softwareSerial as the hardware one cause BT port being busy when connected to processing
SoftwareSerial BTSerial(10, 11); // RX | TX

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

void setup() {
  BTSerial.begin(115200);

  //Comment out below if not printing to arduino serial monitor
  //Serial.begin(115200); // opens serial port, sets data rate to 9600 bps
   
  //while (!Serial) {
    //; // wait for serial port to connect. Needed for Native USB only
  //}
  
  pinMode(LED_BUILTIN, OUTPUT);
}

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

    // print string to Arduino software monitor
    //Serial.print("String received: ");
    //Serial.println(incomingString);

    // convert the incoming to action code
    // all incoming is hard coded in processing
    action = incomingString.toInt();
        
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
import processing.serial.*;

import controlP5.*;

PFont pfont;

ControlP5 cp5;

Serial myPort;

int myColor = color(0);


void setup() {
  size(1024,768);
  
  printArray(Serial.list());
  
  //Confirm the BT COM port and change below line
  myPort=new Serial(this, "COM17",115200);
  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
5 projects • 2 followers
IT Engineer, interested in 3D printing, Arduino, RaspberryPi and etc
Contact

Comments

Please log in or sign up to comment.