Tuesday, October 27, 2009

TSVETA [ Code 1/6, 2/6, 3/6 and 6/6 ]

Code 1/6

int led = 5;
int buttonpin = 10; //set up pin for the button
int flex = 3; //set up pin for flex sensor/photo cell
int buttonstate = HIGH;


void setup(){
pinMode(led, OUTPUT);
pinMode(buttonpin, INPUT);//buttonpin and flex sensor/photocell pin to be recognized as input
pinMode(flex, INPUT);
Serial.begin(9600);
}


void loop(){
int state = analogRead(flex); //read analog value from sensor and assign it to state
Serial.println(state);
int button = digitalRead(buttonpin); //
if (button != buttonstate){ //check if state has been changed and if yes
digitalWrite(led, HIGH); // turn led on
delay(state*2); // double the value from analog input and use to determine delay
digitalWrite(led, LOW);//turn led off
delay(state*2);
}

}

Code 2/6


int buttonpin = 10;//set up pin for the button
int photo = 3;//set up pin for photo cell
int buttonstate = HIGH;//initial state for later comparison



void setup(){
for (int thisPin = 2; thisPin <= 7; thisPin++) {
pinMode(thisPin, OUTPUT); //going through the leds to set them as output
}
pinMode(buttonpin, INPUT); //buttonpin and photo pin to be recognized as input
pinMode(photo, INPUT);

Serial.begin(9600);//begin serial port

}

void loop(){

int button = digitalRead(buttonpin); //get the data from the button and assign it to interger button
if (button != buttonstate) { //check if the data is different than the initial setup
int analogvalue = analogRead(photo); //read analog data from photo cell and store it in int analogavalue
Serial.println(analogvalue);
for (int thisPin = 2; thisPin <= 7; thisPin++) { //go through leds from lower number to higher to light them up fluidly


digitalWrite(thisPin, HIGH); // turn the pin on:
delay(analogvalue); // use the analog data to set up delay - dynamically changing
digitalWrite(thisPin, LOW); // turn the pin off:
delay(analogvalue);
//set switch to 0 - stop reading flex
}
delay(20);

for (int thisPin = 7; thisPin >= 2 ; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(analogvalue); // use the analog data to set up delay - dynamically changing
digitalWrite(thisPin, LOW); // turn the pin off:
delay(analogvalue);
}

}

}


Code 3/6

