Hi all,
I'm having a problem with my step sequencer code.
Basically i have to do multiple digitalWrites again and again through out my code to switch channels on a multiplexer (mux sheild). I decided to try to use function calls, instead of specifying the digitalWrites through the code multiple times. This is theoretically working fine, however, the function i am trying to use just isnt responding to any integers. i need to change the value of "i" for each execution of the function, buts its not working, I have tried loads of combinations of placing the interger here and there, but with no luck.
am i misunderstanding what a function is capable of? or am i just making a silly mistake, i cant really tell from what i have read as none of the examples are close enough to my code to make a comparison, at least for me anyway.
if (voice1[1] == 0){
if ((counter/4 == 1) && (beatStep == 1)){
muxWrite(15);
digitalWrite(Led29,HIGH);
}
else digitalWrite(Led29,LOW);
}
i kept getting the error "i wasn't declared in this scope"
i didnt realise that the function would automatically assign the (15) to i, i kept trying int i=15; and i=15;
thanks, i tried to wirte it as an argument on one of my attampts beforehand, i just didnt send the 15 to the function correctly.
the value of i is only zero if the function is called without argument (0 is default) or if the param is explicit zero.
That said the function can be made shiftless, because digitalWrite() interprets all non zero values as HIGH
void muxWrite(int i=0)
{
digitalWrite(S0, i & 8); // just test for the bit that would become 1 in the shift version
digitalWrite(S1, i & 4);
digitalWrite(S2, i & 2);
digitalWrite(S3, i & 1);
}
removed the comments as these were in conflict with the code ...
What do you suppose (0 & 15) returns? How can that possibly ever differ from 0?
What is the use of right shifting 0?
to be honest, i'm not really sure, its taken from the Mayhew Labs mux sheild page, i would suppose that 0&15 returns 0? which is fine, i want it to be 0 in some cases, its easier just to keep the code the same all the way through and only change the integer at each call. am i missing something here? because to be honest, i dont really undertsnad that line myself, i just know it works.
robtillaart:
the value of i is only zero if the function is called without argument (0 is default) or if the param is explicit zero.
That said the function can be made shiftless, because digitalWrite() interprets all non zero values as HIGH
void muxWrite(int i=0)
{
digitalWrite(S0, i & 8); // just test for the bit that would become 1 in the shift version
digitalWrite(S1, i & 4);
digitalWrite(S2, i & 2);
digitalWrite(S3, i & 1);
}
removed the comments as these were in conflict with the code ...
ahh, after reading through that and refering to the arduino reference page, and checking out some binary, its become clear to me that the purpose of (S0, (i&15)>>3) and so on, is to always output either 0 or 1 (low or high) at the end of the operation, i never realised (even though it now seems obvious) that it could be 0 to whatever and still work.
so i'm guessing that the >> is solely to convert the output to either 1 or 0 and nothing else? (i is never > 15)
also could you tell me, is it sloppy to use bitshift? its obviously a tidier solution as far as output goes, but the actual value of the output is totally non importnt in this case, am i going to get undesirable effects from using bitshift?
so i'm guessing that the >> is solely to convert the output to either 1 or 0 and nothing else? (i is never > 15)
also could you tell me, is it sloppy to use bitshift? its obviously a tidier solution as far as output goes, but the actual value of the output is totally non importnt in this case, am i going to get undesirable effects from using bitshift?
the >> moves/shifts the bits so that you are left with a 1 or 0 indeed. The fact that the shiftless code works is because the digitalWrite interprets everything that is not LOW as HIGH. If it had a stricter interface the conversion to 0/1 would be mandatory.
It is not sloppy to use bit shift, on contrary it makes the code more explicit what you mean.
If the Arduino team decides to make digitalWrite() with an additional parameter e.g. LOW, HIGH, and lets say INVERT (inverts the current state) then the shiftless version would become trickier as not all non zero values mean HIGH.
THe only effect you get from the bitshift and the mask is that it costs a bit extra time few micros()...
thanks robtillaart, i thought it might use more processing time, but i dont think its critical to my current project, thanks for the clarification though. i think i will leave it in place, it seems more like the right way to go about it, even though its less legible, as long as i know what it does then its fine