Key Input & LED Output

Input through the keyboard and LED output

Here the input is through the keyboard and the output is through the LED.
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');
}
}
}

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