I would like to sense 2 digital pins simultaneously and perform a task independently, if the condition satisfies one digital, a said task shall be executed and the second pin shall continue to sense for a condition. If the condition for the second pin satisfies a different task has to be performed independently. (there shall be a delay in both the tasks).
You do realise if these inputs are buttons, one WILL be detected before the other. Even if you think you're pressing them both together, it's inevitable that there will be a delay of a quite a few microseconds between each.
buttonRead = PIND & 0b00000011; // read 2 bits at the same time.
if (buttonRead == 0b00000010) {
// one was high
}
if (buttonRead == 0b0000001) {
// other was high
}
if (buttonRead == 0b00000011) {
// both were high
}
if (buttonRead == 0b00000000) {
// neither was high
}
I would like to sense 2 digital pins simultaneously and perform a task independently, if the condition satisfies one digital, a said task shall be executed and the second pin shall continue to sense for a condition. If the condition for the second pin satisfies a different task has to be performed independently. (there shall be a delay in both the tasks).
Kindly suggest the best way forward.
Best way is to have one task reading the pins and setting a variable to trigger actions done by other tasks.
And you need to get a lot more detailed about what the pins do, especially if they are buttons.
For example, with good code you may read both pins 100x a millisecond. So your code sees pin 1 HIGH or LOW (depending on how you wire the pins and what modes you use, LOW can be "pressed") and sets a trigger for pin 1 pressed and then 10 microseconds later it sets the trigger again, repeat. Will that try and do some action as many times?
If your read the pins task does not code for immediate action then it can determine status such as press-and-release or pin-state-change to only send 1 trigger to a separate action task. Or it may watch buttons on pins to wait for the button bounces to stop before setting a trigger status. It depends on what you connect to the pin!
Keep the code for each device or step in the process separate and you benefit by
using less indents, your code will be simpler. Pass logic through variables you can print to debug.
with each task being modular you can alter or replace modules to suit changes or reuse in new sketches.
you can get rid of calls to delay() because your code will be event/trigger based.
So perhaps now you have different information to present and more questions?
Is this a homework assignment? If so, review what you've been studying recently to try and put this in context...
The two tricky words are "simultaneously" and "independently".
Nothing in the physical world happens simultaneously. For example, when the CPU reads from memory, there has to be "settling time" between when the address lines are set-up, the read-line is triggered, and all of the data lines are settled and stable. If you look at the data sheet for a RAM or flash chip, you'll see the related timing diagrams.
And, the CPU can only do one thing at a time. When your computer multitasks* it's actually switching quickly between tasks. For the purposes of what you are doing, the Arduino can only execute one line of C/C++ code at at time.
The Arduino doesn't have an operating system, so it's up to you to make sure it's not stuck in a loop with Task-A "hogging" the CPU if you also need to be checking inputs and/or doing Task-B at the "same time".
Even when you are only running one application, your computer is multitasking. It's checking for keyboard & mouse input, updating the clock and display, and a bunch of other background stuff.
You can collect digital data from all the open pins on a Port in the same read as opposed to sequential reads. You can digital write in the same fashion. Check if that takes 1 or 2 cycles. At 16 MHz, digital read and write is pretty close to simultaneous. If you need "within a microsecond" then it's more than enough.