I have a strange problem, and just want to verify that a for n= 1 to whatever sequence in a separate function ( subroutine) that it doesnt return to the main loop until it has counted through all n operations ?
As stated, you are correct, unless you have a return hiding in the looping function somewhere. Post your code though, funky behaviour may be an indication of pointer issues or low memory.
edit: grammar
Its a function to read the data from bcd switches, the clock pulse advances a CD4017 whose outputs go to the common of the switches.
Putting the 4 bits together with the OR function is pasted from another sketch that works in many of my sketches, I don't know if there is a simpler bit manipulation way?
void scan ()
{
for ( int x=2; x<= switchbanks; x++ )
{
digitalWrite(clock, HIGH);
add3 = digitalRead(SW3);
// shift it left 3 places
add3 = add3 << 3;
add2 = digitalRead(SW2);
// shift it left 2 places
add2 = add2 << 2;
add1 = digitalRead(SW1);
// shift it left 1 place
add1 = add1 << 1;
add0 = digitalRead(SW0);
// now OR it together
address = address|add3;
address = address|add2;
address = address|add1;
address = address|add0;
digitalWrite(clock, LOW);
Serial.print("data for switchbank "); Serial.print(switchbank); Serial.print(" = ");
Serial.println(address, BIN);
msg [switchbanks] = address;
} // end of for ( int x=2; x<= switchbanks; x==)
} // end of scan function
A bitwise OR is usually the correct method. It is a logical bit-operation, and is one of simplest ones (if neither is false then true). However, you seem to have forgotten to zero the address at the beginning of each loop cycle - if you don't, OR will never set a bit to false, so only the first reading will be valid.
With your code, it seems like you'd be better off with something like this (it's the same, just a little more elegant):
uint8_t sw[4]; // assign your four pins
void scan() {
for ( int x = 2; x <= switchbanks; x++ )
{
address = 0;
digitalWrite(clock, HIGH);
for (uint8_t i = 0; i < 4; i++) {
address |= digitalRead(sw[i]) << i;
}
digitalWrite(clock, LOW);
Serial.print("data for switchbank ");
Serial.print(switchbank);
Serial.print(" = ");
Serial.println(address, BIN);
msg [switchbanks] = address;
}
}
Thanks thats elegant, but what is the address |=
in
for (uint8_t i = 0; i < 4; i++) {
address |= digitalRead(sw*) << i;*
- }*
but what is the address |=
"address |= x;" is is the same as writing "address = address | x;"
Works with binary arithmetic and logical operators,
-=
+=
*=
/=
&=
^=
Got it - compound bitwise or
thanks.