Arduino blink without delay issue

Thread Starter

spuddo

Joined May 2, 2013
72
Hi , All
The attached code is my attempt to include a pot to vary the rate of blink.
The aim was to control the rate between 1Hz and 20Hz.
The code produced a swing between 1Hz and 4.2 KHz ??
Checked hardware connections , all seemed OK.
Any thoughts?

Code:
// constants won't change. Used here to set a pin number :
const int ledPin =  11;      // the number of the LED pin
const int Potpin =  A1;

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;          // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.

  // read the input on analog pin A1:


  int interval = analogRead(A1);            //this bit added to control
  int Potpin = map (A1, 0, 1023, 500 , 25); //rate of LED
 
unsigned long currentMillis = millis();


  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
 
Last edited:
Top