string of bits to pin

Hi,
This is going to be a very novice question, just getting started in arduino programming and I can't find anything that answers this question.

I've got a string of bits (16 bits) that I need to write out to a digital pin based on a clocking pin. I've got an interrupt set up to detect the change in clock and I have this working perfect but I'm not sure how to put a single bit out to the pin on each clock cycle.

Essentially how it works is the data is read on the clock high and written on the clock low.

Thanks!

In what form is the "string of bits (16 bits)"? Do you mean an 'int'/'unsigned int', or a 16-element array of boolean variables, or a C string of 16 characters, '0' / '1', or a String holding the same?
(I ask because someone earlier was doing the same, with a String object, so thought I'd better ask you to clarify.)

You write the next bit when the clock goes low then... Don't detect CHANGE, use FALLING instead.

My intent was to just the String (16 characters) if that was possible.

My initial thought was to hold a global counter and modify that as I went through the string and wrote the value to a pin but then was curious if there was some clever way to just shift the MSB off the String to the pin and have the string modified.

Knowing interrupts need to be fast I didn't want to do any string manipulation.

A String object will hold ASCII characters, '0' (0x30), or '1' (0x31), not bits.

Do you really need to use String? How do you get those bits in the first place? Much easier if they're just in an 'int' / 'unsigned int'.

(It's very late and I'm on the way to bed, but your answer will assist whoever else helps next. :slight_smile:

What the use looks like is a virtual button is pressed, that button press is referenced to the binary and then outputted to the output pin.

I can define the binary in an unsigned int if that makes more sense.

What it looks like
Button1 = 1111111100000001
Button2 = 1111111100000010
Button3 = 1111111100000011
etc.

The 0xFF to start is to say a button was pressed where the rest is defining the button value. I can't redefine the button values because I'm trying to duplicate the functionality of a keypad.

Thanks for the help!

Using pseudo code, do you think something like this would work?

default sendvalue = 11111111 11111111
button = 11111111 00000001
start mask = 10000000 00000000

if button pressed
sendvalue = button

if sendvalue & mask
transmit HIGH
else
transmit LOW

mask = mask >> 1

if !mask
mask = start mask
sendvalue = default button

what I'm trying to do is if no button is pressed it always sends 1's. Once a button is pressed it sends the correct button code.

I couldn't sleep. :smiley:

I can't redefine the button values because I'm trying to duplicate the functionality of a keypad.

What keypad are you trying to "duplicate the functionality of", that requires a constant stream of ones to be sent when no keys are pressed?

What is the device you're sending the keypresses to? Another Arduino?

What are the "virtual buttons"?

One thing I can say for sure - the String class definitely has no place in this.

It would be nice if you could accurately describe exactly what you're trying to do.

If you're already sending a constant stream of 1s with no key pressed, how will the receiving device know when the first 8 bits of the 16-bit value are arriving? Won't they be detected as "no key pressed", just like the constant stream when no key is pressed.

How many (virtual) keys will there be?

I'm trying to build a virtual keypad to connect to a security panel. The way the system works is the clock goes high for a longer period to signal sync the data stream. The all 1's from a keypad signals "idle".

I'm looking at emulating 16 keys. (the normal keypad has more then that but I only need that many for what I'm doing)

the first 8 bits of 1s indicates that the message is coming from a keypad. the panel messages have other codes that I'm working on figuring out.

I do appreciate the help!

Essentially how it works is the data is read on the clock high and written on the clock low.

Using pseudo code, do you think something like this would work?

If you shift the mask and write the bit on the clock low it should work.

Do you have documented serial communication protocol for the security system? Who controls the clock--the Arduino or the Security system? How fast is the clock?

Thanks! There is no documentation at all on the protocol so I'm working through the reverse engineering. I have scoped the signal and found others that have done some work on it so I'm making progress.

The clock is controlled by the panel. Arduino just functions as a slave off it like a normal security keypad.