Interrupts

I am new to the Arduino and I am trying to do a very basic thing.
I am slowly cross fading 4 Leds to make lots of colour combinations.
When I see one I like I want to press a button to halt the sequence where it is.
Then if I press the same button again it will continue from where it halted.
Please explain how to do this and include the code.
I've read so many posts about interrupts but they just don't make any sense to me.
Thanks in advance... please help I'm going round in circles
john

I've read so many posts about interrupts but they just don't make any sense to me.

Don't worry, you are probably better off not using interrupts to do what you want.

If you have the cross fading being done in a function (like loop) you can check if buttons are pressed before each change in the fade (each repeat of the function that does the fade). If a button is pressed you can stop the fade and wait for the button to be released.

You can even have buttons that change the behaviour of the fade by changing settings that are read and used as the fade progresses.

Thanks for the reply.
I was thinking along those lines, but it would be good to have a very simlpe example of an interrupt.
The posts and reference take for granted a lot of knowledge of this system .
Thanks again
john

the reference takes for granted a lot of knowledge of this system

are you referring to the attachInterrupt reference? http://www.arduino.cc/en/Reference/AttachInterrupt

What additional information do you think is needed to improve that page?

This is a response to a PM from 344thorne:

The note on the page Attachinterrupt does not mean much to me "declare as volitile..."???

The page say: “You should declare as volatile any variables that you modify within the attached function.”
The word volatile means capable of rapid change. The reason for this has to do with the way the compiler optimizes code and it can get confused if you don't indicate that the variables value can be changed at any time by the interrupt.

What is blink...does this mean void blink() can be called any time or that any void ****() is a
Label that you can jump to and back from???

The void in front of that function is its return type – void means nothing is returned. Blink is called whenever an interrupt occurs. It can be called by your sketch code as well if it makes sense to do so.

From the nature of your questions I think you are just getting started with programming. My advice is to avoid using interrupts until you are more familiar with the basic concepts. Interrupts can be very tricky to debug (even for experience programmers) and they are not much covered in introductory books because 99 times out of 100 there is a much easier way to solve the problem.

When you are familiar with writing sketches, creating your own functions, and handling the logic of more than one thing happing in your sketch it will be easier to grasp the principles of using interrupts.

Re: void.

Strictly speaking, any interrupt service routine must be void.
An interrupt service can't know when and from where it will be called, so returning a value makes no sense.*

  • Though of course, nested functions called from the interrupt service routine can return values.

I agree with mem, and remember a textbook on microprocessors from the early 1980s, that started each chapter with a quote.
The chapter on interrupts started something like "Interrupts are difficult and dangerous. Their use should be avoided at all costs".
;D

Can you show me the code to do the following without using an interrupt:
I am in a void loop I want to press a push button to hold the loop at the point I pressed the button.
At the next press of the button the loop continues from the piont it halted.
The loop would be fading up or down leds.
thanks
john

Show us what you've got, and we can help you do what you want.
This kind of thing crops up here fairly regularly.

Hi
I am away from home at the moment I will show you my code when I get back.
Y our help is much appreciated.
I didn't realize Arduino code was c/c++...wiring. Some learning to do.
john

Hi again
This is the code I am running
I would like to add a push button on pin 2 ( it is debounced) to hold the program where ever it is on the first press
and to continue from the same point on the next push.
Hope this makes sense
john

// Cross fading leds with Random delays


int value = 0;                                // variable to keep the increment value 
int green = 6;                                // led connected to digital pin 6
int yellow = 9;                               // led connected to digital pin 9
int red =10;                                  // led connected to digital pin 10
int blue =11;                                 // led connected to digital pin 11
int x =50;                                     // sets delay for slow fade
long z;
long randNumber;


