Category Archives: Week 2

LED’s

 

I used a piezo resistor to control the color of an RGB LED.

CODE:

int knockSensor = 0;

byte val = 0;

int THRESHOLD = 5;

int redPin = 12; // Red LED, connected to digital pin 9
int greenPin =11; // Green LED, connected to digital pin 10
int bluePin =10; // Blue LED, connected to digital pin 11

void setup() {

Serial.begin(9600);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}
void loop() {

val = analogRead(knockSensor);

if (val >= THRESHOLD) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);

Serial.println(“Knock!”);

}

else{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}

delay(50
);

}

i also made this:

 

//code for fade taken and modified from http://www.instructables.com/id/Fading-RGB-LED-Arduino/?ALLSTEPS
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;

int i = 0; // Loop counter
int wait = 1; // 50ms (.05 second) delay; shorten for faster fades
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // …set up the serial ouput on 0004 style
}
}
#define fade(x,y) if (x>y) x–; else if (x<y) x++;

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
action();
}
else{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
void action(){

{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else
{
i = 1;
}

analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
delay(wait); // Pause for ‘wait’ milliseconds before resuming the loop
}

 

 

Week 2 – Blink

Hi everyone, this is a step by step into my first homework, I started controlling the LED with the potentiometer ..

photo 1

connecting Arduino with the board

photo 2

adding the potentiometer to control an LED

photo 3

adding the LED

Code:

const int ledPin = 9; // pin that the LED is attached to
int analogValue = 0; // value read from the pot
int brightness = 0; // PWM pin that the LED is on.

void setup() {
Serial.begin(9600);
// led pin as an output:
pinMode(ledPin, OUTPUT);
}

void loop() {
analogValue = analogRead(A0);
brightness = analogValue /4;  //divide by 4 to fit in a byte
analogWrite(ledPin, brightness);
Serial.println(brightness); // print the brightness value back to the serial monitor
}

 

Then after this exercise I wanted to use a Flex sensor to manipulate the brightness:

photo 4

then I had a problem with resistors. I think. I hope.

 

 

 

homework for week 2: Blink and lighten

The blink frequency and brightness of LED can be both controlled at the same time by the knob.

IMG_9051_2

 

 

/*
LED -> GND, 9
KNOB -> (middle) ANALOG IN 0
-> (other) 5V, GND
*/

int buttonPin = 2;
int ledPin = 9;
int potpin = 0;
int val, f;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
f = (180 – val) * 2;

analogWrite(ledPin, val);
delay(f);
analogWrite(ledPin, val/3);
delay(f);
}

Homework week 2 – Udit Mahajan

I took help from the SIK Guide and made this.  An RGB led that changes colors. It goes from primary to secondary and then displays the entire spectrum.

https://drive.google.com/a/newschool.edu/file/d/0B1d_zqYso7DgbU9NWFlCUVl3UFU/edit?usp=sharing

I did put it inside a box!

https://drive.google.com/a/newschool.edu/file/d/0B1d_zqYso7DgVHl6Um5rVUFrWEE/edit?usp=sharing

Code:

int red_pin = 9;
int green_pin = 10;
int blue_pin = 11;

void setup()
{

pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
}

void loop()
{
digitalWrite(red_pin, LOW);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);

delay(1000);

for (int x = 0; x <= 767; x++)
{
rgb(x);
delay(10);
}
}

void rgb(int color)
{
int red, green, blue;

if (color <= 255)
{
red = 255 – color;
green = color;
blue = 0;
}
else if (color <= 511)
{
red = 0;
green = 255 – (color – 256);
blue = (color – 256);
}
else
{
red = (color – 512);
green = 0;
blue = 255 – (color – 512);
}

analogWrite(red_pin, red);
analogWrite(blue_pin, blue);
analogWrite(green_pin, green);
}

HW2_PUSHBUTTON&LEDS_YU_ZHANG

20140912131310

 

Push Button -> LEDS

Seven green flashing LEDs controlled by one pushbutton.

All seven green flashing LEDs were connected on another breadboard.

Each time I push the button, the flashing LEDs will stop flashing.

————————————————

CODE:

int buttonPin = 2;
int ledPin = 9;
int buttonState = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
    delay(50);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

HAOTIAN_HOMEWORK

I use the RGB LED light with photoresistors to make the LED change color. The LED can show different colors when you change the amount of light that hits on the photoresistors.

WEEK2

Video:

