Assignment 5-saurabh

Digital to analog conversion

Arduino Code:

int outputPins[] = {3,4,13};
//int outputPin = 13;
int val;

void setup()
{
Serial.begin(9600);
pinMode(outputPins[0], OUTPUT);
pinMode(outputPins[1], OUTPUT);
pinMode(outputPins[2], OUTPUT);
}

void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'a') {
digitalWrite(outputPins[0], HIGH);
delay(1000);
}
if (val == 'b') {
digitalWrite(outputPins[1], HIGH);
delay(1000);
}
if (val == 'c') {
digitalWrite(outputPins[2], HIGH);
//delay(1000);
}
if (val == 'e') {
digitalWrite(outputPins[0], LOW);
digitalWrite(outputPins[1], LOW);
digitalWrite(outputPins[2], LOW);
}
}
}

Processing Code:

import processing.serial.*;

Serial port;

void setup()
{
size(200, 200);

println(Serial.list());

// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[1], 9600);
}

int value =0;

void draw()
{
fill(value);
rect(50,50,50,50);
}

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=110;
port.write('c');
}
}

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

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