Now Let's Talk!(2)
This chapter continues the assignment from last week where I prepared the bluetooth communication for the attiny44 on my board.Writing a Program which runs on your computer and communicates with your board.
This time, With the great help from Marcel, I first tried simple 'blinking app', which communicate
with my Attiny over Bluetooth network.
With the scope of final project, I would also be able to extend this with further control. e..g,
servo motor movements.
Using Processing
previous week, I told my pcb module to turn led on if it gets '1' and off if it gets '0' over
bluetooth network. This week I will programm a Processing
application.
In our application code yet, we will send '0' and '1' based on state change of a mouse click on an
image in the application which shows if the LED is on (happy emoji) or off (sleeping emoji).
The code below generates a simple application window with one image of a sleeping emoji
(LEDoffImage) which indicates that the LED on the board from Week15 is switched off. Once the user
clicks the image it switches to the happy emoji (LEFonImage) and sends a "1" character over the
bluetooth communication which triggers the attiny44 to switch the LED on the board on. Clicking the
image again switches back to the sleeping emoji (LEDoffImage) and sends a "0" character over the
bluetooth communication. This indicates the attiny44 to switch the LED on the board off.
Aside from simple setup method which creates the application window and loads images one important
part is the setup of the bluetooth connection. The section "browse available ports" browses through
the list of available serial modules. The list is provided by the serial library of
processing. The code looks for the string "/dev/cu.HC-06-DevB" in the list of available devices
which represents the HC-06 bluetooth module and saves the position in the portNumber
variable. The variable is later used to create a Serial object with the constructor for that
class. The function write of the Serial class can then be used to send characters over
the serial communication using the bluetooth module.
The mousePressed() function
of the processing Library gives ability to use the mouseclick on image to trigger the change of
image and sending of the characters "0" or "1" over bluetooth connection.
//import library for serial comunication
import processing.serial.*;
boolean toggle = true; // A boolean to switch between the mode of sending a 1 or a 0
PImage LEDonImage;
PImage LEDoffImage; //Two objects which will contain images to represent if the controlled LED on our PCB is switched on or off
int width = 800; //App window height
int height = 600; //App window width
Serial myPort; //An object which will contain the port on which we will establish our bluetooth connection
int portNumber; // A variable which will contain the number of the port on which we will establich our bluetooth connection
void setup() {
//size(width, height);
size(800,600);
//load and resize image
LEDonImage = loadImage("LEDon.PNG");
LEDoffImage = loadImage("LEDoff.PNG"); //in default images should be in the same folder with .pde file
LEDonImage.resize(width, height);
LEDoffImage.resize(width, height);
//browse available ports
for (int count = 0; count < Serial.list().length; count++)
{
if (Serial.list()[count].length() > 17)
{
//look at which port we can find the device.
if (Serial.list()[count].substring(0, 18).equals("/dev/cu.HC-06-DevB"))
{
portNumber = count;
}
}
}
//print portlist and number
println(Serial.list());
println(portNumber);
String portName = Serial.list()[portNumber];
myPort = new Serial(this, portName, 9600);
myPort.write("0");
}
//chaning grpahics, processing mouse eventrs
void draw() {
}
//handle mouse events
void mousePressed() {
if (toggle)
{
myPort.write("0");
image(LEDoffImage, 0, 0);
} else
{
myPort.write("1");
image(LEDonImage, 0, 0);
}
toggle = !toggle;
}
Below are results from the assignment.