Pcomp_week2

Arduino code


const int greenLEDPin=9;

const int redLEDPin=10;
const int blueLEDPin=11;

const int redSensorPin=A0;
const int greenSensorPin=A1;
const int blueSensorPin=A2;

int redValue=0;
int greenValue=0;
int blueValue=0;

int redSensorValue=0;
int greenSensorValue=0;
int blueSensorValue=0;

void setup(){
Serial.begin(9600);
pinMode(greenLEDPin,OUTPUT);
pinMode(redLEDPin,OUTPUT);
pinMode(blueLEDPin,OUTPUT);
}

void loop(){
redSensorValue = analogRead(redSensorPin);
delay(5);
greenSensorValue = analogRead(greenSensorPin);
delay(5);
blueSensorValue = analogRead(blueSensorPin);
Serial.print(“Raw Sensor Value \t Red:”);
Serial.print(redSensorValue);
Serial.print(“\t Green:”);
Serial.print(greenSensorValue);
Serial.print(“\t Blue:”);
Serial.println(blueSensorValue);
redValue = redSensorValue/4;
greenValue = greenSensorValue/4;
blueValue = blueSensorValue/4;

Serial.print(“Mapped Sensor Value \t Red:”);
Serial.print(redValue);
Serial.print(“\t Green:”);
Serial.print(greenValue);
Serial.print(“\t Blue:”);
Serial.println(blueValue);

analogWrite(redLEDPin, redValue);
analogWrite(greenLEDPin, greenValue);
analogWrite(blueLEDPin, blueValue);

}


Continue reading

Luobin_Homework

A simple Arduino – Processing communication project.

The 3 LEDs are controlled by a button switch. The potentiometer is used to control one of the green lights, and send serial data to processing. Processing simply uses the data to draw a dynamic sphere on the screen.

 

P1_LBW

 

 

ARDUINO CODE

————————————————————

int switchState = 0;

void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
}
void loop(){

switchState = digitalRead(2);
float wait = map(analogRead(A1), 0, 1024, 0, 100);
Serial.println(analogRead(A1));

if(switchState == LOW){

digitalWrite(3, HIGH);
delay(wait);
digitalWrite(3, LOW);
delay(wait);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);

}
}

 

PROCESSING CODE

___________________________________________________

import processing.serial.*;
Serial myPort;
String val;
float rad;
void setup()
{
size(500, 500, P2D);
frameRate(60);

// serial example from processing
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this,”COM3″, 9600);
}
void draw()
{
background(0);
noStroke();
fill(0, rad, 0);
if ( myPort.available() > 0)
{ // If data is available,

//try read port data, failed
// float inByte = myPort.read();
println(rad);
val = myPort.readStringUntil(‘\n’);

//exclude null for result
//if not, will return null pointer exception
if(val != null){
rad = float(val);
}

map(rad, 0, 1024, 0, 255);
ellipse(rad, rad, rad, rad);

}
//print it out in the console

// rad = float(val);

}

 

Assignment2-Blink

The LED light already true on now because it received the light from the atmosphere but the LED light turn off is when you lay your hand down close to photocell or touch it.

hw2

https://vimeo.com/105937518

Code

int sensorValue = 0; // value read from the pot

int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 500, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print(“sensor = ” );
Serial.print(sensorValue);
Serial.print(“\t output = “);
Serial.println(outputValue);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

 

cr. by Tom Igoe

 

 

PCOM assignment 1 : a button

here is the code :

int led = 13;
int controller = 2;
int n =0;

void setup()
{
pinMode(2,INPUT);
pinMode(13, OUTPUT);
pinMode(4,OUTPUT);
}

void loop()
{
n = digitalRead(2);
Serial.println(n);
if (n == HIGH)
{

digitalWrite(13,HIGH);
digitalWrite(4, LOW);

}
else {

digitalWrite(13, LOW);
digitalWrite(4, HIGH);
}
}

 

 

i  make a button to control one RGB led’s light color which is also a  switch for the  other 2 led .

here is the video

 

 

 

kAi_homework_1

I tried to switch LEDs by button.

One LED is with fading, controlled by code. The other one is just physics. Some magical thing was happened that I could turn on/off the LED by air…. LOL

My code:

const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
int brightness = 0;
int fadeAmount = 5;

// variables will change:
int buttonState; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;

if(brightness == 0 || brightness == 255){
fadeAmount = -fadeAmount;
}
delay(15);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

}