What is wrong with my program??

I am trying to write a program for a pot to control how long the light on (10 sec to 2 min) and another pot to control the brightness (not on at all to full brightness). The light starts when ever you hit a tact switch and will stay on according to how the pot is set, and for what ever brightness. I also what the brightness and time to be changed whenever (even when the led is on). I have written some code but the code will not let the led even come on.

Thanks, Austin

here is what i have so far

int lightPin = 11;//my led
int lightBright = A1;//my pot to controll the brightness
int lightTime = A2;//my pot to controll the time it is on
int lightReset = 2;//my tact switch to turn on the light
int timeA = 0;//the time befor it is mapped for time length
int bright = 0;//the brightness
int timeB = 0;//the time after it is mapped for time length

void setup()
{
  pinMode(lightPin, OUTPUT);
  pinMode(lightBright, INPUT);
  pinMode(lightTime, INPUT);
  pinMode(lightReset, INPUT);
  
}

void loop()
{
 bright = analogRead(lightBright);
 timeA = analogRead(lightTime);
 timeB = map(timeA,1,10000,1023,120000);

if (lightReset == HIGH)
{
  analogWrite(lightPin, bright);
  delay(timeB);
  analogWrite(lightPin, 0);
}
else
{
 analogWrite(lightPin, 0);
} 

  
}
if (lightReset == HIGH)

is the same as

if (2 == HIGH)

You need to read the input, probably

if (digitalRead(lightReset) == HIGH)

if you have a switch on that pin that is connected to GND with a pullup resistor.


Rob

 timeA = analogRead(lightTime);

timeA will be a value between 0 and 1023.

 timeB = map(timeA,1,10000,1023,120000);

About 90% of your from range will never be covered by the input value, so 90% of your to range will never be covered. Which is just as well, since 75% of your from range won't fit in an int anyway.

How would i have a light stay on for 2 min then?

How would i have a light stay on for 2 min then?

Assign the value 1000L * 60L * 2L To a long or unsigned long variable

this is what i have for my map am i doing it correctly it stays on for alot longer than 2 mins.

 timeB = map(timeA, 1, 1023, 1000, 1000L * 60L * 2L);

And how do get it so i can adjust the brightness and time when the light is on?

If the idea is to define how long an LED is on, based on a potentiometer reading, with a range of 10 seconds to 2 minutes, your from range is wrong, as well as your to range. The from range should be 0 to 1023, not 1 to 1023. The to range should be 10000 to 120000L.

it stays on for alot longer than 2 mins.

Have you fixed the other issue in your code?

I got the time down but it still won't let me adjust the brightness when the light is on. How would I code that?

analogWrite does it for me. What does your code look like?

Have you fixed the other issue in your code?

was a hint for you to post that code.

Here is my most resent code. I really like all the help i'm getting. This is a very good community. Thanks

int lightPin = 11;//my led
int lightBright = A1;//my pot to controll the brightness
int lightTime = A2;//my pot to controll the time it is on
int lightReset = 2;//my tact switch to turn on the light
int timeA = 0;//the time befor it is mapped for time length
int bright = 0;//the brightness
int timeB = 0;//the time after it is mapped for time length

void setup()
{
  pinMode(lightPin, OUTPUT);
  pinMode(lightBright, INPUT);
  pinMode(lightTime, INPUT);
  pinMode(lightReset, INPUT);
  
}

void loop()
{
 bright = analogRead(lightBright);
 timeA = analogRead(lightTime);
 timeB = map(timeA, 0, 1023, 10000, 120000L);

if (digitalRead(lightReset) == HIGH)
{
  analogWrite(lightPin, bright);
  delay(timeB);
  analogWrite(lightPin, 0);
}
else
{
 analogWrite(lightPin, 0);
} 

  
}

Once you get rid of the delay, you'll be able to vary the brightness.

How do I get it to be able to adjust the brightness whenever I want with the delay.

Have a look at the blink without delay example.

Conveniently linked here :D

I'm sorry I don't understand how to implicate that in to my program I am a huge newbie when it comes to programs. And when i set my pot to 2 min it never goes off.

Ok - here's some code I've quickly knocked together to try an illustrate a way to track time without using the delay function. It's in no way the best approach, and I haven't tested it other that to check it compiles - but the idea is about right. I like the approach of 'learning by examples' so I've tried to comment as much as I can - but it would be good for you to read over the Reference and Learning pages as well for additional help.

One example is that an integer(int) can only hold a maximum value of 65,535 when it's unsigned and +/-32,768 when it's signed, so trying to hold 2 minutes in milliseconds would exceed this.

As to why the LED isn't turning off - without seeing how you've connected the hardware together there could be a few things, one could be the switch itself is always returning high. How have you got everything hooked together?

Hope this helps

