BACK

Assignment 6


This is the schematic for my circuit. It's similiar to my previous one, except for the LED. The joystick has two potentiometers (x and y). The user moves the joystick around to change the value of the potentiometer. The LED was added to be responsive to the user's press of the space key on the keyboard.




This is my circuit. The joystick connects directly to the UNO board's power and ground. The X and Y potentiometer connections of the joystick are connected to the UNO board's A1 and A2. The LED is connected to ground and pin 5. There is a 220 Ohms resistor between pin 5 and the LED. A 220 Ohm resistor was used because a blue LED has a voltage drop of 1.8V and a desired ampage of 0.02A. Given the equation, V=IR, the optimal resistor is 160 Ohms. 220 Ohms was the closets I had.




Firmware

//setup function
void setup() {
//serial setup
Serial.begin(9600);
//pinMode setup for LED
pinMode(5, OUTPUT);
}

//loop function
void loop() {
//defining s1 variable
int s1 = analogRead(1);
//defining s2 variable
int s2 = analogRead(2);
//serial making an array
Serial.print("[");
//putting s1 in serial array
Serial.print(s1);
// serial array
Serial.print(",");
//putting s2 in serial array
Serial.print(s2);
//closing serial array
Serial.println("]");

//if statement checking for serial data
if (Serial.available() > 0) {
//reading serial data
int inByte = Serial.read();
//converting sata to raw binary data
Serial.write(inByte);
//if statement for space bar
if (inByte == 32){
//if space bar was the last key pressed turn LED on
analogWrite(5, 50);
}
//else statement
else{
//if another key is pressed turn LED off
analogWrite(5, 0);
}
}
}

JavaScript

var serial; // variable to hold an instance of the serialport library
var portName = 'COM5' //rename to the name of your port
var dataarray = []; //some data coming in over serial!
var xPos = 0;


function setup() {
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing

serial.list(); // list the serial ports
serial.open(portName); // open a serial port
createCanvas(1200, 800);
background(0x08, 0x16, 0x40);
}

// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}

}

function serverConnected() {
print('connected to server.');
}

function portOpen() {
print('the serial port opened.')
}

function serialError(err) {
print('Something went wrong with the serial port. ' + err);
}

function portClose() {
print('The serial port closed.');
}

function serialEvent() {
if (serial.available()) {
var datastring = serial.readLine(); // readin some serial
var newarray;
try {
newarray = JSON.parse(datastring); // can we parse the serial
} catch(err) {
//console.log(err);
}
if (typeof(newarray) == 'object') {
dataarray = newarray;
}
console.log("got back " + datastring);
}
}

function keyPressed() {
console.log("writing key");
serial.write(key);
}
function draw() {
background(25);
ellipse(dataarray[0], dataarray[1], 50,50);
}

This is the code I used in Arduino to create this circuit and webpage. The firmware creates an array with the potentiometers' values and that is read by the JavaScript which spares that array into the potentiometers' values. These values are reflected on the webpage by a white cirlce's place in a pink box, as shown below. There is also a key pressed function that tracks the keys pressed on the keyboard, the firmware then uses this data to turn on/off the LED on the board.




This is the gif of my circuit and webpage in operation. When I move the joystick it is reflected by the white dot on screen. And when I press the space bar the LED turns on. When I press a different key it turns off.

Code sourced from: https://github.com/machineagency/hcde439