1

I am trying to build a cashless RFID based water dispensing unit. I am using Arduino mega 2560, PN532, relay, flow sensor. Here is the flow of the program in I2C mode; I have two functions, one to scan the card and the other for the flow sensor. As soon as the RFID card is placed on the NFC module, data stored in the card (i.e, date of expiry and balance amount) is obtained and is checked for validity. If the card is valid, then the relay is turned on. A flow sensor measures the quantity of water dispensed. As soon as the limit of 1000 ml is reached, the relay is turned off. The whole process happens serially, when the card scanning is complete, it waits for the entire 1000 ml of water to be dispensed. What I want to achieve is after one card is scanned, 1st relay with the flow sensor has to be turned on and simultaneously the scanning of the second card has to begin. If the first relay is busy, second relay has to be assigned to it. Can I achieve this using Arduino Mega? How do I manipulate the atmega to perform all these tasks at once?

2

2 Answers 2

1

You do not necessarily need a build a "multitask with interrupts on Arduino". Instead consider using a state machine. First define the problem by drawing a state diagram:

enter image description here

Consider all state you wish to be in. Including your example "...after one card is scanned, 1st relay with the flow sensor has to be turned on and simultaneously the scanning of the second card has to begin...". Once you have defined all states and the rules that need to be tested for before jumping to a new state you can start programming your state machine.

There are plenty of tutorials including this one on the web specifically for people who want to implement a state machine on an Arduino. But, briefly, you will want to create a loop that will execute with out stopping over and over again as fast as possible. This loop will (1) know what the current state is and (2) will repeatedly test to find out if the program can change to any of the possible next states. When you change states is when, if you need to, you perform tasks such at turning the water on or off.

If you forsake the state machine and only use interrupts, testing the code tends to be very difficult. As controlling exactly when an interrupt occurs relative to other interrupts and the state of the processor becomes problematic and nearly impossible to reliably repeat. This is why the state machine is suggested. Interrupts can still detect external events. But by using a state machine the program is in control of what happens next. Instead of leaving it up to luck.

5
  • I tried implementing the state machine for my program. It helped me get to the solution. Thanks a lot. Cheers!
    – Riya
    Mar 12, 2020 at 5:18
  • 1
    You are welcome. Please choose the best answer to your question so that others with a similar problem can find your question and an appropriate solution. Comment if nether solution is the best answer - telling us how to edit the answer to make the overall question & answer better.
    – st2000
    Mar 12, 2020 at 22:49
  • As you mentioned, I designed a state diagram. Hence, it was clear to me in which state each component had to be at a particular time. I ensured that the program did not have to wait for anything to continue. I got the solution by simply manipulating the interrupts used by the flow sensors to make them function almost at the same time. However, I did not entirely implement the state machine program on my arduino. Designing the state diagram helped me get to the solution.
    – Riya
    Mar 17, 2020 at 11:45
  • Glad the answer helped. Now I have a question for you: Since it sounds like you implemented your solution using only interrupts, how easy was it to test? And are you sure you tested every situation? Are you willing to ride a rocket ship or drive a car running your code? I'm trapping you. The answer? It is very hard to test all situations when interrupts are involved. That is why I suggested the state machine. You can still detect situations using interrupts. But using a state machine means you are in control of what happens when. Instead of leaving it up to luck.
    – st2000
    Mar 18, 2020 at 1:13
  • Thanks @st2000. You do have a valid point. I will try to implement it using state machines to have control over the program.
    – Riya
    Mar 23, 2020 at 6:30
1

There is no real multitask on Arduinos. The Arduino can only do one thing at a time.

But, you can do things sequentially, one after another, so fast, that a human will think, that it happens simultaneously. This involves a non-blocking coding style, which needs no interrupts

  • Every delay in your code (at least the long onces) need to be removed. if you want to do timed things, you should use the coding principle from the BlinkWithoutDelay example, that comes with the Arduino IDE.

  • Also no function should wait for something to happen. Think of a function, that waits for a command from the serial interface. Blocking code would wait indefinitely, until a command was received, preventing any other code from execute. Instead the function could exit fast, when there is currently no command to process, leaving time for the other code to execute.

The whole principle relies on the loop() function executing very fast, so that your code can still be reactive. This works for most of the situations. Only sometimes you need to react that fast, that you need interrupts. Your usecase doesn't seem to belong to the interrupt needed faction.

2
  • Thank you for helping me out! I removed all the delays in my program and got the code working just right.
    – Riya
    Mar 12, 2020 at 5:25
  • @Riya Please mark one answer that fits you most. This will help others seeking for help on similar problems. Mar 18, 2020 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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