### Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

### Messages - Ayle

Pages: 
1
##### User Projects / Code Examples / Add a pot to change timer of LED's
« on: April 28, 2013, 08:31:02 AM »
Add a pot to change timer of LED's, adjusting the pot resets the timer with out rewriting the sketch then loading it again and again to git  just the right speed.

/*
For Loop Iteration

Demonstrates the use of a for() loop.
Lights multiple LEDs in sequence, then in reverse.

The circuit:
* LEDs from pins 0 through 12 to ground 5k pot at A0 adj timer

created 2006
by David A. Mellis
modified 5 Jul 2009
by Tom Igoe
modified Aug 2011 by Ayle
http://www.arduino.cc/en/Tutorial/ForLoop
*/
int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int timer;   // The higher the number, the slower the timing.

void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < 13; thisPin++)  {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
timer = map(val, 0, 1023, 0, 254);     // scale it to use it with the timer (value between 0 and 255)

// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < 13; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);

}

// loop from the highest pin to the lowest:
for (int thisPin = 12; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);

}
}

Pages: