Interrupts not working - can anyone help me?

This forum is for all topics related to Simulator for Arduino v0.98. Please include sample code where possible.

Moderator: Adrian

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Interrupts not working - can anyone help me?

Postby mikeworkman » Tue Apr 16, 2013 4:00 am

Simple Interrupt routine - V0.98 never seems to take the interrupt.

Here is the code:
__________________________________________________________________

Code: Select all

/*
 Analog Input from Temperature Sensor

Averages a variable number of samples to get a noise free Temperature reading of a hot water recirculation line.  Reading is put through a hysteritic loop to turn recirculation pump on or off. A couple of timers prohibit the pump for being on too long, and lock it out for a period of time once it is turned off.

The interrupt (0) is triggered by a pushbutton.  This sets either the low or high temperature set-point for the hysteric loop via the ISR.  Pushing the button toggles the next set point to be set. If multiple interrupts are received due to bouncing, nothing happens in the routine until a sample delay.

Created by Mike Workman

 */
unsigned long  Sample_Time = 90; // Sample interval in milliseconds (approx, doesn't include time for  A to D conversions)
int sensorPin = A0;             // select the input pin for the Thermistor-based temperature sensor
int Set_Temp_Input_Pin = 2;// Pressing a momentary switch wired to this pin raises it high, signalling you want
                                          // temperature point, either high or low set point, as indicated by Set_Point_Flag
int Recirculate_Pin = 5 ;       // Recirculate LED indicator driven off of pin 5
int Pump_Control_Pin = 13;  // select the pin for the Pump Control
int Sensor_Value = 0;           // variable to store the value coming from the sensor
int Number_of_Samples = 4;      // Total number of samples to average for determining Temperature, MAXIMUM = 32
int Temp_samples[32] = {0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,    0, 0, 0, 0,   0, 0, 0, 0,    0, 0, 0, 0 };
volatile int Temp =0;                     //
int index;                                // array index
int pump = 0;                           // Always start with Pump off.
int Sample_Index = -1;             // Initialize pointer to store most recent Temperature sample after incrementing.
volatile int Temp_Low = 500;   // Return temperature minimum set point
volatile int Temp_High = 600;  // Return temperature maximum set point
int Temp_accumulator = 0;                 // This is where we add up all the samples to average them
unsigned long Pump_On_Time_Max = 3000; // Number of milliseconds (300K or 5 minutes) pump can stay on without timing out (say heater is broken/off)
unsigned long Pump_Off_Lockout_Time = 6000 ; // Once Pump is on, it is locked out from coming on for this period of time (10 minutes = 600K milliseconds)
unsigned long Pump_Off_Elapsed_Timer = 0;     // Timer that reset when pump is turned off and starts counting time until turned on again.
unsigned long Pump_On_Elapsed_Timer = 0;      // Timer for most recent amount of time Pump has been on initialized to zero minutes
unsigned long Pump_On_Elapsed_Timer_Start_Point;  // Time mark for latest pump on event
unsigned long Pump_Off_Elapsed_Timer_Start_Point; // Time mark for latest pump off event
volatile int Interrupt_Latch = 0;         // This gets set by the interrupt routine. Once set it cannot get reset until a full sample time goes by.
volatile int Set_Point_Flag = 0;          // When flag is 0, set point set when button pushed will be low set point.
                                          // When flag = 1, setpoint when button is pushed will be the high set point.
volatile int Setpoint_Latch = 0;  // When this is 0 we perform a toggle and set operation, from then on - if bouncing - we wait one sample before reacting.
                 
void setup() {
  pinMode(Set_Temp_Input_Pin, INPUT);            // Temperature pushbutton is pin 2, Digital Interrupt 0
  pinMode(Recirculate_Pin, OUTPUT) ;               // LED Indicating Recirculation on/off
  attachInterrupt(0,set_and_toggle, FALLING);   // Interrupt 0, ISR is "set_and_toggle", on Falling edge of pin 2
  pinMode(Pump_Control_Pin, OUTPUT);            // declare the Pump_Control_Pin as an OUTPUT
  digitalWrite(Pump_Control_Pin, LOW);            // Pump off
  pump = 0;                                                      // Pump off when pump = 0
  Pump_Off_Elapsed_Timer_Start_Point = millis(); // initialize the pump off timer to "now"
  }

