bytes inside an integer array? how come?

hi!

I found this code, and it's working just fine, for controlling a small 5v stepper motor:

// This Arduino example demonstrates bidirectional operation of a 
// 28BYJ-48, using a ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the 
// operating Frequency is 100pps. Current draw is 92mA. 
////////////////////////////////////////////////

//declare variables for the motor pins
int motorPin1 = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
  if(count < countsperrev )
    clockwise();
  else if (count == countsperrev * 2)
    count = 0;
  else
    anticlockwise();
  count++;
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
  Serial.println(bitRead(lookup[out], 0));
  delay(10);
}

so the part I'm not quite getting, is this:

int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

I know that's an array, and I know the basics of arrays, but I don't quite understand how it's possible to make an integer array and then store byte values inside of it. I mean "B" obviously is no integer.

And one more thing.
In the end of the code, inide "void setOutput", the array is being accessed starting with index number 7 and going down to 0 (reading all the 8 bytes in our array. The other thing that confuses me a bit here, is that the specific bit in each byte that we are reading (with "bitRead") is 0, 1, 2, and 3. So then (according to my logic, which obviously must be wrong), if we start of with the first byte in our array, we are reading: B, 0, 1, and 0. If that's correct understand, then I would like to know how it's possible to send "B" with digitalWrite?

I hope this made sense.
Please just ask if I'm not explaining myself good enough.

Regards

The "B" term in the initializer list is a binary formatter and tells the compiler that what follows is represented as binary data. See:

http://arduino.cc/en/Reference/Byte

int lookup[8] = {B01000 ...

Is the same as:

int lookup[8] = {B0000000000001000 ...

while

byte lookup[8] = {B01000 ...

is the same as:

byte lookup[8] = {B00001000 ...

econjack:
The "B" term in the initializer list is a binary formatter and tells the compiler that what follows is represented as binary data. See:

http://arduino.cc/en/Reference/Byte

thanks, but I've already looked there. the arduino byte-reference doesn't say anything about what a binary formatter actually is.

guix:

int lookup[8] = {B01000 ...

Is the same as:

int lookup[8] = {B0000000000001000 ...

while

byte lookup[8] = {B01000 ...

is the same as:

byte lookup[8] = {B00001000 ...

so all the first 0's (before the first 1) are irrelevant?

Code:

int lookup[8] = {B01000 ...

Is the same as:
Code:

int lookup[8] = {B0000000000001000 ...

No, it isn't, because the second one will give you a compile error :wink:

Generally leading zeroes will be ignored irrespective of the format (except for octal base specifiers), but the "Bxxx" is an Arduino-ism and only good for up to eight bits.

the arduino byte-reference doesn't say anything about what a binary formatter actually is.

Perhaps not, but I did tell you that its purpose is to tell the compiler how a piece of data is being represented in the definition. In the statement:

int b = B01010;   // B for the binary formatter

the "B" says the data is the binary representation of the value (i.e., decimal value 10). You could also say:

int b = 10;         // No formatter defaults to decimal

and the compiler still assigns 10 into variable b. You could also use:

int b = 0x0A;      // Use hexadecimal formatter

and the compiler knows that the data are stated using a hexadecimal numbering system because of the hex formatter ("0x"). As I said, the binary formatter "B" is simply a message to the compiler to help it figure out what numbering system you are using to initialize the variable.

If you want a degree of portability, I suggest you forget Bxxx, and use 0bxxx instead - whilst it isn't completely standard like 0x... is, at least it won't trip you up if you go over eight bits.

econjack:
the arduino byte-reference doesn't say anything about what a binary formatter actually is.

Perhaps not, but I did tell you that its purpose is to tell the compiler how a piece of data is being represented in the definition. In the statement:

int b = B01010;   // B for the binary formatter

the "B" says the data is the binary representation of the value (i.e., decimal value 10). You could also say:

int b = 10;         // No formatter defaults to decimal

and the compiler still assigns 10 into variable b. You could also use:

int b = 0x0A;      // Use hexadecimal formatter

and the compiler knows that the data are stated using a hexadecimal numbering system because of the hex formatter ("0x"). As I said, the binary formatter "B" is simply a message to the compiler to help it figure out what numbering system you are using to initialize the variable.

thanks! that really made sense :slight_smile: