Thursday, October 8, 2009

Tsveta [ Code 4/6 and 5/6 ]

CODE 4/6 - Lighting LEDs using Array


int ledPin = 3;//declare led pin
int photoPin = 0;//declare the analog reading pin
int photoValue = 0;//declare value to store the analog value
int brightness = 0;//declare variable to store and pass the analog value to the led
void setup() { pinMode(ledPin, OUTPUT);//declare output pin
pinMode(photoPin, INPUT);//declare input pin
Serial.begin(9600); }
void loop() {
photoValue = analogRead(photoPin);//read the analog values from the photocell/flexing sensor
brightness = photoValue;//pass analog value from the analog read varibale to the led brightness variable
Serial.println(brightness);//print value received from the photocell/flexing sensor
analogWrite(ledPin, brightness);//pass the analog value to adjust brightness
delay(200);//remove noise
}

_________________________________________________
--------------------------------------------------------------------------------------

CODE 5/6 - Lighting LEDs using Array
The goal here was to have 3 LEDs in one array and 3 led in a different so I can light them up fast from the center of the row to the ends and when pot turned all the way to the other side then it changes the direction and speed.
The issue was that I could not figure out how to make the two different arrays to be doing the same thing at the same time. That is why I took the i that i used as a delay timer and put actual numbers

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 switchPin2 = 9;
int switchstate = HIGH;




void setup() {
pinMode(switchPin, INPUT);
pinMode(switchPin2, 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
}
}
void loop() {
int state = digitalRead(switchPin);
int state2 = digitalRead(switchPin2);
if ( state != switchstate){
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
//for ( i = 0; i<2000;>
digitalWrite(pinArray[arrayVal], HIGH);
delay(100);
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(100);
digitalWrite(pinArray[arrayVal], LOW);
delay(100);
}
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);}
}
else{
for(arrayVal=0; arrayVal<=2; arrayVal++){ //test pins going from lower number to higher
digitalWrite(pinArray[arrayVal], HIGH); //light up led in a fluid manner
delay(40);
digitalWrite(pinArray[arrayVal+1], HIGH);
delay(40);
digitalWrite(pinArray[arrayVal], LOW);
delay(40);
}
for (arrayVal2 = 2; arrayVal2 >= 0; arrayVal2--){ //test pins going from higher number to lower number
digitalWrite(pinArray2[arrayVal2], HIGH);
delay(40);
digitalWrite(pinArray2[arrayVal2-1], HIGH);
delay(40);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(40);
}
}
}

No comments:

Post a Comment