void setup() 
{   Serial.begin(9600);
    Serial.println(" Fade in Yellow to start");
    
         for(value = 0 ; value <= 255; value+=1)        // fade up yellow to start
                    { analogWrite(yellow, value);       // Y
                                       delay(x); }    
} 

 
void loop() 
{ 
    randomSeed(analogRead(0));
// 1
        Serial.println(1);
        for(value = 0 ; value <= 255; value+=1)        // increment value in steps of 1
                     { analogWrite(blue, value);       // B Y
                                       delay(x); }     // waits for x milli seconds to see the dimming effect 
   
                     z = random(5000,8000);            // random number 
                                  delay(z);            // random delay between light cues
                         Serial.println(z);
                         
  
 // 1a
         Serial.println(1.5);
         for(value = 255 ; value >= 0; value-=1)        
                     { analogWrite(yellow, value);     //  blue      fade the yellow from previous cycle
                                       delay(x); }     
   
                      z = random(5000,8000);                            // random number 
                                   delay(z);                            // delay between light cues
                          Serial.println(z);
     
 // 2
         Serial.println(2);
         for(value = 0 ; value <= 255; value+=1)        
                      { analogWrite(red, value);                   // R B
                                       delay(x); }                 
      
                          z = random(5000,8000);
                                       delay(z);
                               Serial.println(z);
  
 // 3
         Serial.println(3);
         for(value = 0 ; value <= 255; value+=1)        
                   { analogWrite(yellow, value);                   //R B Y
                                       delay(x); }                 
   
                           z= random(5000,8000);
                                       delay(z);
                              Serial.println(z);
   
    
 // 4
         Serial.println(4);
         for(value = 0 ; value <= 255; value+=1)        
                     { analogWrite(green, value);                   // R B Y G
                                        delay(x); }                 
    
                           z= random(5000,8000);  
                                       delay(z);   
                              Serial.println(z);  
    
  // 5
         Serial.println(5); 
         for(value = 255 ; value >= 0; value-=1)        
                       { analogWrite(red, value);         // fade B Y G
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);                
    
 // 6
         Serial.println(6);
         for(value = 255 ; value >= 0; value-=1)        
                    { analogWrite(yellow, value);       // B G
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);                
    
 // 7
         Serial.println(7);
         for(value = 0 ; value <= 255; value+=1)        // B R G
                       { analogWrite(red, value);       
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);   

 // 8
         Serial.println(8);
         for(value = 255 ; value >= 0; value-=1)        //  G
                       { analogWrite(red, value);  
                        analogWrite(blue, value);
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);  
                               Serial.println(z);
 //9
         Serial.println(9);
         for(value = 0 ; value <= 255; value+=1)        //  R G
                       { analogWrite(red, value);
                       analogWrite(green, value);       
                                        delay(x); }   
                   
                           z= random(5000,8000);  
                                       delay(z);   
                              Serial.println(z);  

 //10
         Serial.println(10);
         for(value = 0 ; value <= 255; value+=1)        //  R Y G
                    { analogWrite(yellow, value);
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);  

// 11
        Serial.println(11);
        for(value = 255 ; value >= 0; value-=1)        //  R Y
                    { analogWrite(green, value);  
                                       delay(x); }   
                   
                           z= random(5000,8000);  
                                       delay(z); 
                              Serial.println(z);                                      
     
// 12
        Serial.println(12);
        for(value = 255 ; value >= 0; value-=1)        //  R 
                   { analogWrite(yellow, value);  
                                       delay(x); }   
                   
                           z= random(5000,8000);  
                                       delay(z);
                              Serial.println(z);                                       
              
 //13
         Serial.println(13);
         for(value = 0 ; value <= 255; value+=1)        //  R B Y 
                      { analogWrite(blue, value);
                      analogWrite(yellow, value);
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);   
 
 // 14
         Serial.println(14);
         for(value = 255 ; value >= 0; value-=1)        //  Y 
                      { analogWrite(blue, value);
                         analogWrite(red, value);  
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);  
                               Serial.println(z);
                               
 //15
         Serial.println(15);
         for(value = 0 ; value <= 255; value+=1)        //  Y G
                     { analogWrite(green, value);
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);   
                               Serial.println(z);    
                             
 //16                            
                               Serial.println(16);
         for(value = 255 ; value >= 0; value-=1)        //  Y 
                      { analogWrite(green, value);
                         
                                        delay(x); }   
                   
                            z= random(5000,8000);  
                                        delay(z);    
 
                               Serial.println(z);
                 Serial.println("Back to Start");
  
}

An easy way to implement that is to write a function that handles both the delays and checks the state of the button.

The following will read the switch state. When not paused, the first debounced press will pause and wait for another press before continuing

Pseudocode for myDelay (unsigned long ms) function :

Get the debounced button state
If the button is pressed then:
wait for the button to be released
wait for the button to be pressed again
return from myDelay
else
if the button is not pressed then delay 1 millsecond
repeat from the start for as many times as you need to get a total delay for the given number of ms.

Thanks but I don't understand
Could you show me the actual code and where it would go In my program.
I am new to this language.

I suggest you start with a simple test sketch that verifies the switches are correctly debounced. Do you think you can write a sketch just prints a single message each time the button is pressed and released?

For now, don't worry about including any other functionality

I wrote a sketch to turn on one of the leds in my circuit when I pressed the switch and to go off when I released it.
It works fine.
I use the same debounce circuit on lots of other projects using picaxe or stamp chips.
It's the syntax and language of the Arduino that I'm having a problem with.
It will take a while to get my head around it, but I need to get this done soon.
I have the circiut built with a switch on pin 2 and my led strings being fed via darlington drivers.
It all works fine with nice crossfades, I will alter the timmings later.
I just need to incorporate the switch.
Thanks
john

Using mem's outline:

Pseudocode for myDelay (unsigned long ms) function :

while (ms) {
Get the debounced button state
If the button is pressed then {
wait for the button to be released
wait for the button to be pressed again
return from myDelay
} else {
(the button isn't pressed, so) delay 1 millsecond
subtract one from "ms"
}
}

Starting to look like C?

Sorry
I'm not sure what is a comment and what is code.
Where do I put exact bits of code into my sketch to get the button to work.
Once it's working I may be able to figure out how it works.
confused???

Not wanting to waste your time or mine, could you say if you have an interest in learning how to achieve the functionality you want, or are you hoping that someone will write your sketch for you?

It's mostly code - if it were much more code, it'd be written, and that's cheating. :wink:

We can't write it, because we don't know what your switch reading stuff looks like.
All you need to do is pad out the definition of "myDelay", test it in a very simple sketch (a simple blinking LED should do the trick), and replace all calls to "delay" with calls to "myDelay".

Hint: "wait for the button to be released" usually translates to a "while () {}" or a "do {} while ()" loop.