Arduino assignments(suruchika)

Assignment 1:

Program to blink an LED at an interval -

int pinNo=13;
int timer=1000;

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

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

Assignment 2:

Program to light an LED by pushing a button -

Arduino code :

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

void setup()
{
int i=0;
Serial.begin(9600);
  for(i=0;i<numpins;i++){
     pinMode(outputPins[i], OUTPUT);
  }
}

void loop()
{
int i=0;
if (Serial.available()) {
val = Serial.read();
   if (val == 'a' || val == 'A') {
     digitalWrite(outputPins[0], HIGH);
  }
   if (val == 'b' || val == 'B') {
     digitalWrite(outputPins[1], HIGH);
  }
  if (val == 'c' || val == 'C') {
     digitalWrite(outputPins[2], HIGH);
  }
  if (val == 'd' || val == 'D') {
     digitalWrite(outputPins[3], HIGH);
  }
  if (val == 'e' || val == 'E') {
   for(i=0;i<numpins;i++)
   digitalWrite(outputPins[i], LOW);
  }
}
}

Processing code:

import processing.serial.*;

Serial port;

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

println(Serial.list());

port = new Serial(this, Serial.list()[1], 9600); // Arduino board is connected to (in this case #1)
                                                                      // speed Arduino is using (9600bps)
}

int value =0;

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

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

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

Assignment 3

Program using potentiometer sensing to generate output on the screen

Arduino code:

int potPin = 2;      // input pin for the potentiometer

int ledPin = 13;    // pin for the LED

int val = 0;           // to store the value coming from the sensor

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

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

Serial.print(val);
}

Processing code:

import processing.serial.*;

Serial port;

int squ = 200;

void setup()
{
  println(Serial.list());
  size (squ,squ);
  background(0,0,0);
  port = new Serial(this, Serial.list()[1], 9600);

}

int value =255;

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

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

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