Arduino Assignments

Hands on with arduino kit - simple serial communication between Processing and arduino board. Also use of photo sensors.

# Program 1 - Serial Communication

ArduIno Code

int[] outputPins = {2,3,4,5}
int val;
int count=4;

void setup()
{
Serial.begin(9600);
for(int i=0;i<count;i++)
{
pinMode(outputPins[i], OUTPUT); // initialize pins 2 3 4 and 5 for output
}
}

void loop()
{
if (Serial.available()) {
val = Serial.read();

switch(val){
case 'A':digitalWrite(outputPins[0], HIGH);
break;

case 'B':digitalWrite(outputPins[0], HIGH);
break;

case 'C':digitalWrite(outputPins[0], HIGH);
break;

case 'D':digitalWrite(outputPins[0], HIGH);
break;

} // end switch
} // end if
} // end loop

Processing Code

// For passing info to ArduIno - define 2 ports

import processing.serial.*;
Serial port;

void setup()
{
size(400,400);
noStroke();
background(300);

port=new Serial(this,Serial.list()[0],9600); // Port
}

int count = 0;

void draw() {

if(keyPressed) {
switch(key){
case 'a':
port.write('A');
break;

case 'b' :
port.write('B');
break;

case 'c' :
port.write('C');
break;

case 'd' :
port.write('D');
break;
}
//rect(25, 25, 50, 50);
}
}

# Program 2 - Photo Sensor

int inputPin = 4; // input pin for the photoresistor
int ledPin = 13; // pin for the LED

int val = 0; // value from the sensor

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}

void loop() {
val = analogRead(inputPin); // read the value from the sensor
analogWrite(ledPin, val/2); // turn the ledPin on
delay(100); // stop the program for some time
analogWrite(ledPin, val/2); // turn the ledPin off
delay(100); // stop the program for some time
}

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