Arduino Assignments

Assignment 1.

Programme to blink the LED

int ledPin = 13; // LED connected to digital pin 13

void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(500); // waits for a half second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

Assignment 2.

Programme to blink the LED'S on key press a,b,c

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' ) {
value =255;
port.write('a');
}
if (key == 'b' ) {
value= 200;
port.write('b');
}
if (key == 'c' ) {
value=110;
port.write('c');
}
}

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

Assignment 3.

Programme for Photo sensor

Arduino Code:

int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

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

void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time

Serial.print(val);
}

Processing Code:

import processing.serial.*;

Serial port;

int Length = 200;
int Width = 300;

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

int value =255;

void draw() {
fill(255);
rect(50, 50, 200, 100);

int val = port.read();
println(" " + val + " ");
fill(val);
rect(50, 50, 200, 100);

}

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