Assignment 4 - Sanket Bindle

by

Sanket Bindle

Processing - Arduino Interfacing

Part 1:

Keyboard as Input and LED for Output

Using Keyboard to generate the output where different LED glows corresponding to a different key press.
Arduino IDE is used to compile and burn the code to Arduino board using USB.
Processing is used to detect the key press and pass on this information through a serial port to the Arduino board.

Code for Arduino IDE____________________

int outputPin1 = 13;
int outputPin2 = 2;
int outputPin3 = 5;
int outputPin4 = 6;

int val;

void setup() {
Serial.begin(9600);
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
pinMode(outputPin3, OUTPUT);
pinMode(outputPin4, OUTPUT);
}

void loop() {
if (Serial.available()) {
val = Serial.read();
if (val == 'A') {
digitalWrite(outputPin1, HIGH);
}

if (val == 'Z') {
digitalWrite(outputPin1, LOW);
}
if (val == 'S') {
digitalWrite(outputPin4, HIGH);
}
if (val == 'X') {
digitalWrite(outputPin4, LOW);
}
if (val == 'D') {
digitalWrite(outputPin3, HIGH);
}
if (val == 'C') {
digitalWrite(outputPin3, LOW);
}
if (val == 'F') {
digitalWrite(outputPin2, HIGH);
}
if (val == 'V') {
digitalWrite(outputPin2, LOW);
}
}
}

Code for Processing___________________

import processing.serial.*;

Serial port;

int x = 400;
int y = 400;

void setup()
{
size(400,400);
background(255,255,0);
frameRate(10);
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600);
}

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

void draw()
{
line(x/2,0,x/2,y);
line(0,y/2,x,y/2);

if (keyPressed){
if((key == 'a' || key == 'A') && val1 == 0){
val1 = 255;
fill(val1);
rect (0,0,x/2,y/2);
port.write('A');
}
else if((key == 'a' || key == 'A') && val1 == 255){
val1 = 0;
fill(val1);
rect (0,0,x/2,y/2);
port.write('Z');
}
if((key == 's' || key == 'S') && val2 == 0){
val2 = 255;
fill(val2);
rect (x/2,0,x/2,y/2);
port.write('S');
}
else if((key == 's' || key == 'S') && val2 == 255){
val2 = 0;
fill(val2);
rect (x/2,0,x/2,y/2);
port.write('X');
}
if((key == 'd' || key == 'D') && val3 == 0){
val3 = 255;
fill(val3);
rect (0,x/2,x/2,y/2);
port.write('D');
}
else if((key == 'd' || key == 'D') && val3 == 255){
val3 = 0;
fill(val3);
rect (0,x/2,x/2,y/2);
port.write('C');
}
if((key == 'f' || key == 'F') && val4 == 0){
val4 = 255;
fill(val4);
rect (x/2,y/2,x/2,y/2);
port.write('F');
}
else if((key == 'f' || key == 'F') && val4 == 255){
val4 = 0;
fill(val4);
rect (x/2,y/2,x/2,y/2);
port.write('V');
}
}
}

/////////////////////////////////////

Part 2:

Amount of light to determine the blinking of the LED

The photoresistor was used to control the frequency of blink of the LEDs.
More light reduces the frequency and vice-versa.
Here only Arduino IDE was used as there was no external input device.

Code for Arduino IDE_____________________

int potPin = 2;
int ledPin = 13;
int val = 0;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
val = analogRead(potPin);
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);
delay(val);
}

///////////////////////////////////

Part 3:

Light as Input on Arduino and an Output on the Screen

Photodiode was used to get light as an input on Arduino, the activity of which was monitored through the program running on processing through a serial port and the output was generated on the screen using processing language. Here a square keeps on alternating colours between black and white corresponding to the blinking of the LED.

Code for Arduino IDE___________________

int vPin = 1;
int outputPin = 13;
int checkPin = 7;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(checkPin, OUTPUT);
}

int h = 1;
int l = 0;

void loop() {
val = analogRead(vPin);
digitalWrite(outputPin, HIGH);
if (Serial.available()) {
Serial.print(h);
}
delay(val);
digitalWrite(outputPin, LOW);
if (Serial.available()) {
Serial.print(l);
}
delay(val);
}

Code for Processing___________________

import processing.serial.*;

Serial port;

void setup()
{
size(400,400);
background(255,255,0);
frameRate(10);
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600);
}

int val = 0;
int portval = 0;

void draw()
{
port.write('0');
portval = port.read();
println(portval);
if (portval == '1')
{
fill(255);
rect(100,100,200,200);
}

else if (portval == '0')
{
fill(0);
rect(100,100,200,200);
}
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License