Assistance with binary counter

I am not very good at programming. I have from the net obtained a piece of code and modified it slightly to get what I want. Basically, I am trying to count upwards from 0 to 127 in binary.

At the bit level it seems to be working. However at the end of forming a new 7 bit number, I want to be able to take all those 7 bits and write it into an address string for further manipulation.

While my code does not throw any errors, I am not getting any output where I try to Serial print the address string.

The second question I have is when the program runs, in the serial monitor past the 100 0000 stage, I start to get a funny character leading the 7 bit number. Why is that?

Any help would be much appreciated.
Binary_Counter.ino (1.0 KB)

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

Please post your code here rather than attaching it and please use code tags when you do

all calculations are binary

1 Like
void setup() {
  Serial.begin(115200);
}

void loop() {
  static uint8_t Number = 0;
  for (uint8_t i = 8; i > 0; i--)
    Serial.print(Number & 1 << (i - 1));
  Serial.println();
  Number++;
  if (Number > 127)Number = 0;
  delay(500);
}

Thank you for your reply.

I am afraid irrespective of the count number, I want the binary output appearing to be 7 bits long (padded with zeros if required). So a 2 should appear as 000 0010. Your code give it as just 10.

Once that problem is solved, could you also advice how I could get those 7 bits written into a an array of characters ( which I have called as address)?

Basically I am going to use this address to scan a for devices on a network and that is the end use. Just so that you know.

already done.
and now with array
upd
wait, sometning wrong

I think that 'Serial.print' suppresses leading zeros (ignores them) so '10' is actually '0000 0010', the same thing.

Thank you for your reply.

While I understand the Serial.print suppresses the leading zeros, and I am okay with it not appearing on the serial monitor as I am using Serial.print merely to debug the code.

However, I need to be able to capture the the leading zeros to be able to write the entire 7 bit number into an array of 7 characters.

This array then forms the address of the device I intend to search for (during my scan). The scan itself will be done by reading each of the 7 characters and forming a high or a low pulse on a serial bus.

So if i have not confused you already, the leading zeros are important for me to ensure correct pulse formation when I do a device search.

Yes there is a tiny error somewhere.
This is the output I am getting

00000000
00000001
00000020
00000021
00000400
00000401
00000420
00000421
00008000
00008001
00008020
00008021

Nearly there...

char Adderes [8] = {0};
void setup() {
  Serial.begin(115200);
}

void loop() {
  static uint8_t Number = 0;
  for (uint8_t i = 8; i > 0; i--) {
    Serial.print(Number & 1 << i - 1?1:0);
    Adderes[i] = (Number & 1 << (i - 1))?1:0;
  }
  Serial.println();
  Number++;
  if (Number > 127)Number = 0;
  delay(500);
}



Thank you . This is much better.

However, I have one more question.

I notice by putting it in a loop (8 times), you are serial.printing the number to form the 8 bit number. The same bit is there after stored in the address[i] position as well.
I get that.

Why is it that after the loop of 8 (modified to 7) is over, after your Serial.println command,
I am not able to print the contents of the entire address array with a
Serial.print(Adderes); command? << In fact this throws up an error.

So my question is how do I print the entire Adderes?

char Adderes [7] = {0};
void setup() 
{
  Serial.begin(115200);
}

void loop() {
  static uint8_t Number = 0;
  for (uint8_t i = 7; i > 0; i--) {
    Serial.print(Number & 1 << i - 1?1:0);
    Adderes[i] = (Number & 1 << (i - 1))?1:0;
  }
  Serial.println();
  
  **Serial.print(Adderes);** 
  
  Number++;
  if (Number > 127)Number = 0;
  delay(500);
}

char Address [8] = {0};

void setup() {
  Serial.begin(115200);
}

void loop() {
  static uint8_t Number = 0;
  for (uint8_t i = 0; i < 8; i++) {
    bool Bit = Number & 1 << (7 - i);
    Serial.print(Bit);
    Address[i] = Bit;
  }
  Serial.println();
  Number++;
  if (Number > 127)Number = 0;
  for (uint8_t i = 0; i < 8; i++)Serial.print(Address[i], DEC);
  Serial.println("\r\n");
  delay(500);
}

Perfect !! Just what I wanted.
Nice to be a part of an open source community where so much of expertise is handy so quickly.

Thank you so much .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.