Solved

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

Posted on 2011-03-12
9
934 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
Comment Utility
So, you need sei() before the delay call, and cli() after it perhaps?
0
 
LVL 1

Author Comment

by:livegirllove
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:livegirllove
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:livegirllove
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now