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?
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.
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.
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);
}