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); } } }*/ } }

No comments:

Post a Comment