Variable Resistor: Light Value
int ledPin = 11; // Declare pin 11 for LED
int photoPin = 0; // variable resistor on analog pin 0
int val = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 11 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 0 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);
}
Variable Resistor: LED Array
int timer = 100; // Number for the time - the higher the number, the slower the timing.
int ledPins[] = {8,9,10,11,12 }; // an array of pin numbers to which LEDs are attached
int pinCount = 5; // the number of pins
int analogPin = 0; // the pin that the potentiometer is attached to
int analogValue = 0;
void setup() { // loop over the pin array and set them all to output:
int thisPin;
pinMode(analogPin, INPUT);
for (int thisPin = 0; thisPin < style="color: rgb(102, 51, 0);">// the array elements are numbered from 0 to (pinCount - 1).
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() { // loop from the lowest pin to the highest
int analogValue = analogRead(analogPin);
if(analogValue >= 50) {
for (int thisPin = 0; thisPin < style="color: rgb(102, 51, 0);">// loop from the highest pin to the lowest
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
}
Variable Resistor: Timing
int led1Pin = 9; // PWM pin 9 for the LED 1
int led2Pin = 11; // PWM pin 11 for the LED 2
int analogPin = 0; // variable resistor on analog pin 0
void setup(){ }
void loop()
{
for (int i=0; i<=255; i++) // ascending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}
for (int i=255; i>=0; i--) // descending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}
for (int i=0; i<=255; i++)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
for (int i=255; i>=0; i--)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
}
int delayVal()
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 7; // this convert 0-1024 to 0-128
return v; // returns final value
}
No comments:
Post a Comment