Assignment5 Led

Arduino - Processing

Blink LED

int pinNo=13;
int on=1000;
int off=500;

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

void loop()
{
digitalWrite(pinNo, HIGH);
delay(on);
digitalWrite(pinNo, LOW);
delay(off);
}

Inputs from keyboard, LED Output

int outputPins[] = {2,3,4,5};
int val;
int num_pins=4;
int i;
void setup()
{
Serial.begin(9600);
for(i=0;i<num_pins;i++)
pinMode(outputPins[i], OUTPUT);
}

void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'a') {
digitalWrite(outputPins[0], HIGH);
}
if (val == 'b') {
digitalWrite(outputPins[1], HIGH);
}
if (val == 'c') {
digitalWrite(outputPins[2], HIGH);
}
if (val == 'd') {
digitalWrite(outputPins[3], HIGH);
}
if (val == 'l') {
for(i=0;i<num_pins;i++)
digitalWrite(outputPins[i], LOW);
}
}
}

Processing CODE

import processing.serial.*;
Serial port;
int value =0;
void setup()
{
size(300,300);
println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
}
void draw()
{
fill(value);
rect(100,100,75,75);
}

void keyPressed() {
if (key == 'a' || key == 'A') {
value =255;
port.write('a');
}
if (key == 'b' || key == 'B') {
value= 200;
port.write('b');
}
if (key == 'c' || key == 'C') {
value=150;
port.write('c');
}
if (key == 'd' || key == 'D') {
value=50;
port.write('d');
}
}

void keyReleased() {
value=0;
port.write('l');
}

Input from a photosensitive device and produce fluctuations on screen

Processing CODE

import processing.serial.*;

Serial port;

int Length = 150;
int Width = 150;

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

int val,preval=0;
void draw() {
fill(255);
rect(50, 50, 50, 50);

int val = port.read();
if(preval!=val)
{
fill(val);
rect(50, 50, 50,50);
}
else{
fill(255);
rect(50, 50, 50, 50);
preval=val;
}
}

Arduino CODE

int potPin = 2; // select the potentiometer
int ledPin = 13; // select the LED pin
int val = 0; // variable to store the value derived from the sensor
int timer=1000;

void setup() {
pinMode(ledPin, OUTPUT); // ledPin for OUTPUT
Serial.begin(9600);
}

void loop() {
val = analogRead(potPin); // value from the sensor
digitalWrite(ledPin, HIGH); // ledPin on
delay(val); // delay the program
digitalWrite(ledPin, LOW); // ledPin off
delay(timer); // delay the program

Serial.print(val);
}

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