Store 2 bytes as an int

Hi guys, I am bitbanging a 16 bit shift register and i would like to store the 2 bytes of information to be sent as an int.

I have the following code:

void Display (uint8_t first, uint8_t second)
{
  digitalWrite(VDR, LOW);
  for ( int i = 0; i < 8; i++) {
    if ( (first & 0x80) == 0) {
      digitalWrite(Data, LOW);
    }
    else {
      digitalWrite(Data, HIGH);
    }
    digitalWrite(CLK, LOW); // toggle clock
    digitalWrite(CLK, HIGH);
    first = first << 1;
  }
  for ( int i = 0; i < 8; i++) {
    if ( (second & 0x80) == 0) {
      digitalWrite(Data, LOW);
    }
    else {
      digitalWrite(Data, HIGH);
    }
    digitalWrite(CLK, LOW); // toggle clock
    digitalWrite(CLK, HIGH);
    second = second << 1;
  }
  digitalWrite(VDR, HIGH);
}
 if (x_position < Left && y_position > Up)
    //First
  {
    Display(B10000000, B00001010);
  }

  if (x_position < Left && y_position < Down)
    //Second
  {
    Display(B00100111, B00100111);
  }

I would like to do something like this:

int firstGear = (B10000000, B00001010)


//...//


 if (x_position < Left && y_position > Up)
    //First
  {
    Display(firstGear);
  }

So the question is how can i store two individual bytes under a singular int?

Take a look here

BTW hex notation is easier than Arduino's non-standard Bxxx binary notation. Or use 0bxxx, if you must.

Hi AWOL, still vauge as ever, you really like making people work for it yeah :wink:

So it should look something like this?

int firstGear = 1000000000001010
//or int firstGear = 0x800A


//...//


 if (x_position < Left && y_position > Up)
    //First
  {
    Display(highByte(firstGear));
    Display(lowByte(firstGear));
  }

Please note, all binary will be changed to hex soon, its just easier for me with binary at the moment as i have each bit mapped to a segment on the display.

You say "work", I say "learn".

Potato, potato, tomato, tomato.

i have each bit mapped to a segment on the display.

In that case, named macros would make understanding and maintenance simpler than trying to pick though hex or binary.

#define SEG_A 0x01
#define SEG_B 0x02
....

#define ARBITRARY_PATTERN SEG_A | SEG_B

etc.

It could be a good idea, but in any case I still have 16 bits I gotta get banged out for each gear position. I like the idea of having the code say if gearstick is up left display first gear. It’s nice and self explanatory.
Just not sure how to do it. Can we rewind to the previous post, am I going in the right direction?

The simplest way to test it is to cut down your sketch to just the display driver, and try it.

It could form the basis of a useful test tool.

0xaa55 - hex
0b1010101001010101 - binary

byte gear1 = 0x11;
byte gear2 = 0xaa;
int gearCombo = (gear1<<8) || gear2; // shift gear1 into upper byte,  bitwise OR gear2 into lower byte

Appreciate the help, I actually solved the problem another way, which also allowed me to reduce the code by a few lines :slight_smile:

I changed the bitbang to the following in order to send out 16 bits instead of 2x 8 bits:

 void Display (uint16_t val) //Bitbang to display driver
{
digitalWrite(VDR, LOW); //set latch low
for ( int i = 0; i < 16; i++) //start bangin' bits
{
if ( (val & mask) == 0) {
digitalWrite(Data, LOW);
}
else {
digitalWrite(Data, HIGH);
}
digitalWrite(CLK, LOW); // toggle clock
digitalWrite(CLK, HIGH);
val = val << 1;
}
digitalWrite(VDR, HIGH); //set latch high
}

Which allowed me to write this:

 unsigned int mask = 0x8000; //For Bitbang
unsigned int Neutral = 0xC2C2; //Each "gear" int stores 2 bytes of information in hex to be sent to display driver via Bitbang
unsigned int firstGear = 0x800A;
unsigned int secondGear = 0x2727;
unsigned int thirdGear = 0xA527;
unsigned int fourthGear = 0x8183;
unsigned int fifthGear = 0xA5A5;
unsigned int Reverse = 0x43A7;

Which allowed me to do this:

 if (y_position < Up && y_position > Down) //display gear according to x and y position of gearstick
{
Display(Neutral);
}
if (x_position < Left && y_position > Up)
{
Display(firstGear);
}
if (x_position < Left && y_position < Down)
{
Display(secondGear);
}
if (x_position > Left && x_position < Right && y_position > Up)
{
Display(thirdGear);
}
if (x_position > Left && x_position < Right && y_position < Down)
{
Display(fourthGear);
}
if (x_position > Right && y_position > Up)
{
Display(fifthGear);
}
if (x_position > Right && y_position < Down)
{
Display(Reverse);
}

So, mission accomplished 8)