Week 16 Interface and application programming¶
This week we had an individual assignment and a group assignment
Group assignment¶
For the group assignment we had to compare as many tool options as possible. For more info click here
Individual assignment¶
For the indivdual assignment we had to write an application that interfaces a user with an input &/or output device that you have made.Because we were out of Attiny45, I resoldered my output board and put it back on my input board. I then had to reprogram the board again. The sensor I used for my input board was the phototransistor. This sensor detects light and when programmed right can have your LED turn on in the dark and turn off when the there is light reflecting on it.
Software¶
The board I reprogrammed using Arduino. First time I got an error burning the bootloader, but it eventualy worked.
This is a piece of the code I used to program my input board again in Arduino.
#include <SoftwareSerial.h> // *** // *** Define the RX and TX pins. Choose any two // *** pins that are unused. Try to avoid D0 (pin 5) // *** and D2 (pin 7) if you plan to use I2C. // *** #define RX 1 // *** D3, Pin 2 #define TX 2 // *** D4, Pin 3 // *** // *** Define the software based serial port. Using the // *** name Serial so that code can be used on other // *** platforms that support hardware based serial. On // *** chips that support the hardware serial, just // *** comment this line. // *** SoftwareSerial Serial(RX,TX); // What pin to connect the sensor to #define LIGHTPIN 3 void setup(void) { Serial.begin(9600); pinMode(4, OUTPUT); pinMode(1, OUTPUT); }
I checked the serial monitor, but I kept on getting COM 14 not available. It was on COM 15.
Programming¶
I downloaded & installed processing 3.5.3. I then used this code to communicate with the board through serial. I set the stroke color
{ size(800,700); //make our canvas 800 x 700 pixels println(Serial.list()); //show available ports thisPort = new Serial(this, Serial.list()[0], 9600); thisPort.bufferUntil('\n'); //set the port to store incoming data background(0); //set background color for the window } void draw() { stroke(055,100,0); //set stroke color line(xPos, 0, xPos, inByte); //draw a line if (xPos >= width) { //if the screen has filled with the graph xPos = 0; //reset position in X to 0 background(0); //reset background } else { //otherwise xPos++; //advance one pixel to draw next line } } void serialEvent(Serial thisPort) { String inString = thisPort.readStringUntil('\n'); // store data read from serial //in a String type variable if (inString != null) { //if there is information read from serial inString = trim(inString); //removes whitespace characters fro //the beginning and end of a String inByte = float(inString); //convert serial data to a flat type number println(inByte); //show value inByte = map(inByte, 0, 1000, 0, 700); //re-maps a number from one range to another } }
Code explanation¶
stroke(055,100,0); //set stroke color
This piece of code sets the color of the graphs
{ String inString = thisPort.readStringUntil('\n'); // store data read from serial //in a String type variable if (inString != null) { //if there is information read from serial
This piece of the code stores data read from serial
inString = trim(inString); //removes whitespace characters fro //the beginning and end of a String
This piece of code removes whitespace characters from the beginning and end of a string
inByte = float(inString); //convert serial data to a flat type number println(inByte); //show value
This piece of code convert serial data to a flat type number
inByte = map(inByte, 0, 1000, 0, 700); //re-maps a number from one range to another }
This piece of code re-maps a number from one range to another
The result of my programming¶
My interface shows my input board turning serial into string, which means the serial numbers will be displayed as a graph and the bars of the graph will change according to the light reflecting on the board.