Help with Interrupt needed

Hello
I started playing with Arduino 5 months ago, therefore I am not a ‘noob’, but not an expert either. I’m from below average to average.

First off, I’ve set up an on-off switch that works. When it clicks down the pin state is HIGH, otherwise when It’s back up the pin state is LOW. The on-off switch is supposed to change a variable number (0 and 1). Why? Because in the loop() function, It’s supposed to make a LED Diode turn on and off automatically when 5 seconds have passed. (I did not use the delay()) I used a custom delay that I found. Anyway, if the variable is set to 0, the LED diode remains It’s current state, if the variable is set to 1, the LED diode will turn on and off every 5 seconds. I did read lots of pages about interrupt, I know why I should use it for my project (This is not the project, I only use this to test out interrupt). But it still confuses me. Anyway, the problem is that the LED doesn’t turn on and off at all. I used Serial.print to see the variable. It did change when pressing the switch but the LED just won’t work).

So, when I turn the switch on, It’s supposed to allow the LED to blink every 5 seconds, when I turn off the switch, It’s supposed to break the custom delay and immediately start the code over again.

I hope you can understand me and excuse my bad English.
Thanks in advance.

Code

#define LED 23
#define SW 21

volatile byte xLed = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(SW, INPUT);

  digitalWrite(LED, LOW);

  attachInterrupt(digitalPinToInterrupt(SW), zeroxLed, FALLING);
  attachInterrupt(digitalPinToInterrupt(SW), onexLed, RISING);
}

void loop()
{
  if(xLed == 0)
  {
  }
  else
  {
    digitalWrite(LED, HIGH);
    myDelay(5000);
    digitalWrite(LED, LOW);
    myDelay(5000);
  }
}

void myDelay(int x)
{
  for(unsigned int i = 0; i <= x; i++)   
  {
    delayMicroseconds(1000);
  }
}

void zeroxLed()
{
  xLed = 0;
}

void onexLed()
{
  xLed = 1;
}

I did read lots of pages about interrupt, I know why I should use it for my project

Interrupts are NOT for reading switch states when the switch is pressed by a human.

I did not use the delay()) I used a custom delay that I found.

It is still a delay.

The blink without delay example shows how to blink an LED without using delay() (or any form of delay()).

Adding code to that sketch to read a switch state is trivial. See the state change detection example.

Blinking, or not, the LED based on whether the switch is (or has become) pressed, is trivial.

You don't need interrupts for a human operated switch, but perhaps this is for learning.

xled is a variable, not the pin connected to the LED.

I hope that you were NOT using Serial.print in an ISR.

I think that only one attachInterrupt can be used per pin, but I may be wrong.

All of those delay(...) and myDelay(...) are going to cause you issues in the long run. Nothing can happen immediately.

I think that only one attachInterrupt can be used per pin, but I may be wrong.

You are not.

It's still a little bit confusing.

I thought I can use Interrupt to change something immediately without waiting the delay?

I hope that you were NOT using Serial.print in an ISR.

No

I thought I can use Interrupt to change something immediately without waiting the delay?

You can. But, you can't change what loop() is doing. If loop() is in a delay(), it won't recognize that you changed something until the delay is over.

So, DO NOT USE delay() or delayMicroseconds() or millis() and your own delay function. Period.

But can I change what the function called by loop is doing?

mcwEight:
But can I change what the function called by loop is doing?

From within the ISR? No.

Is there a way to actually do that?

For example we have a program that saves light from the light sensor on an sd card every 2 hours (For testing purposes we should use 5 seconds). Then there are options (Pushbuttons can be used) to change the frequency time. But when the button is pressed and it changes to 4 hours (10 seconds for testing) it needs to finish the current delay (2 hours need to pass) so it can start again at a new frequency, how do I break the delay?

Why don't you use the BlinkWithoutDelay example (as mentioned before) that comes with the IDE? As PaulS indicated, it should be trivial.

You're code is nearly there. Get rid of the interrupt and just read the button(s). Because the BlinkWithoutDelay does not use blocking delays, it will constantly read the buttons. So you can change interval value on the fly.

mcwEight:
how do I break the delay?

By not having one in the first place.

http://www.gammon.com.au/blink