iterate over bits on one pin?

I was looking at this:

and I tried the example code, and it doesn't seem to work.

byte transmit = 13; //define our transmit pin
byte data = 170; //value to transmit, binary 10101010
byte mask = 1; //our bitmask
byte bitDelay = 1000;

void setup()
{
   pinMode(transmit,OUTPUT);
}

void loop()
{
  for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
    if (data & mask){ // if bitwise AND resolves to true
      digitalWrite(transmit,HIGH); // send 1
    }
    else{ //if bitwise and resolves to false
      digitalWrite(transmit,LOW); // send 0
    }
    delayMicroseconds(bitDelay); //delay
  }
}

The only thing I changed was the pin (13, it has an LED on my Uno.) and the delay, since I wanted to see the LED change. Did I do something wrong? Or is the example code missing some detail?

Basically what I want to do is iterate over bytes on one pin, while I clock another pin up and down.

Thanks,

jimmy

mixographer:
and it doesn't seem to work.

When you take your car to the mechanic, do you just hand them the keys and say "it doesn't work" and then walk away?

When you take your car to the mechanic, do you just hand them the keys and say "it doesn't work" and then walk away?

That's CLASSIC ! I copy and pasted it into a notepad file so I can use it for future newbie posts.

"iterate over bits on one pin"? What the heck does that mean? A pin might conceivably represent one bit.

I'm not sure what my mechanic has to do with anything. But I'm glad you took the time to help.

Jimmy

I mean send the bits serially on one pin. Not put one bit on each pin. I don't know why this question is so much more stupid than all my other posts, or why it is drawing such derision.

I'm not sure what my mechanic has to do with anything.

If you take your car to a mechanic, you have to explain what the car is doing, and how that differs from what you want. You seemed to have left out a few details from your post. Like what the code actually does, and what you expect it to do.

I don't know why this question is so much more stupid than all my other posts

That was YOUR conclusion, keep in mind.

I thought the code would write High and Low to pin 13 in sequence, thus blinking the LED on 13. At least that was the way I read the tutorial.
(edit: oh yeah, the code doesn't do anything that I can tell.)

My goal is to send data bit by bit out one pin (serially, and I'll use another pin to clock it by writing that pin high and low alternately.)

Thanks,

Jimmy

The code does blink the LEd it's just that your to slow to see it.

    delayMicroseconds(bitDelay); //delay

Mark

My goal is to send data bit by bit out one pin (serially, and I'll use another pin to clock it by writing that pin high and low alternately.)

Did you know shiftOut() does this already?
And shiftIn() for bringing bits back in?
If you want to do it really fast, 8 MHz rates, then SPI.transfer() is the way to go.

Your code works fine. It turns the LED on for 1 ms, off for 1 ms, etc.

Thanks Mark, Lar3y and CrossRoads, for looking at the code and seeing what I didn't.

CrossRoads, I'll look at shiftOut, I've only ever looked at it when working with shift registers, and I guess that's what my LED display is acting like.

Those responses were very helpful. Cheers,

Jimmy

mixographer:
The only thing I changed was the pin (13, it has an LED on my Uno.) and the delay, since I wanted to see the LED change. Did I do something wrong? Or is the example code missing some detail?

Something like this may be what you need:

#define LED_PIN 13
uint8_t pattern;

pattern = 0b01010101; // off/on/off/on/etc....
bits_out (pattern); // send it
delay (1000); // wait between demos

pattern = 0b11110000; // long on, long off
bits_out (pattern);
delay (1000); // wait between demos

void bits_out (uint8_t data)
{
    uint8_t bits = 8;
    while (bits--) {
        digitalWrite (LED_PIN, data & _BV (bits) ? HIGH : LOW); // LED on or off depending on bit
        delay (250); // delay between bits so you can see the blinks
    }
}

For longer runs of bits, you can change the "uint8_t data" to uint16_t, uint32_t or even uint64_t (and change "bits" to 16, 32 or 64 as necessary).

Hope this helps.

Thanks to everyone who helped me out with this. I now have my Chess 960 position generator working on a ATTiny85 and a SCDQ5584 LED display. There are some libraries for driving the displays, but using the Tiny85 required me to save space and "iterate over bits on one pin." I may have re-written shiftOut() in the process, but it works and I understand how it's working, so that is nice. It's really cool to get this project down to the Tiny from the Atmega328 I originally wrote it on.

So thanks very much!
(Edit: and Karma points given out, too)

Jimmy