int led = 10;//set up pin 10 for the button
int photo = 3;//set up pin for flex sensor/photo cell
void setup(){
pinMode(led, OUTPUT);//led pin will be outputing info
pinMode(photo, INPUT);//photocell pin will be receiving input
Serial.begin(9600);//begin serial port }
void loop(){
int delaytime = analogRead(photo); //assign analog data received to temporary variable
for(int i=0; i<= 225; i++){ //use i as a counter
analogWrite(led, i); //output to gradually light up led
delay(delaytime); //use value from photocell to set up delay time, dynamically changing }
for(int i=225; i>=0; i--){
analogWrite(led, i); //output to gradually turn off led
delay(delaytime); } }


Code 6/6


int pinArray[] = { 2, 3, 4 }; //declare first array that includes pins 2 through 4
int pinArray2[] = {5, 6, 7}; //declare a second array that includes pins 5 through 7
int arrayVal = 2; //declare a variable to hold the array values; int arrayVal2 = 0;
int switchPin = 10;
int switchstate = HIGH;
int photo = 3;

void setup() {
pinMode(switchPin, INPUT);
pinMode(photo, INPUT);
for ( arrayVal = 2; arrayVal >= 0; arrayVal-- ){ //use the for statement to go through all numbers from the array - from 4 to 2
pinMode(pinArray[arrayVal], OUTPUT); //set all pins from that array to be output }
for ( arrayVal2 = 0; arrayVal2 <=2 ; arrayVal2++ ){ //use the for statement to go through all numbers from the array - from 5 to 7
pinMode(pinArray2[arrayVal2], OUTPUT); //set all pins from that array to be output }
Serial.begin(9600);
}

void loop() {
int state = digitalRead(switchPin); //check switch state
int analogvalue = analogRead(photo); //take analog values and assign to local variable
Serial.println(analogvalue);
if (state != switchstate && analogvalue ){
arrayval =" 2;">=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH); //light them up fluidly
delay(30);
digitalWrite(pinArray[arrayVal-1], HIGH);
Delay(30);
digitalWrite(pinArray[arrayVal], LOW);
delay(30);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH); //light up led in a fluid manner
delay(30);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(30);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(30);
}
}
if ( state == switchstate ){ //if there has been change in the switch mode
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH);
delay(analogvalue); //use the analog value received to determine
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal], LOW);
delay(analogvalue);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH);
//light up led in a fluid manner
delay(analogvalue);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(analogvalue);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(analogvalue); }
if (state != switchstate && analogvalue > 60){ //if the analog value is more than 60 and switch state hasn't been changed
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal], LOW);
delay(analogvalue);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH); //light up led in a fluid manner
delay(100);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(100);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(100);
}
}
/* //////////////////HERE IS WHERE I STILL HAVE ISSUES TO SOLVE BUT THE GOAL IS TO USE SINGLE ANALOG VALUE, LET IT GROW IT OVER TIME AND USE THAT TO GRADUALLY TURN THE LEDS ON FOR A SOFTER FEELING AND LOOK/////////////////////////////////// if (state==switchstate && analogvalue>=200){ int myvalue = analogvalue; for(arrayVal=0; arrayVal<=2; arrayVal++){ //test pins going from lower number to higher for( myvalue =0; myvalue<=255; myvalue++){ analogWrite(pinArray[arrayVal], myvalue); //light up led in a fluid manner delay(20); analogWrite(pinArray[arrayVal+1], 255-myvalue); delay(20); //analogWrite(pinArray[arrayVal], myvalue); //delay(2); } for (arrayVal2 = 2; arrayVal2 >= 0; arrayVal2--){ //test pins going from higher number to lower number digitalWrite(pinArray2[arrayVal2], myvalue); delay(20); digitalWrite(pinArray2[arrayVal2-1], myvalue); delay(20); digitalWrite(pinArray2[arrayVal2], myvalue); delay(20); } } }*/ } }

Shanna B Exercise #3 (Resistor Light Value)

int ledPin = 9; // Declare pin9 for LED
int photoPin = 1; // variable resistor on analog pin 1
int val = 0;

void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 9 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 1 as OUTPUT
}

void loop() { // run over and over again
val = analogRead(photoPin); // set val equal to the resistor input

if(val == LOW) {
digitalWrite(ledPin, HIGH);
delay(100);

} else {
digitalWrite(ledPin, LOW);
}
Serial.println(val);
} video

Sunday, October 18, 2009

PWM LED

int ledPin = 10;
int analogPin = 0;
int analogValue = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(analogPin, OUTPUT);
}


void loop() {
  analogValue = analogRead( analogPin);
  
  analogWrite(ledPin, analogValue);
  
/*  if(analogValue == HIGH){
  analogWrite(ledPin, LOW);
  delay(200);
  }
  
  else
{
  analogWrite(ledPin, HIGH);
}*/
Serial.println(analogValue);
}
video

D.collins_Homework_Exercise 3

int ledPin = 9; // declare Pin 9
int analogPin = 2; // Analog pin 2

void setup(){} // no setup needed

void loop() //Function read the analog value and set a delay time and controls the speed at which an LED brightens and dims
{
for (int i=100; i<=10; i++) // give value of 255 for variable "i"
// The above "for loop" function starts the integer "i" at 0, and tests to see if i is still less or equal to 255 and if true, increments "i" by 1 and executes the enclosed statements. i++ is the same as i=i +1 or i by +1

{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
for (int i=255; i>=0; i--) // if i is 255 then i should be 0
{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
}

int delayVal() // declare variable for delay time value
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 20; // convert 0-1024 to 0-128
return v; // returns final value
} video

D.collins_Classwork_Exercise 3

int ledPin = 12; // Declare pin 11 for LED
int photoPin = 3; // variable resistor on analog pin 0
int photoValue = 0;
int boo = 0;

void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 12 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 3 as OUTPUT
}

void loop() { // run over and over again
photoValue = analogRead(photoPin); // set val equal to the resistor input

boo = (photoValue/ 3) * 40;



Serial.println(boo);
analogWrite(ledPin, boo);
delay(100);
} video