Solved

Arduino (processing/C) loop wont loop with a delay

Posted on 2011-03-12
9
946 Views
Last Modified: 2012-05-11
On line 27 if I uncomment it the program will loop once or sometimes twice and thats it.  As soon as I comment it out the programm loops with just the 1 second delay while the hall effect sensor is timing pulses.
I've moved it up into the main void loop, moved it to the top, moved it to the bottom.  I cant figure out why it kills the program?  No matter where I put in the additional delay it dies.  But I have many other sketches that have multiple delays with no problem.


//Dry Remote Arduino Dingle Dongle for GroPI5000  (CO2, Temp, Humidity, Flood Alert)

int AirtemperaturePin = 3;     //Air temp connected to analog 3
int airtempsensorvalue = 0;    //Air Temp Sensor Value 
int airtemperature = 0;        //Air Temp
int CO2Pin = 4;                //CO2 Sensor Pin
int CO2SensorValue = 0;        //CO2 Sensor Value
int CO2LevelValue = 0;         //CO2 Level Value
int FloodValue = 0;            //Flood Sensor value
int FloodPin = 5;              //Flood Sensor Pin

volatile int NbTopsFan;        //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 2;            //The pin location of the sensor
void rpm ()     //This is the function that the interupt calls 
{ 
  NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600); //This is the setup function where the serial port is initialised,
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
void loop()        {             // run over and over again
doDryDingleDongle();
//delay (5000);	//Wait 5 seconds  --- THIS DOESNT WORK

}

void doDryDingleDongle()
{
 ///////// Water Flow Meter

   NbTopsFan = 0;	//Set NbTops to 0 ready for calculations
  sei();		//Enables interrupts
  delay (1000);	//Wait 1 second
  cli();		//Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate 
  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line

  // Air Temp
   
airtempsensorvalue=analogRead(AirtemperaturePin);//Connect LM35 on Analog 3     
airtemperature=(500*airtempsensorvalue)>>10; 
airtemperature = (airtemperature * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
Serial.print("Air Temp:");
Serial.print(airtemperature);                     //printing the result
Serial.print(" / ");
Serial.print("Pin Reading:");
Serial.println(airtempsensorvalue); 
 

  /////////////////////////////////// //  CO2
   CO2SensorValue = analogRead(CO2Pin);
CO2LevelValue = map(CO2SensorValue, 0, 1023, 0, 2000); ;            
  Serial.print("CO2:");
  Serial.print(CO2LevelValue);                     //printing the result
Serial.print(" / ");
 Serial.print("Pin Reading:");
 Serial.println(CO2SensorValue);
 Serial.println("---------------------------------");
} 

Open in new window

0
Comment
Question by:livegirllove
  • 4
  • 4
9 Comments
 
LVL 10

Expert Comment

by:peetm
ID: 35115052
So, you need sei() before the delay call, and cli() after it perhaps?
0
 
LVL 1

Author Comment

by:livegirllove
ID: 35115065
no I need the 1 second delay between enabling and disabling interupts.  Thats where its counting the hall effect pulses.
0
 
LVL 10

Accepted Solution

by:
peetm earned 500 total points
ID: 35115070
So, if you increase your 1 second delay to 5, it works?

If yes, then there's presumably some interrupt that needs handling - yet that's been missed?  Is this right?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:livegirllove
ID: 35115189
lol.  i dont know.  thats why Im asking the experts.
I suppose I could
sei();		//Enables interrupts
  delay (10000);	//Wait 1 second
  cli();		//Disable interrupts
  Calc = (NbTopsFan * 6 / 7.5); //(Pulse frequency x 6) / 7.5Q, = flow rate 

Open in new window


That should fix the math.

But your statement about the missing interupt has me thinking.  Ive been testing with the device not doing anything.  I wonder If I run some water through it will it activate the interupt.

Honestly Im talking crazy here as this is my first digital sensor or hall effect sensor and my programming skills are not great.  Ive only used analog sensors in the past.  I dont really know what the interupts are.
0
 
LVL 1

Author Comment

by:livegirllove
ID: 35115324
running water through it didnt work.
I can change the one delay and that works.
However I would like to find out why I cant use another delay as I may NEED to further in the progect.
0
 
LVL 10

Expert Comment

by:peetm
ID: 35115685
I think I'd have to see the documentation on Arduino interrupt-handling to help further (and the programming guide(s) too of course) - I don't have them here.
0
 
LVL 12

Expert Comment

by:satsumo
ID: 35131034
I have no answer to the question, just wanted to say doDryDingleDongle is one of the best function names ever.  I saw a function called SaveTheWorld, a noble thing to write.  My own best (or maybe worst) line of code is this masterpiece 'pidldupl (padl, podl)'.
0
 
LVL 1

Author Comment

by:livegirllove
ID: 35131069
lol fair enough.  This is a remote sensor in a dry area.  I have another called WetDingleDongle.  I need to close this and open it up on the arduino forums.
0
 
LVL 10

Expert Comment

by:peetm
ID: 35131481
I worked on Windows (back in the 90s) - where we had undocumented functions like (if I remember it right) ChangeoPrestoSelector() ... that could change a data segment into a code segment (Ooo, self-modifying code!)

Sadly, most of those have now gone away!  Another of my favs was TabTheTextOutForWimps() - more background if you're interested:

http://blogs.msdn.com/b/oldnewthing/archive/2003/10/15/55296.aspx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question