void loop() {
  Sample_Index = (++Sample_Index)%(Number_of_Samples); // Increment index pointer and have it circulate from 0-Number_of_Samples
  // read the value from the sensor:
  Sensor_Value = analogRead(sensorPin); 
  Temp_samples[Sample_Index] = Sensor_Value; // Store Sensor value in Sample Memory
  Temp_accumulator = 0;
  for (index = 0; index < Number_of_Samples ; index++ ) {
    Temp_accumulator = Temp_accumulator + Temp_samples[index]; // add up all the samples in the array
  }
  Temp = Temp_accumulator / Number_of_Samples;  // Compute the average temperature of Number_of_Samples
  if( pump == 0 ) {
    Pump_Off_Elapsed_Timer = millis() - Pump_Off_Elapsed_Timer_Start_Point; //  Length of time Pump has been off
    if( Temp <= Temp_Low ) {                                 // If we are ready for heat (Recirc) turn on the pump
      if(Pump_Off_Elapsed_Timer > Pump_Off_Lockout_Time) {   // Make sure pump has been off for awhile before turning back on
      pump = 1;
      digitalWrite(Pump_Control_Pin, HIGH);           // Turn the pump on and
      digitalWrite(Recirculate_Pin, HIGH) ;           // Turn the "Recirculate" LED on
      Pump_On_Elapsed_Timer_Start_Point = millis();   // Mark the TIme at which the Pump turned on
    }
   }
  }
  if(pump ==1) {
    Pump_On_Elapsed_Timer = millis() - Pump_On_Elapsed_Timer_Start_Point; // Length of time in mSec that the Pump has been running.
    if( Pump_On_Elapsed_Timer > Pump_On_Time_Max ) {
      pump = 0;
      digitalWrite(Pump_Control_Pin, LOW);             //  Turn pump off
      Pump_Off_Elapsed_Timer_Start_Point = millis();   // Mark the time we turned on the pump
      digitalWrite(Recirculate_Pin, LOW);              //  Turn off recirc LED
  }   
    if( Temp >= Temp_High ) {
      pump = 0;
      digitalWrite(Pump_Control_Pin, LOW);             //  Turn pump off
      digitalWrite(Recirculate_Pin, LOW);              //  Turn Recirc LED off
      Pump_Off_Elapsed_Timer_Start_Point = millis();   // Mark the time we turned off the pump
    }
  }
       
// Now wait for a delay and start again:
delay(Sample_Time);
Interrupt_Latch = 0;    //Clear the Interrupt Latch

}
// Interrupt from Switch button push causes the temperature reading to be stored as either the low or the high set point,
// Sets the low or high set point and toggles the Set point flag so the next push sets the other (high or low) set point.
// Note the interrupt latch debounces the switch using Sample_Time by only allowing one action per sample.
void set_and_toggle() {
  if(Interrupt_Latch != 1 ) {
  if( Set_Point_Flag == 0 ) {
    Temp_Low = Temp;
    Set_Point_Flag = 1;
  } else {
    Temp_High = Temp;
    Set_Point_Flag = 0;
  }
  if (Temp_High < (Temp_Low+10) ) {       // If this is the case the user made an error and set the high point lower than or too close to Temp_Low
    Temp_High = Temp_Low + 10;            // Do not allow the High setting to be below the low setting plus 10 counts of hysteresis
  }
 }
Interrupt_Latch = 1;
}

User avatar
Adrian
Super user
Posts: 56
Joined: Thu Jan 10, 2013 10:22 pm

Re: Interrupts not working - can anyone help me?

Postby Adrian » Tue Apr 16, 2013 7:46 am

High there mikeworkman,

It works for me in the current release
I copied your code and set a breakpoint at line 93

I set the sketch running (F9) and set pin2 High, on setting it to back to low (falling) the interrupt routine is entered and the program breaks

