# using a pot to control delay

how can i get this sketch to work so that the led has a delay controled by the pot, but it is turned on or off by the pt?

``````int ledpin = 13; // led is on pin 13
int pt = A1; // phototransistor is on pin analog 1
int val = 0; // place to store value from the phototransistor
int pot = A2; // pot is on pin analog 2
int val2; // place to store value from pot

void setup(){
pinMode(ledpin, OUTPUT); // declares the ledpin as an output
}

void loop(){

if  (val < 200) // checks to see if val is off
{
digitalWrite(ledpin, LOW); // turns led off
}
else
{
digitalWrite(ledpin, HIGH); // turns led on
delay(pot); // delays how long the led is off
}
}
``````

It seems you are calling delay with the analogue pin 2 name, try

delay(pot); delay( val2 );

that help thanks. is there a way i can make it so that the val2 will equal a delay like have the pots input be say like the pot is in the middle of its range and then have that equal X amount the time of delay?

jareeb:

``````     delay(pot); // delays how long the led is off
``````

You probably meant:

``````     delay(val2); // delays how long the led is off
``````

In other words, the delay is determined by the value read from the analog input, not by the pin number. I don’t know whether the analog input value will be exactly what you need - you might need to scale it up or down to get the duration you want for a given pot position.

oh alright

Your pot should be returning a value between 0 and 1023. You should use the map() function to map this to the range of time that you need. For example, if you want a delay between 0 and 3 seconds, then use map to change the range (0,1023) to (0,3000) milliseconds.

You can look up the syntax for the map() function in the online help.

thanks that was what i was looking for.

i dont think its working right.

``````int ledpin = 13; // led is on pin 13
int pt = A1; // phototransistor is on pin analog 1
int val = 0; // place to store value from the phototransistor
int pot = A2; // pot is on pin analog 2
//int val2; // place to store value from pot

void setup(){
pinMode(ledpin, OUTPUT); // declares the ledpin as an output
}

void loop(){
val2 = map(pot, 0, 1023, 10, 40000);
val2 = constrain(pot, 10, 40000);

if  (val < 200) // checks to see if val is off
{
digitalWrite(ledpin, LOW); // turns led off
}
else
{
digitalWrite(ledpin, HIGH); // turns led on
delay(val2); // delays how long the led is off
}
}
``````

int is a signed value that ranges from -32768 to 32767, so setting it to 40,000 is not going to work.

Either change the variable to unsigned int or long, or make the range smaller (say 30,000).

its still not working.

Carefully look at this code

``````  int val2 = analogRead(val2); // reads the value from the pot
val2 = map(pot, 0, 1023, 10, 40000);
val2 = constrain(pot, 10, 40000);
``````

What is the value of val2 at the end?

its 40000, but i did change it to 30000 and it still didnt work. even lower values dont work.

What pin are you reading here

``````int val2 = analogRead(val2);
``````

What value are you mapping here

``````val2 = map(pot, 0, 1023, 10, 40000);
``````

What value are you constraining here

``````val2 = constrain(pot, 10, 40000);
``````

Do you see the problem?

marco_c:
What pin are you reading here

``````int val2 = analogRead(val2);
``````

What value are you mapping here

``````val2 = map(pot, 0, 1023, 10, 40000);
``````

What value are you constraining here

``````val2 = constrain(pot, 10, 40000);
``````

Do you see the problem?

no.

``````int ledpin = 13; // led is on pin 13
int pt = A1; // phototransistor is on pin analog 1
int val = 0; // place to store value from the phototransistor
int pot = A2; // pot is on pin analog 2
int val2; // place to store value from pot

void setup(){
pinMode(ledpin, OUTPUT); // declares the ledpin as an output
}

void loop(){
val2 = map(pot, 0, 1023, 10, 3000);

if  (val < 200) // checks to see if val is off
{
digitalWrite(ledpin, LOW); // turns led off
}
else
{
digitalWrite(ledpin, HIGH); // turns led on
delay(val2); // delays how long the led is off
}
}
``````

Perhaps this will help -

``````const uint8_t pinLED   = 13;
const uint8_t pinLDR   = A1;   // Light Dependent Resistor (Photo-Resistor)
const uint8_t pinPOT   = A2;   // Potentiometer

const uint8_t LED_OFF  = LOW;
const uint8_t LED_ON   = HIGH;

void loop()
{
unsigned long   tmDelay = map(valPot, 0L, 1023L, 10L, 40000L);

if ( valLDR < 200 )
{
digitalWrite(pinLED, LED_OFF);
}
else
{
digitalWrite(pinLED, LED_ON);
delay(tmDelay);
}
}

void setup()
{
pinMode(pinLED, OUTPUT);
}
``````

pot is defined as A2, the pin you want to read; val2 is where you want to store the value.

So the code should look like

``````  int val2 = analogRead(pot); // reads the value from the pot <-- the comment was not what the code was doing
val2 = map(val2, 0, 1023, 10, 30000);
val2 = constrain(val2, 10, 30000);
``````

lloyddean has cleaned it up a lot and also improved the formatting.

Using variable names that are descriptive can often stop you making these sort of mistakes as what you read will not look right.