Why won't this mod work?

Hi folks, just trying to get the hang of C for Arduino. I want to change this sketch so that the LED blinks every 1.5 seconds but only for 100 milliseconds.
I thought the following modification would work but it isn't changing the rate for some reason?

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.
  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;
      long interval = 100; 
    } else {
      ledState = LOW;
      long interval = 1500; 
    }

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

Google "scope C++"

1 Like

Since you have the long data type specifier in front of the interval there, that is a local variable so is only visible inside of the if block. It is a totally different variable that the one declared above setup() and used in the millis() timer.

A link to a tutorial for what @anon73444976 mentions.

1 Like

Does this mean you want the LED on for 100ms or off for 100ms?
Paul

1 Like

Thank you for that! I got confused with the fact that interval is, as far as I was concerned at the time, declared outside of the "if" statement. I am still not sure how the two are connected but disconnected lol... I'll read your suggestion though thanks.

I am accomplished at assembler and have loved the challenges programming pics has provided, but realise how much easier things are in the arduino environment.

However, I am struggling wrapping my head around what to me is quite alien, and not very obvious!! Assembler is what it is, but C seems quite abstract to me at this time!

Hopefully it will all come good in the end lol

On for 100ms, off for 1500. I think the chaps above have helped get it working though thanks

They aren't. The global one has the same name but is a different location than the local one. Changing the value of the local one does nothing to the global one.

It is a good idea to use a naming convention to keep from defining a local variable with the same name as a global. I like to use a capital letter as the first letter of a global and a lowercase letter as the first letter of a local.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.