Or what would your LED do if it were sacred of the dark?
Building upon the lab example and adding a cell sensor into the mix, plus some simple programming.
The trick was in calibrating the values correctly in order to achieve a pleasing effect.
Or what would your LED do if it were sacred of the dark?
Building upon the lab example and adding a cell sensor into the mix, plus some simple programming.
The trick was in calibrating the values correctly in order to achieve a pleasing effect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <em> int potPin = 0; // Analog input pin that the potentiometer is attached to int potValue = 0; // value read from the pot int cellPin = 1; // Analog input pin that the potentiometer is attached to int cellValue = 0; // value read from the pot int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9 void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { cellValue = analogRead(cellPin); // Read the cell value int outputCellValue = map(cellValue,1,25,1,1000); // map the cell value Serial.println("Cell Value = "); // print the cell tag Serial.println(cellValue); // print the cell value back to the debugger pane Serial.println("Cell output = "); // print the cell tag Serial.println(outputCellValue); // print the cell value back to the debugger pane potValue = analogRead(potPin); // read the pot value if (outputCellValue<320) { // check for light conditions if (outputCellValue<20) { // set low limit for behavior in extreme darkness outputCellValue=20; } if (outputCellValue>2000) { // set high limit for behavior in bright conditions outputCellValue=1950; } int delayVar = outputCellValue; //Serial.println("delayVar = "); //Serial.println(delayVar); // print the cell value back to the debugger pane analogWrite(led, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte) delay(delayVar); // blink delay analogWrite(led, 0); // blink off delay(delayVar); // blink delay // Serial.println("Dial Value:"); // print the pot value back to the debugger pane // Serial.println(potValue); // print the pot value back to the debugger pane } else // Simply turn LED according to potentiometer values when in lit conditions { analogWrite(led, potValue/4); } delay(20); // wait 10 milliseconds before the next loop }</em> |