i want to create a program like this
digitalread (pin1,pin2,pin3,pin4)
if val of(pin1,pin2,pin3,pin4)==(0,1,0,1,0)
action one
and so on is it possible ? :-[
i want to create a program like this
digitalread (pin1,pin2,pin3,pin4)
if val of(pin1,pin2,pin3,pin4)==(0,1,0,1,0)
action one
and so on is it possible ? :-[
if(digitalRead(pin1)==0 && digitalRead(pin2)==1 && digitalRead(pin3)==0 && digitalRead(pin4)==1){
//do something here
}
That help?
Bitwise and, the reference page has more info
Mowcius
If not then you might be able to do port reads with direct port manipulation and then compare that to the values you want...
Not sure about that though.
The reason for wanting to do this can only be speed. Firstly see digitalReadFast:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267553811/0
If that's not fast enough then you'll need to directly access PINB, PINC and PIND directly depending on how you've assigned your pins.
There is another reason: memory consumption. Direct port uses less flash.
Udo
yeah andy brown was right i wanted to do it for speed
any more help in that direction
As Andy said digitalReadFast is about 30 times faster than digitalRead. For multiple pins at once port manipulation can be up to 8 times faster still, but with a steeper learning curve. Arduino Reference - Arduino Reference
How much speed do you need and for what? Sometimes you can beat even direct port manipulation. But it depends on what you want to achieve.
Udo
ill need the maxium possible speed udo
Of course you need the maximum possible speed. But for what? Depending on what you are doing there may be means to be faster than with direct port manipulation.
If you really need maximum possible speed you can drop Arduino and go for a faster processor and/or an FPGA. Or you can replace the crystal by a 20 MHz crystal.
It's not only port access statements. The rest has an inpact on speed as well. You might need to consider using assembler. Still the question is why. Sometimes a faster solution is using just one or two additional logic chips.
Udo
Input pins can be read up to 8 at a time (depending on which pins) using "direct port manipulation." You can read pins 0 through 7 at PIND (bits 0-7), 8 through 13 at PINB (bits 0-5) and the analog pins (in digital) at PINC (bits 0 through 5)... See Arduino - Google Sheets
So for example:
pins = PINB & 0x7; // read pins 8/9/10
switch (pins) {
case 0:
// no switches set
break;
case 1:
switch1_func();
break;
case 2:
switch2_func();
break;
case 3: // switches one and two.
switch1_func();
switch2_func();
case 4: // switch 3 makes 1&2 into "don't care"
case 5:
case 6:
case 7:
switch3_func();
break;
}
}
If this makes perfect sense to you, then you should be all set, and the rest of us should be remorseful about wasting your time with philosophical diversions...
If it doesn't make much sense, it's going to be difficult to explain how to do what you're asking "at maximum speed" until you've read up enough background material that it at least starts to make sense. (This is not the only way to make decisions based on reading multiple pins, but it has all the basics in there. You'll need to deal with bitmasks, for example.)