Dear friends,
I need an advice from the experts of this kind.
What I need to do is: I am reading a special byte with hold the five Least Significant Bits represented the status of five external switches as (MSB) 0.0.0.SW5.SW4.SW3.SW2.SW1 (LSB). Using the Arduino C/C++ IDE how can “copy” the status of these five bits (SW5, SW4 etc) into five byte or Boolean variables, using the fastest possible way?
To get a better idea, (SW5, SW4 etc) are external switches with pullup resistors so when inactive the related status bit referenced to this switch will be “1” and when pressed will be “0”.
Please if you can, give me code examples in order to implement the above.
Dear friends,
thanks a lot for your fast answer and the support.
I will try the method with this style byte a = x & 1; but I forgot to tell you that I need also multi-keypress support (ex more than one switch pushed down at the same time).
I will see the proposed solutions and I will let you know.
but I forgot to tell you that I need also multi-keypress support (ex more than one switch pushed down at the same time).
I don't see that as a problem.
But, as PaulS alluded to, key presses (which may well require debouncing) and the necessity for "the fastest possible way" don't really square, particularly on a 16MHz platform.
Dear friends,
in continuation with my first post, (quoted bellow), let's assume that my "status switch byte" at any given time holds the status of the switches SW5.SW4.SW3.SW2.SW1, if I right shift the "status switch byte" and store each bit (that represents the status of each switch) at five distinguished variables, at the end of these five shifts shall I have the desired result? (to "copy" the, one byte multi "status switch byte", to variables possible boolean...).
What do you think? What is your proposed best programming way to achieve this?
Thanks and Best Regards,
Mike Kranidis
mikekgr:
Dear friends,
I need an advice from the experts of this kind.
What I need to do is: I am reading a special byte with hold the five Least Significant Bits represented the status of five external switches as (MSB) 0.0.0.SW5.SW4.SW3.SW2.SW1 (LSB). Using the Arduino C/C++ IDE how can “copy” the status of these five bits (SW5, SW4 etc) into five byte or Boolean variables, using the fastest possible way?
To get a better idea, (SW5, SW4 etc) are external switches with pullup resistors so when inactive the related status bit referenced to this switch will be “1” and when pressed will be “0”.
Please if you can, give me code examples in order to implement the above.
AWOL:
"Best" means a lot of things, many of which may well be completely irrelevant.
See earlier replies.
Dear AWOL,
first of all I appreciate your support.
If you can, please give me a code example to implement my demand as described in my last post: "right shift the "status switch byte" and store each bit (that represents the status of each switch) at five distinguished variables".
in continuation with my first post, (quoted bellow), let's assume that my "status switch byte" at any given time holds the status of the switches SW5.SW4.SW3.SW2.SW1, if I right shift the "status switch byte" and store each bit (that represents the status of each switch) at five distinguished variables, at the end of these five shifts shall I have the desired result? (to "copy" the, one byte multi "status switch byte", to variables possible boolean...).
What do you think? What is your proposed best programming way to achieve this?
That is a good way to do it.
If you can, please give me a code example to implement my demand as described in my last post: "right shift the "status switch byte" and store each bit (that represents the status of each switch) at five distinguished variables".
Give it a try, and post your code. Arduino is a learning environment. You will find the code is quite simple and you can print out your results to see if it did what you wanted. If you can't figure it out, try Paul's suggesion of bitRead() in reply#1, or AWOL's approach in reply #2.
One thing to pay attention to is that the bits in your status switch byte are zero referenced(i.e. 0 to 7), as well as the elements of your individual switch states (elements 0 to 4) in an array called switchState[5]. However, you reference the values as SW1,SW2,SW3,SW4,SW5.
P.S. I am very code ( C/C++ ) newbie
Not that new if you are talking about bit shifting.
Give it a try, and post your code. Arduino is a learning environment. You will find the code is quite simple and you can print out your results to see if it did what you wanted. If you can't figure it out, try AWOL's approach in reply #2.
One thing to pay attention to is that the bits in your status switch byte are zero referenced(i.e. 0 to 7), as well as the elements of your individual switch states (elements 0 to 4) in an array called switchState[5]. However, you reference the values as SW1,SW2,SW3,SW4,SW5.
Not that new if you are talking about bit shifting.
Dear cattledog,
thanks for your reply.
Just to clarify that my reference at values as SW1,SW2,SW3,SW4,SW5 is human quotation.
Of course the correct is MSB 0.0.0.SW5.SW4.SW3.SW2.SW1 LSB that is definitely the "status switch byte".
Any code sample for me?