Int to Byte array

Hello,
My goal is to be able to read potentiometer position, map it to 0 - 255 values and shift out byte array of its int value.

Is there a simple way I can do conversation?

My goal is to be able to read potentiometer position, map it to 0 - 255 values and shift out byte array of its int value.

Once you map the value to the range 0 to 255, the value is a byte. There is no "mapping" needed, nor is there a need to shift out an int value.

int potVal = analogRead(potPin);
byte shiftoutVal = potVal / 4;

How can I get it like " byte blu[] = {0,0,0,0,0,0,1,1}; "
That I can digitalWrite it with FOR LOOP?

digitalWrite implies an I/O pin, but what you want is a simple assignment to an array element, it seems to me.

Sorry, maybe this will help to explain my problem.

Right now I have write it so far and it do exactly what I want it to do. But further I want to change red array values depending on pot int value.

byte red[] = {0,0,0,0,1,0,0,0};

for (int i=0; i < 8; i++)
{if (red*==1) {digitalWrite(13,HIGH);digitalWrite(13,HIGH);digitalWrite(13,LOW);}*
else {digitalWrite(13,HIGH);digitalWrite(13,LOW);digitalWrite(13,LOW);}}

look at bitRead()

(but if you are shifting out a byte to a shift register, you wouldn't need it)

  int myArray[8];
  for (byte i = o; i < 8; i++)
  {
    myArray[i] = bitRead(myByte, i);
  }

But I suppose , if I it reads myInt = 1, it will save array like this = (1), not like = (0,0,0,0,0,0,0,1)?

But I suppose , if I it reads myInt = 1, it will save array like this = (1), not like = (0,0,0,0,0,0,0,1)?

What will?

  byte myByte = 1;
  byte myArray[8];
  for (byte i = o; i < 8; i++)
  {
    myArray[i] = bitRead(myByte, i);
  }

it the equivalent of :

byte myArray[8] = {1,0,0,0,0,0,0,0};

because bit zero is assigned to position zero in the array

  for (byte i = o; i < 8; i++)

Where is o defined?

PaulS:

  for (byte i = o; i < 8; i++)

Where is o defined?

Webster's:

an error (as of spelling) in typed or typeset material

Webster's:

an error (as of spelling) in typed or typeset material

O, I see.

BulldogLowell:

  byte myByte = 1;

byte myArray[8];
  for (byte i = o; i < 8; i++)
  {
    myArray[i] = bitRead(myByte, i);
  }




it the equivalent of :



byte myArray[8] = {1,0,0,0,0,0,0,0};




because bit zero is assigned to position zero in the array

Thanks, it works. 1st time I get strange results, due to byte is inverted.
Thanks, super :wink:

Why I this part do not work? How I can get proper int to byte array conversation? :slight_smile:

byte myByte = 1;
byte myArray[8];
for (byte i = 8; i == 0; i--)
{
myArray = bitRead(myByte, i);

  • }*

Why I this part do not work?

It DOES work. That what it does is not what you want is not a reason to say that it doesn't work.

What does it actually do? How does that differ from what you want?

Why can't you use code tags so the forum software doesn't mangle your code?

  byte myByte = 1;
  byte myArray[8];
  for (byte i = 0; i < 8; i++)
  {
    myArray = bitRead(myByte, 7-i);
  }
byte myArray[] = {0,0,0,0,0,0,0,1};

BTW, an array of eight elements has eight locations, zero through seven.

Thanks both of you, now it is working like a charm. :wink:

  byte myByte = 1;
  byte myArray[8];
  for (byte i = 0; i < 8; i++)
  {
    myArray[i] = bitRead(myByte, 7-i);
  }

it is working like a charm

Meaning it works at best 50% of the time?

uint8_t         myByte  = 1;

const size_t    COUNT_BITS = sizeof(myByte) * CHAR_BIT;
uint8_t         array[COUNT_BITS];

for ( size_t mask = (1 << COUNT_BITS), i = 0; mask >>= 1; i++ )
{
    array[i] = ((myByte & mask) ? 1 : 0);
}