count every bit

I want to write a program, but I need to count every incoming bit and put it into an array. What kind of code do I need to write to achieve this?

Either C or C++ will do the job.

Set up a counter (initialized to zero) and then every time a bit comes in, just increment the counter. When all the bits that are going to arrive have, just examine the counter to see how many bits arrived.

xxninjabunnyxx:
I want to write a program, but I need to count every incoming bit and put it into an array. What kind of code do I need to write to achieve this?

What is a bit?

What is a bit?

1/8th of a byte.

what kind of code do I need to write for the arduino to read the pin I want.

int analagValue = analogRead(someAnalogPin);
int digitalValue = digitalRead(someDigitalPin);

PaulS:

int analagValue = analogRead(someAnalogPin);

int digitalValue = digitalRead(someDigitalPin);

jawsome!! thanks!!

I take it you haven't looked at any of the example sketches in the IDE then.

What was that other thread saying about people being incapable of doing any research?


Rob

What was that other thread saying about people being incapable of doing any research?

Hey, could you find that, and read it for me. I need a summary by later today. Any help would be much appreciated.

This would make a lot more sense if he said "pulse counting".

Expect more questions.

For the OP, just in case it is bits and not pulses, check the Bits and Bytes section of this page:

And if Murphy is watching, it will be pulses the OP wants.

i want to count the incoming bits from a serial connection, not pulses.

i want to count the incoming bits from a serial connection, not pulses.

So, how many bits in a 'Z'?

PaulS:

i want to count the incoming bits from a serial connection, not pulses.

So, how many bits in a 'Z'?

I want to read the serial input, alter it, and output a new serial connection.

I want to read the serial input, alter it, and output a new serial connection.

What does this have to do with counting bits? Seems like a much more reasonable thing to do, but it does not seem related to the thread title.

untested:

byte B = 'Z';
byte count = 0;
while ( B ) {
count += B & 1;
B = B >> 1; // right shift of unsigned integers does not do sign extension, as a char value would
}

I leave the rest up to the bunny..........

xxninjabunnyxx:

PaulS:

i want to count the incoming bits from a serial connection, not pulses.

So, how many bits in a 'Z'?

I want to read the serial input, alter it, and output a new serial connection.

I think that your requirements would be stated better if you just stated you need a function that counts the number of bits set in a integer or byte variable. Data received from the serial input function will return the value as a variable. The fact that the value in the variable originated from a serial stream is not germain to the requirement, and only adds confusion to your requirement as read by others. I suspect that there is probably a function already avalible in the AVRlib or some other C standard library? I'm pretty sure I once ran across such a function in one of Donald Knuth's, The art of programming series.

Lefty

i want to make a converter like the one that is used in that video, but i want to make it for different systems.

Code for counting 1-bits in a byte:

unsigned char bitcount(unsigned char n)
{  unsigned char count = 0 ;
   while (n)
   {  count++ ;
      n &= (n - 1);
   }
   return count;
}