2010年11月30日火曜日

For Patrick, check this sample program for your purpose

//sample progrma with shiftresiostors and photosensors
// this program is very basic. you can use one photosensor and upto two shiftresistor so that you can control upto 16 LED with only 3 digital pins of Arduino

//connect photosensor to analog pin 0
int analogData0;


//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//regure variables
int RadomVal0;


void setup(){
//Start Serial for debuging purposes
Serial.begin(9600);
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
}

void loop(){
//read data from analog pin 0
analogData0=analogRead(0);
//scale down the value form 0-1023 to 0-255 which works with shift resistor.
analogData0=analogData0/4;
//put a randomize value into variable RadomVal0 this value take form "0" to "the scaled value" so that the new value still reflect the photosensor's value
// this code is optional
RadomVal0 = random(0,analogData0);
// tell shiftresistor to start
digitalWrite(latchPin, 0);
// actual data
shiftOut(dataPin, clockPin,MSBFIRST, RadomVal0);
//if you have second shift resistor. elase // and activate the program
//shiftOut(dataPin, clockPin,MSBFIRST, RadomVal0);
//tell shiftresistor to edn
digitalWrite(latchPin, 1);

// duration of the led light
delay(1000);
}

/*

the following program is optional.
if you want to use the following program replace "void loop" to the following program

void loop(){
//read data from analog pin 0
analogData0=analogRead(0);
//scale down the value form 0-1023 to 0-255 which works with shift resistor.
analogData0=analogData0/4;
// tell shiftresistor to start
//this will enable to light up LED more interestingly
for (int j = 0; j < analogData0; j++) {
digitalWrite(latchPin, 0);
// actual data
shiftOut(dataPin, clockPin,MSBFIRST, RadomVal0);
//if you have second shift resistor. elase // and activate the program
//shiftOut(dataPin, clockPin,MSBFIRST, RadomVal0);
//tell shiftresistor to edn
digitalWrite(latchPin, 1);

// duration of the led light for each led pattern
delay(300);
}
}

*/

0 件のコメント:

コメントを投稿