0

I am rather new to the Arduino world and am looking for a way to create my own tipping rain gauge with only one Arduino(micro).

I have had some help from a friend to get a loop that checks for when a switch is closed, which signals that the bucket has tipped and thus "x units" of rain have fallen. The code I have for sensing this is:

void loop()
{
  /*
    digitalWrite(4, HIGH);
    delay(500);
    digitalWrite(4, LOW);
    delay(6500);
  */
  int analogValue = digitalRead(8);

  if (analogValue==HIGH)
  {
    Serial.println(count);
    count++;
    delay(250);
  }
}

Then I developed a code to count up in 0.5 second intervals so that I could essentially time stamp when the switch from the first loop was closed/activated. The code for the timer is as follows:

void loop()
{
  while (ssec < 10)
  {
    Serial.print(hour);
    Serial.print(":");
    Serial.print(minute);
    Serial.print(":");
    Serial.print(sec);
    Serial.print(".");
    Serial.print(ssec);
    //Serial.print(" s");
    //Serial.println();
    ssec += 5;
    delay(500);
    Serial.println();
  }
  if (ssec == 10);
  {
    sec++;
    ssec = 0;
    //Serial.print(sec);
    //Serial.print(".");
    //Serial.print(ssec);
  }
  if (sec == 60)
  {
    minute++;
    sec = 0;
  }
  if (minute == 60)
  {
    hour++;
    minute = 0;
  }
  if (hour == 24)
  {
    Serial.println();
    Serial.print("DAY ");
    Serial.print(day);
    Serial.print(" COMPLETED!");
    Serial.print("Data Points: ");
    //Serial.print(count);
    Serial.println();
    Serial.println();
    hour = 0;
    day++;
  }
}

I understand that it is very hard to run these together at the same time due to the fact that I am using a delay function to run the timer.

Are there any ways to essentially run these side by side? Or a library I can use that will run the timer while i have the arduino looking for the switch to close, and I can simply call the timer when I need it?

Thank you for any and all help! :)

1

What's with the double-spacing? It makes it hard to read.

if(ssec==10);

That is wrong.

Arduino programming traps, tips and style guide

I understand that it is very hard to run these together at the same time due to the fact that I am using a delay function to run the timer.

I know StackExchange doesn't like link-only answers, but it is this or copy-and-paste:

How to do multiple things at once ... like cook bacon and eggs

| improve this answer | |
  • I couldn't understand how to format the code and it was just coming up as a big chunk of text, so i just spaced it out. – Mitch Oct 30 '15 at 7:09
  • This site does explain how to do that (and I edited it for you). Basically you select the code and click on the "code" button which indents it all 4 spaces. The indentation is what is needed, not lots of new lines. – Nick Gammon Oct 30 '15 at 19:34
0

You can find some 3rd party library that does software emulation of threads. For example: ArduinoThread. With the use of such library I successfully developed an app that was switching LEDs on and off and at the same time listened to any data coming from Bluetooth (serial port). It is worth a try.

| improve this answer | |
  • No, this is really not a problem that calls for threads. Jumping right to that for a problem of this simplicity is skipping over some of the key basics of embedded development, and not something that should be encouraged. – Chris Stratton May 26 '16 at 17:16
0

If you do not need to know the timing when the event occurs with a high resolution, and do not have anything else you need to do, you can simply delay in short intervals and check the condition in between them. Of course you will not know when the event occurred more precisely than knowing it happened sometime in the interval since the previous check.

Another approach is to not use the delay mechanism, but rather continually check the input in a loop, and also check the elapsed time so that you can accomplish your periodic events when intervals of time have elapsed. Do be aware of the millis() overflow eventuality if taking this approach.

If you had important work to do while waiting, you could accomplish the check with a pin change interrupt, and record a timestamp in the interrupt service routine, but if all you would be doing in the foreground is running a delay, this is inferior to the previous approach. Using an interrupt service routine (or any more elaborate form of threading) also requires care to ensure consistency of data shared between the main and interrupt or alternate thread context.

Ultimately the best solution for an actual real-world embedded application like this is typical to try to have the processor in a low powered sleep mode most of the time. Typically you would have a low power real time clock running, but the processor's high speed clock and associated logic stopped. Your tilt sensor would be wired as an interrupt that the MCU is configured to wake up to normal operation in response to. On wakeup, it would read the clock to accomplish a timestamp, then go back to sleep. You can also use various mechanisms in the real time clock or in the MCU to wake up periodically to announce the passing of time intervals. Wakeup on a small MCU is typically a fairly efficient process - it can be beneficial in terms of net power consumption to go (at least partially) to sleep even for a fraction of a second.

| improve this answer | |
  • Just in case the OP goes looking for it, there is no RTC on 8-bit AVRs (UNO, Mega2560 et al) nor the Arduino 101. There is an RTC on the 32-bit SAM-based boards (Due et al). – slash-dev May 27 '16 at 14:25
0

Yes, you can run the two loop() function side-by-side with a Multi-tasking Scheduler.

Please see https://github.com/mikaelpatel/Arduino-Scheduler. There are plenty of examples to get you started but basically you call Scheduler.start() with loop() functions. You will have to give them different names.

The Scheduler will context switch between the loop() functions on yield() or delay().

Cheers!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.