Interrupts and Delays

So I have a piece code:

#include <TimerOne.h>

int led[] = {2,3,4,5,6,7,8,9,10,11};
int lever = 12;
int button = 13;

int buttonState = 0;
int buttonDelay = 0;
bool buttonAct = true;

int leverState = 0;

int fadeAmount = 2;
int fadeDelay = 0;

int modeCount = 0;
int maxCount = 4;

int counter = 0;
bool counterFl = true;
bool counterSwitch = true;

void setup() {
  Serial.begin(9600);
  Serial.println("Setup");
  //Input
  pinMode(button,INPUT_PULLUP);
  pinMode(lever,INPUT_PULLUP);  
  Timer1.initialize(1500);
  Timer1.attachInterrupt(inputInterrupt);
  for(int i = 0; i<10; i++){
    pinMode(led[i],OUTPUT);
    Serial.println("Finish");
  }

}

void loop(){
  modeToggle();
}

void modeToggle() {
  if(modeCount == maxCount){
    modeCount = 0;
  }
  if(buttonAct == false){
    Serial.println(modeCount);
    buttonDelay++;
    if(buttonDelay == 100){
      buttonDelay = 0;
      buttonAct = true;
    }
  }
  if(leverState == HIGH){
    if(modeCount == 0){
      ledRun();
    }
    if(modeCount == 1){
      ledOn();
    }
    if(modeCount == 2){
      fade();
    }
    if(modeCount == 3){
      blinkLeds();
    }
  }
  else{
    for(int i = 0; i<=10; i++){
      digitalWrite(led[i],LOW);
    }
  }
}

void fade(){
  for(int i = 0; i<=10; i++){
    digitalWrite(led[i],HIGH);
  }
  delayMicroseconds(fadeDelay);
  for(int i = 0; i<=10; i++){
    digitalWrite(led[i],LOW);
  }
  delayMicroseconds(fadeDelay);
  fadeDelay = fadeAmount + fadeDelay;
  if(fadeDelay == 1500 || fadeDelay == 0){
    fadeAmount = -fadeAmount;
  }
  //Serial.println(fadeDelay);
}

void ledOn(){
  for(int i = 0; i<10; i++){
    digitalWrite(led[i],HIGH);
  }
}

void blinkLeds(){
  if(counterFl){
    if(counterSwitch){
      for(int i = 0; i<10; i++){
        digitalWrite(led[i],HIGH);
      }
      counterSwitch = false; 
    }
    else{
      for(int i = 0; i<10; i++){
        digitalWrite(led[i],LOW);
      }
      counterSwitch = true;
    }
    counterFl = false;    
  }
  else{
    counter++;
    //Serial.println(counter);
    if(counter==50){
      counter=0;
      counterFl=true;
    }
  }
}

void ledRun(){
  for(int i = 1; i<10; i++){
    digitalWrite(led[i],HIGH);
    digitalWrite(led[i-1],LOW);
    delay(50);
  }
  for(int i=8; i>-1; i--){
    digitalWrite(led[i],HIGH);
    digitalWrite(led[i+1],LOW);
    delay(50);
  }
}


void inputInterrupt(){
  if(buttonAct){
    buttonState = 1-digitalRead(button);
  }
  if(buttonState == HIGH){
    modeCount++;
    buttonState = 0;
    buttonAct = false;
  }
  leverState = 1-digitalRead(lever);
}

The idea is for a 10LED strip to be switched through different modes for a PC build. Did a sketch before where I didn't use the timer1 library and was horrendously inefficient.

The problem with the interrupts on this one is that whenever I have a "mode" (currently 'void ledRun()') the delays seem to confuse the arduino and it won't go to the inputInterrupt loop when I try to switch the mode with my button.

I've gotten around it one way with the Led flash with a counter, but seems too much code to try it again. So I am wondering if there is a better way to get round this, or why is the delays affecting my interrupts?

You should not be using an interrupt to read a button or switch.

This suggests you do not understand what interrupts are.

If your code which didn't use the timer1 library was horrendously inefficient, you should have posted that code here for correction. Whatever the timer1 library is useful for, it almost certainly is not your application.

You have not provided a diagram of your connections.

You should not be using an interrupt to read a button or switch.

I see no good reason why an interrupt should not be used to read a button. Of course, if you don't do a careful job of debouncing, you'll get an interrupt interrupting your interrupt, and then things get funny.

I have had occasions where the code's main function was to keep track of an incoming data stream with no available buffering, from a raw OOK receiver, so I didn't want to take time out for polling a button. The interrupt in that case was the right choice.

However, let's say I read a button on an interrupt pin, as follows.

unsigned long later = 0;                   //global variable, or static if declared in loop()
 


void myButtonPush()                       //the interrupt routine
{
    
    if (later  < millis())
   {
       //execute your interrupt code
   }
   later = millis() + 1000;                 //provides 1 sec of debounce time
}

Your code may fail when a button press happens near millis() wrap.

The problem with the interrupts on this one is that whenever I have a "mode" (currently 'void ledRun()') the delays seem to confuse the arduino and it won't go to the inputInterrupt loop when I try to switch the mode with my button.

As Paul says, you don't seem to understand interrupts. They return to exactly the same place in the main code as they were just before the interrupt happened. So if you get an interrupt at the start of a delay period, after the interrupt code has run it goes back exactly to the same point in the delay and continues the delay. That is how interrupts work and that is what is happening with your code.

Your ledRun function has 18 x 50ms delays ... so the CPU is stopped 900ms+ before it can even check if the mode has changed. I guess this is why you have debounce set to 1 sec?

Try rewriting using time intervals with millis() instead of delay.

Paul__B:
You should not be using an interrupt to read a button or switch.

So I have a switch and a button on pin 12 and 13 respectively, how would you read each input for immediate change in the 'mode selection'?

Define "immediate"

how would you read each input for immediate change in the 'mode selection'?

Basically you can't with the code you have, you have to write your code as a state machine.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0