Interrupts

Hello,

I'm working on a project that requires me to variably attenuate 5 volts with a potentiometer and monitor the wiper voltage. The monitored wiper voltage controls a delay in my program. Everything is working fine but I would really like to interrupt the delay when the analog level changes so the user can immediately see the change in delay time. Can someone direct me to a thread or link that helps me to program this kind of interrupt. I'm having having trouble finding an example that matches what I need.

Thank you

READ THIS POST
http://forum.arduino.cc/index.php/topic,17450.0.html

 int marker = 12;   // marker output pin
int aval = 0;      // analog value

void setup() {
  pinMode(marker, OUTPUT); // pin = output
  DIDR0 = 0x3F;            // digital inputs disabled
  ADMUX = 0x43;            // measuring on ADC3, use the internal 1.1 reference
  ADCSRA = 0xAC;           // AD-converter on, interrupt enabled, prescaler = 16
  ADCSRB = 0x40;           // AD channels MUX on, free running mode
  bitWrite(ADCSRA, 6, 1);  // Start the conversion by setting bit 6 (=ADSC) in ADCSRA
  sei();                   // set interrupt flag     // <==============USE THIS AFTER CHECKING VALUE FOR INTERRUPT CRITERIA
}

void loop() {
}

/*** Interrupt routine ADC ready ***/
ISR(ADC_vect) {
  bitClear(PORTB, 4); // marker low
  aval = ADCL;        // store lower byte ADC
  aval += ADCH << 8;  // store higher bytes ADC
  bitSet(PORTB, 4);   // marker high
}

http://playground.arduino.cc/Main/AVR

@Frenchfry222, Since you have NOT posted your code it is impossible to provide accurate information.

My guess is that you are using delay() for your timing. Delay() paralyses the Arduino until the time expires so it can't respond to any other inputs.

If so you need to change that to the technique in the Blink Without Delay example sketch.

Don't consider using interrupts unless you are an experienced programmer who knows how to debug them.

...R

Edit to add the missing "NOT"

I think you mean "Since you have NOT posted your code ...etc "

And the mantra I so often find it necessary to repeat - It is one of the most common misunderstandings for "newbies" to think that an interrupt is a means to alter the flow of the main program. In fact, the very opposite is the case, an interrupt is designed such that a brief but important operation which must and can be processed immediately will be performed without affecting the flow of the primary program in any way.

This is not to be confused with the consequence that something that the ISR does may later be consequential to the main program as it continues to process its various tasks. This is equally true of a multi-tasking OS where an interrupt may result in task-switching but nevertheless on resumption, the immediate course of each task is as a matter of definition, unaffected.


So ...

If you care to post your code, we will (most likely) be happy to explain the mistakes (such as using the "delay()" function in an actual application) you have made - in a constructive (believe it or not) fashion. :smiley:

I don't even thing that the OP means interrupt in its correct sense at all. I suspect it's more a case of analogRead() then an if depending on the value returned.

Mark

The monitored wiper voltage controls a delay in my program

Everything is working fine but I would really like to interrupt the delay when the analog level changes so the user can immediately see the change in delay time.

It appears the OP wants to generate a delay when the analog value reaches a predefined value and the interrupt is intended to allow him to do that .
@OP,
How about providing the level change criteria ?

Thank you for your help! I'm sorry I did not post my code. I don't have internet at my house and had to post on my phone. My code is very simple and it uses delay(). I'm very interested in the hardware code given by raschemmel but I think I just need to use the blink without delay scenario.

Thank you very much

Thank you again for your help. My brother in law found a traffic light somehow and he wants it working in his backyard. There is no control box so i told him I would put one together with an Uno and a Keyes funduino relay board. I am putting it all in an outdoor box with a potentiometer that controls the light sequence from between 1 and 15 seconds. Here is the code I managed to put together:

unsigned long time = 0;
unsigned long seconds;
int level;
int val;
int ACgreen = 12;
int ACyellow = 11;
int ACred = 10;
int pot = 0;
int index = 1;
int trigger = 1;

void setup() 
{                
  pinMode(ACgreen, OUTPUT);     // outputs to each relay
  pinMode(ACyellow,OUTPUT);
  pinMode(ACred,OUTPUT);  
}

void loop() 
{
  val =analogRead(pot);         // acquire the pot voltage
  level = map(val,0,1023,1,15); // calibrate the level, 1-15
  seconds =level*1000;          // convert to seconds
  if((millis()-time)>seconds)   // check: is time exceeded 
  {index++;                     // increase index when time exceeded
   trigger=1;                   // set trigger
   time=millis();               // reset time
   if(index==4)                 // reset index after 3 cycles
   {index=1;
   }
  }
  
  if(trigger)                   // procced if trigger is set
  {                         
    if(index==1)                // green light states 
    {digitalWrite(ACgreen, HIGH);
     digitalWrite(ACyellow, LOW);
     digitalWrite(ACred, LOW);
     trigger=0;                 // unset trigger
     
    }
    if(index==2)                // yellow light states 
    {digitalWrite(ACgreen, LOW); 
     digitalWrite(ACyellow, HIGH);
     digitalWrite(ACred, LOW);
     trigger=0;
     
    }
    if(index==3)                // red light states 
    {digitalWrite(ACgreen, LOW);
     digitalWrite(ACyellow, LOW);
     digitalWrite(ACred, HIGH);
     trigger=0;
     
    }
  }
}