I'm working on a project reading incoming serial data from a remote control keypad device. The data sent is always 3 bytes of information with the first byte always the same. (0xFE) It only sends data when a key is pressed, so there is no constant stream of data, or a 'no key is being pressed' packet.
I've been able to read the data and decode it just fine for the most part but have run into a problem with the fact that it constantly sends a new packet of 3 bytes as fast as the connection (9600 baud) will allow as long as a key is held down. I do not know how to take this repeated information and use it as one event, as opposed to dozens of repeated events per second.
Ex: while a key is held down on the device it will send 0xFE FF FF over and over again and i want an led to remain turned on constantly as long as the button is held down.
Adding to my trouble is the fact that either the device or my code reads a random zero value from time to time even when the key is held down.
I have searched to the best of my ability for an example of how to deal with this, and so far have come up short. Any help would be greatly appreciated.
I'll be happy to provide whatever further information is needed. My current sketch is a mess of other functions for deciphering the incoming data as i need, but is mostly running as follows, in pseudo code:
if serial available > 2 (if there is at least one full 3 byte command) if first byte = 0xFE, if not read the next byte read second byte, shift 8 bits left read third byte, add to shifted second byte Store 16 bit number to int variable check 16 bit var against conditions, do things if the number matches.
What I need to figure out is how to make it keep doing the same function as long as the incoming data remains the same, then stop when no new data, or different data comes in.