tsakitsan:
This will be translated as 10000 which is equal to 2.
how is 0b10000 equal to 2!?
tsakitsan:
But this does not seem like lean and fine programming. Is there a different way of changing inputs outputs to compensate for of the 32 combinations?
to do 32 combinations, if you need all 5 outputs to change SIMULTANEOUSLY then your best bet is “direct port manipulation” else you could implement something like this in your code:-
for(uint8_t cnt=0; cnt<32;++i){
digitalWrite(2,cnt&0x01);
digitalWrite(3,(cnt>>1)&0x01);
digitalWrite(4,(cnt>>2)&0x01);
digitalWrite(5,(cnt>>3)&0x01);
digitalWrite(6,(cnt>>4)&0x01);
delay(1000);//only here so that u can the see the output changing
}
Also pay attention to your braces : the last one is one too many...
And the lines setupandloopi.e. lines 4 and 15, don't mean anything and should be removed.
In the DIO logic the same is DI1=HIGH DI2=LOW DI3=LOW DI4-LOW DI5=LOW which equals 2.
sherzaad:
how is 0b10000 equal to 2!?
to do 32 combinations, if you need all 5 outputs to change SIMULTANEOUSLY then your best bet is “direct port manipulation” else you could implement something like this in your code:-
If you want all five output pins to change at the same time, you must use direct port manipulation.
Access to the AVR port registers is fully supported in the Arduino environment which simplifies the program at the expense of readability. Just study the online tutorials for direct port manipulation, it’s quite simple once you understand the names and rules.
Here is you demo program, complete, using direct port access. It was tested on an Uno.
void setup() {
// set pins 2,3,4,5, and 6 as outputs
DDRD = DDRD | B01111100;
}
void loop() {
for ( byte cnt = 0; cnt < 32; ++cnt) {
// write binary value 1 to 31 to pins 2,3,4,5, and 6
// while preserving the contents of bits 7, 1 and 0.
PORTD = (PORTD & B10000011) | (cnt << 2);
delay(1000);
}
}
WattsThat:
If you want all five output pins to change at the same time, you must use direct port manipulation.
Access to the AVR port registers is fully supported in the Arduino environment which simplifies the program at the expense of readability. Just study the online tutorials for direct port manipulation, it’s quite simple once you understand the names and rules.
Here is you demo program, complete, using direct port access. It was tested on an Uno.
void setup() {
// set pins 2,3,4,5, and 6 as outputs
DDRD = DDRD | B01111100;
}
void loop() {
for ( byte cnt = 0; cnt < 32; ++cnt) {
// write binary value 1 to 31 to pins 2,3,4,5, and 6
// while preserving the contents of bits 7, 1 and 0.
PORTD = (PORTD & B10000011) | (cnt << 2);
delay(1000);
}
}
Thanks for the code mate, I will have a look and customize it to do quicker the exact manipulations I want.