(PS - I'm at work so this is all off the top of my head ;))

int lightPin = 11;//my led
int lightBright = A1;//my pot to controll the brightness
int lightTime = A2;//my pot to controll the time it is on
int lightReset = 2;//my tact switch to turn on the light
int timeA = 0;//the time befor it is mapped for time length
int bright = 0;//the brightness

// This needs to be of type long to be able to hold a value of 2 minutes in seconds
// the unsigned bit tells us we want the value to be for 0 -> LONG_MAX instead of
// -LONG_MAX/2 -> LONG_MAX/2. For more on the read about the data types on the reference page
// http://arduino.cc/en/Reference/HomePage
unsigned long timeB = 0;//the time after it is mapped for time length

// We will use this one so we can keep track of how long the LED has been on for
unsigned long timeAtReset = 0;

void setup()
{
    pinMode(lightPin, OUTPUT);
    pinMode(lightBright, INPUT);
    pinMode(lightTime, INPUT);
    pinMode(lightReset, INPUT);
}

void loop()
{
    bright = analogRead(lightBright);
    timeA = analogRead(lightTime);

    // Here we grab the current time
    unsigned long currentTime = millis();

    // so here we have mapped our pot value to a time between 10s and 2m(120s)
    // and the idea is we want to be able to change it at any time, So we need to keep updating it
    timeB = map(timeA, 0, 1023, 10000, 120000L);


    // If the button is pressed (this will include being held down btw)
    if (digitalRead(lightReset) == HIGH)
    {
        // Turn the LED on
        analogWrite(lightPin, bright);

        // Set the time we started
        timeAtReset = millis();
    }
    // Otherwise if the button is not pressed/held AND the current time is greater
    // than the time the LED was switched on + the mapped time
    else if(currentTime > timeAtReset + timeB)
    {
        // Turn LED off
        analogWrite(lightPin, 0);
    } 
}
 bright = analogRead(lightBright);

This function returns a value between 0 and 1023.

  analogWrite(lightPin, bright);

This function expects a value between 0 and 255, as the second argument.

if (digitalRead(lightReset) == HIGH)
{
  analogWrite(lightPin, bright);
  delay(timeB);
  analogWrite(lightPin, 0);
}
else
{
 analogWrite(lightPin, 0);
}

After the delay(), what happens? The light pin is turned off, loop() ends, loop() gets called again, and the light pin is turned back on for a different period of time. The off time is never more than a few hundred nanoseconds.

I have been trying a new way to make this work and now i have the brightness were i can adjust it when ever i want but the Led never shuts off. I used a while control structure and and putting a 4 milliseconds delay (small enough were you can notice it when you adjust the brightness) and then i just divided my light time by 4 in the map. but it keeps cycling thought the loop. how do i get it to stop and shut off when i want it to

int lightPin = 11;//my led
int lightBright = A1;//my pot to controll the brightness
int lightTime = A2;//my pot to controll the time it is on
int lightReset = 2;//my tact switch to turn on the light
int timeA = 0;//the time befor it is mapped for time length
int bright = 0;//the brightness
int timeB = 0;//the time after it is mapped for time length
int var;
int brightM;


void setup()
{
  pinMode(lightPin, OUTPUT);
  pinMode(lightBright, INPUT);
  pinMode(lightTime, INPUT);
  pinMode(lightReset, INPUT);
  
}

void loop()
{
timeA = analogRead(lightTime);
timeB = map(timeA, 0, 1023, 2500, 30000L);

if (digitalRead(lightReset) == HIGH)
{
var = lightReset;
while(var < timeB) {
bright = analogRead(lightBright);
brightM = map(bright, 0, 1023, 0, 255);
analogWrite(lightPin, brightM);
delay(4);
} 
}
else
{
analogWrite(lightPin, 0);
}  
}
var = lightReset;

lightReset is a pin number. Why are you setting var to it? This is why we keep emphasizing using meaningful names for variables. If lightReset were actually lightResetPin, and var and timeA and timeB had meaningful names, it would be much more obvious that this was a mistake.

Ok this is my latest code the led will not turn on at all can anyone help
Thanks,

int lightPin = 11;
int lightBright = A1;
int lightTime = A2;
int lightReset = 2;
int timeA;
int bright; 
int x;
int timeB;

void setup()
{
	pinMode(lightPin, OUTPUT);
	pinMode(lightBright, INPUT);
	pinMode(lightTime, INPUT);
	pinMode(lightReset, INPUT);
}

void loop()
{
	
  timeA = analogRead(lightTime);
  timeB = map(timeA, 0, 1023, 10000, 120000L);
  

      


if (lightReset == HIGH)
{
for(x = 0; x <= timeB; x++)
{
  bright = analogRead(lightBright);
  analogWrite(lightPin, bright);
  delay(1);
}
}
else
{
analogWrite(lightPin, 0);
}

}