Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 986
  • Last Modified:

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

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
livegirllove
Asked:
livegirllove
  • 4
  • 4
1 Solution
 
peetmCommented:
So, you need sei() before the delay call, and cli() after it perhaps?
0
 
livegirlloveAuthor Commented:
no I need the 1 second delay between enabling and disabling interupts.  Thats where its counting the hall effect pulses.
0
 
peetmCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
livegirlloveAuthor Commented:
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
 
livegirlloveAuthor Commented:
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
 
peetmCommented:
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
 
satsumoSoftware DeveloperCommented:
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
 
livegirlloveAuthor Commented:
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
 
peetmCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now