Mega 2560 led blink with pot analogwrite or digitalwrite?[solved]

Hi, first post here. XD I'm a noob please go easy lol. Lets say I want to use a pot to control blink rate for onboard LED on pin13. When I say control though I mean when pot is at 0 the led is off and 1023 is always on then duty cycle would be calculated inbetween. My idea was to use analogRead/analogWrite but I want to see the led actually blink not fade and frequency is way too high.

I'd like pulses to be 100ms+ or so. It could even be a bit longer.I tried to adjust frequency of timers themselves then using analog to no avail. Tried with different timers as well. Result always was led off until about 80% on the pot then led on. No in between.

I've also tried using digitalWrite and that actually worked although, at 0 on the pot the led would blink 1 second off and 1 second on using the code I sent to arduino which is 50% duty cycle in my messed up head.

Here's a video showing about what I'm trying to do. Just imagine the lamp is led on pin13 so we don't go off topic..

http://www.youtube.com/watch?v=2gwSoNfCYco

I can't post code since I dumped the 100 different things I tried.... :( If anyone has a good idea for my goad I would really appreciate it! THANKS!

I should've posted this in LED category sorry guys. If someone could move it that would be awesome.

Or in the Programming Questions.

The normal PWM signal with analogWrite() is about 450Hz. Directly using the timers might conflict with Arduino libraries. I would not recommend it. You can use the delay() and millis() function.

There is a map() function which can translate one range to another. Once you learn how to use it, it is a very handy function.

http://arduino.cc/en/Reference/delay http://arduino.cc/en/Reference/millis http://arduino.cc/en/Reference/map

// example code, not tested.

const int time_total = 2000;      // milliseconds for one PWM phase

...

void loop()
{
  int time_on;
  int pot;
  int led;

  pot = analogRead(pinPot);
  // map the analog input range 0...1023 to a delay range 0 ... 2000 ms
  time_on = map (pot, 0, 1023, 0, time_total);

  digitalWrite( pinLed, HIGH);
  delay(time_on);

  digitalWrite( pinLed, LOW);
  // wait remaining time for the total time of one PWM phase
  delay(time_total - time_on);
}

If you need it to be fully off and on, the code should be extended to detect those conditions.

Hi, thanks for your reply. I see what you mean about using map and digitalWrite. I used your code and it worked. The weird thing is the led is very faint and I'm not sure why... If anyone has an idea about that I'm all ears lol. It does work perfectly though thanks again!

const int time_total = 2000;      // milliseconds for one PWM phase
  int time_on = 0;
  int pot = A0;
  int led = 13;
  int potVal = 0;

void setup(){
}
void loop() {
   

  potVal = analogRead(pot);
  // map the analog input range 0...1023 to a delay range 0 ... 2000 ms
  time_on = map (potVal, 0, 1023, 0, time_total);

  digitalWrite( led, HIGH);
  delay(time_on);

  digitalWrite( led, LOW);
  // wait remaining time for the total time of one PWM phase
  delay(time_total - time_on);
}

The weird thing is the led is very faint

Nothing weird about it. INPUT pins WILL be dim. OUTPUT pins will be a lot brighter.

See, told you I was noob lol thanks.

pinMode(led, OUTPUT);

Works perfect.