November 15, 2024, 02:48:10 PM

MOD-IO digitalRead returns nothing?

Started by kleurbleur, October 21, 2022, 05:27:45 PM

Previous topic - Next topic

kleurbleur

I'm using mod-io rev.a and using the arduino library. Everything works as expected except for the digitalRead function which does not return anything it seems.
The led next to the optocoupler does light up and the the digitalReadAll does return something (8 in this case).
When I check the library file itself on line 109 it returns both data and pin (return !!(data & pin);). I changed this to only return data without result. Anything I'm overlooking?

LubOlimex

What is the signal that you have connected to the digital input?
Technical support and documentation manager at Olimex


LubOlimex

Seems to work just fine here. 8 means there is input on the 4th input. 1 means first input, 2 means second input, 4 means third, 8 means forth. Combination of these would mean more than one is present, for example value 15 would mean all are present (1+2+4+8). Just tested it.
Technical support and documentation manager at Olimex

kleurbleur

Allright, I could use that function too. But does the single DigitalRead work with you? Or was it never intended to work since it was not used in the example, though it is present in the lib (line 100 to 110)?

LubOlimex

This function is likely an erroneous leftover - compare digitalRead and digitalReadAll - same code, no way it does something different. Looking into the firmware of MOD-IO I am not sure if single read is possible at all.
Technical support and documentation manager at Olimex

kleurbleur

Yeah I had that feeling already. Would be great for simple setup purposes since it follows the standard Arduino digitalRead way.

kleurbleur

Quote from: LubOlimex on October 28, 2022, 10:28:45 AMSeems to work just fine here. 8 means there is input on the 4th input. 1 means first input, 2 means second input, 4 means third, 8 means forth. Combination of these would mean more than one is present, for example value 15 would mean all are present (1+2+4+8). Just tested it.

I'm now integrating digitalReadAll() but it seems overly complex and prone to errors.
I want to be sure to receive all the possible inputs so for example checking for input 2 would be something like this:

if(digitalReadAll() == 2 || digitalReadAll() == 3 || digitalReadAll() == 6 || digitalReadAll() == 7 || digitalReadAll() == 10 || digitalReadAll() == 11 || OR digitalReadAll() == 14 || OR digitalReadAll() == 15


Doing this for every input is quite annoying to say to the least.

JohnS

Quote from: kleurbleur on October 31, 2022, 07:33:48 PMI'm now integrating digitalReadAll() but it seems overly complex and prone to errors.
I want to be sure to receive all the possible inputs so for example checking for input 2 would be something like this:

if(digitalReadAll() == 2 || digitalReadAll() == 3 || digitalReadAll() == 6 || digitalReadAll() == 7 || digitalReadAll() == 10 || digitalReadAll() == 11 || OR digitalReadAll() == 14 || OR digitalReadAll() == 15


Doing this for every input is quite annoying to say to the least.

Just use

if(digitalReadAll() & 2) ...


kleurbleur

#9
Quote from: JohnS on October 31, 2022, 09:31:52 PMif(digitalReadAll() & 2) ...

I'm still a beginner in C. How does that work?

I looked it up and it is the bitwise operator AND. Basically it adds the two bits and returns only the 1's in the both bits in the same position. I know too little of bits to see how it works with the other numbers, but I guess the if statement only returns to true if there are 1's in the same position as 2?

JohnS

Rather than "adds the two bits", say "compares ...".

Low-level use of C, such as with hardware, tends to use bits often, so being comfortable with them would really help.

John