Interfacing and Applications Programming

Assignment

For our assignment, we have been tasked with writing an application that interfaces a user with an input/output device that we have made.

Since I have already made my Tiny85 board, I will be reusing it for this assignment.

This segment will primarily be a continuation from my previous programming segment

Now that I am able to send data through a software serial, I can now try writing an application to read the data sent and read it.

I'll be using Processing, it can be downloaded from https://processing.org/download/

To start, I'll modify my program that I made from the embedded programming segment.

I must admit that I don't fully understand the code that I wrote to create this janky imitation of Mr Chew's light bulb.

In this case, processing uses Java, which is rather similar to C

I primarily referred to the examples included with processing to write it as well as Mr Chew's examples shown.

In order to list the COM ports, use the printArray(Serial.list()); , it'll print an array showing the various COM ports in use, allowing you to check and choose which one to read serial data from.

Here it is:

        
          import processing.serial.*;

PImage ImageAlpha;
PImage ImageBravo;

Serial myPort;

int val;

void setup(){
  
  ImageAlpha = loadImage("lightbulboff.png");
  ImageBravo = loadImage("lightbulbon.png");
  
  
  //myPort = new Serial(this, Serial.list ()[1],9600);
  size (1280,780);
 image(ImageBravo, 0, 0);
 
  printArray(Serial.list());
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  
}


void draw(){
  
  
  println(val);
  if( myPort.available() > 0){
   val = myPort.read(); 
   
   if(val == 48){
   //fill(0);
   image(ImageAlpha, 0, 0);
   println("0");
  }
  else if(val == 49){
   //fill(255,0,0);  
   image(ImageBravo, 0, 0);
   println("1");
  }
  
  }
   
  
  
  //rect(50,50,100,100);
  
  
}

/*
void serialEvent(Serial myPort){

  inByte = myPort 
  
}

*/
        
      

Modifying the program

When starting up the program, the program only runs the size() command, to create a window with the resolution stated 720p.

As a result, an image does not show up on the screen until the LED is first switched on. To make it more 'realistic', I simply modified the program to load the unlit light bulb at the start.

Here is a video showing the change:

One thing that I haven't exactly figured out is converting the exact data stream through Serial from Arduino to Processing.

I suspect that it's an error due to the different data types. For instance, when I do a port.print(0); it translates to a '48' in processing. I figured this out by using the println command to print the value of the input when I was debugging why my code didn't work and the bulbs were not loading in. Using my google-fu, 48 is the DEC equivalent of the char '0'. I'll have to figure this out and redo it but for now the code works.

Another thing that I'll have to figure out is the scaling of the image. The light bulbs are always cut short at bottom. Perhaps I'll have to resize the image to fit the entire image in the window.