13. Interface and application programming¶
individual assignment:
write an application that interfaces a user with an
input &/or output device that you made
group assignment:
compare as many tool options as possible
Creating the Interface¶
The board I will be using for this week will be the LED Board I created in the Output Devices week. Since I already used python to create a very simple GUI, I will use Processing instead as I am currently in the process of learning it. First, I searched how to read serial inside of Processing and I found a very useful guide from SparkFun that gives a simple tutorial on how to do so.
Simply, we needed to import the serial library and then declare and instantiate a Serial object in the setup method.
import processing.serial.*;
Serial myPort;
void setup() {
myPort = new Serial(this, "COM10", 115200)
}
To write to serial, I used the write() method on myPort and in this case, I needed to give the chip which LED I wanted to light up and the RGB value of it. For now, I simply just set each LED to red. To get all the values to the ATTiny412, I simply sent a string with the delimeter as the colon with all the information necessary:
LED Number:Red Value:Green Value:Blue Value
Each time I wrote to serial, I would use this command:
myPort.write("1:255:0:0"); //Set LED1 to Red
For getting serial input, a user by the name of Robin2 posted a very helpful guide on the Arduino Forums. Instead of reading serial as a String since it overloads the memory on the ATtiny412 chip, we parse the data as a character array and then use the string tokenizer on that. The following code below is what I used:
void loop() {
char str[32];
char* pch;
int LEDNum, red, green, blue;
if (Serial.available()) {
int availableBytes = Serial.available();
for (int i = 0; i < availableBytes; i++) {
str[i] = Serial.read();
}
pch = strtok(str, ":");
LEDNum = atoi(pch);
pch = strtok(NULL, ":");
red = atoi(pch);
pch = strtok(NULL, ":");
green = atoi(pch);
pch = strtok(NULL, ":");
blue = atoi(pch);
if (LEDNum == 100) {
strip.clear();
}
else {
strip.setPixelColor(LEDNum, strip.Color(red,green,blue));
}
strip.show();
}
delay(100);
}
The interface is just six squares that when the first five are clicked, will correspond to the correct LEDs. The last square when clicked will reset the LEDs to unlit. Here is a video of it working:
Furthermore, I went on to change the RGB values randomly on click and have the squares in the interface change to the same color. The code for that can be found in the download and here is a video of it working:
Here are the files for this week: download
Group Project¶
The group assignment can be found here.