Note that my version has gained the }else{ issue and I needed to split the else statement thus...

Code: Select all

}
else{
Apologies for being opinionated - it's nothing personal, feel free to complain.
usual system: Pro V0.98 Arduino Mega 2560 Samsung i5-3210M 2.5Ghz 6GB windows home premium SP1

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Wed Apr 17, 2013 1:53 am

Adrian -

Well thank you!

I have this on two separate machines and it doesn't seem to work on either. It must be me, but I did exactly what you outlined and I cannot get it to take the interrupt...hmmm.

I will go have another look. In the meantime - thank you very much.

Mike

User avatar
Adrian
Super user
Posts: 56
Joined: Thu Jan 10, 2013 10:22 pm

Re: Interrupts not working - can anyone help me?

Postby Adrian » Wed Apr 17, 2013 5:37 pm

Hello again Mike,

That is odd! I was testing some interrupt driven code for an encoder a few weeks ago, so I was confident that it would work.

You can see my system spec. in my signature. Do you have the latest version?

To hijack your thread Mike, as a general point to users, it's always a good idea to get the latest download before posting issues.

Let us know how your get on Mike,

Kind regards

Adrian
Apologies for being opinionated - it's nothing personal, feel free to complain.
usual system: Pro V0.98 Arduino Mega 2560 Samsung i5-3210M 2.5Ghz 6GB windows home premium SP1

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Fri Apr 19, 2013 2:51 am

Adrian -

Well, I took the code to my desktop machine - a PC running Pro Version 0.98A(OS = Win XP) instead of the Laptop running Pro Version V0.98 ( OS = Win 7). I don't know if that is material but it is the only difference I am aware of in the two machines.

The laptop as described does not run the code properly - it will not take an interrupt as I described earlier.

However, the desktop as described works as you indicated yours did - and as designed.

Good and bad.

Any ideas?

And thank you again.

Mike

User avatar
Adrian
Super user
Posts: 56
Joined: Thu Jan 10, 2013 10:22 pm

Re: Interrupts not working - can anyone help me?

Postby Adrian » Fri Apr 19, 2013 6:05 pm

Hi again Mike,

Well, I'm pleased to hear that you have at least one working environment.

As mentioned above, it may be worth ensuring that you have the latest offering available. It has been seen that previous issues have resurrected themselves in later code releases (you can imagine just how complicated it is writing a simulator) and it may be that the version you are using is one of those and you are the first to notice (or believe it's the app and not the coder! :o ).
We know that your code works and that windows7 is not an issue, so the probability is a version issue.

....Otherwise I think this may be a question for the simulator_admin to investigate.

I don't think that many people use interrupts; when reading the forums, they appear to not understand the concepts - the Arduino seems to attract people who wouldn't normally use micro-controllers (which is great) but it does seem to have a dumbing down effect that is very appealing to a much bigger range of potential users. (I suppose this is in part a tribute to both the software and the hardware design). Indeed, I'm not convinced that the majority of users understand the role or use of a simulator, so users in these forums are likely to be more informed :ugeek: :D , so I am surprised at the lack of chatter.
Apologies for being opinionated - it's nothing personal, feel free to complain.
usual system: Pro V0.98 Arduino Mega 2560 Samsung i5-3210M 2.5Ghz 6GB windows home premium SP1

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Sat Apr 20, 2013 3:03 am

Hi Adrian -

Thank you again.

I run a Storage Business (was the CEO of Pillar Data Systems when Oracle bought it if you had ever heard of it), I am used to code versioning issues and Customers propensity not to upgrade. Annoying for sure - especially when they say something like "Our motto is we don't fix it if it isn't broken." As if our motto is "We fix it because there is nothing wrong with it"...not.

Anyway the Laptop code that isn't working was just downloaded last week (I lost the original in a fire :-( ) so it shouldn't be stale.

Anyway, back to the Arduino and the simulator. I love both by the way. To me, the thing is an awesome amount of fun. I can detect similar emotions from most of the community as I am sure you do too.

Interrupts are a blast, I always have loved them. My next question to you will be on the Watchdog Timer. I would like to use it in Reset Mode as opposed to interrupt mode and would like to know if the simulator supports it yet? If so, and I use the longest timer, will the Arduino Mega Bootloader do it's job or will the processor end up in an infinite reset loop? Do I need a different bootloader than the one the Mega is shipped with?

Mike

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Sat Apr 20, 2013 3:50 am

Update - I tried the Watchdog Timer - purposely didn't "Pat the Dog", and when the WDT should have reset the machine it didn't. So I take it, unless I fouled it up, that the WDT function isn't enabled in the simulator yet? :?: :?

Mike

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Sat Apr 20, 2013 5:13 am

Second update - EEPROM supported? I see I can read and write it - but I how do I display what in it since it doesn't show up as a value (I know I can read it into a variable and that tells me, but I was hoping to see the EEPROM Values themselves, perhaps as a collapsable array) :?:

Another question - when I press the reset button (since the WDT Reset doesn't seem to work), the EEPROM values don't seem to persist through the reset. They should, no??? I am not reloading the code, I am just resetting the processor.

Mike

mikeworkman
Master Class
Posts: 10
Joined: Tue Apr 16, 2013 2:27 am

Re: Interrupts not working - can anyone help me?

Postby mikeworkman » Mon Apr 22, 2013 9:40 am

:o :o :o
Sorry - I found EEPROM Display in View - my Bad.

I have been discovering this thing on my own since my Help function was busted...so I went to Google and found the Help files and now a few more things come into shape - again sorry. :mrgreen:

Mike


Return to “Version 0.98”

Who is online

Users browsing this forum: No registered users and 1 guest