Go Down

Topic: Printing byte prints four bytes (Read 45 times) previous topic - next topic

vagulus

I have this debug output sequence in my code
Code: [Select]

// C++ does not use named parameters so, to make things more clear,
//  we get the parameter values individually.
// the 0..9 range references the variable coordinateByte[]
row = random(0, 9);
column = random(0, 9);

// interval is any old time lapse
interval = random(200, 2000);

if (DEBUG)
{
Serial.print("row number is ");
Serial.println(row, DEC);
Serial.print("row pattern is ");
Serial.println(~coordinateByte[row], BIN);
Serial.print("column number is ");
Serial.println(column, DEC);
Serial.print("column pattern is ");
Serial.println(coordinateByte[column], BIN);
Serial.print("interval period is ");
Serial.print(interval, DEC);
Serial.println(" ms");
Serial.println();
}


(coordinateByte[] is defined as

// define data to write to matrix
int coordinateByte[] = {0, 1, 2, 4, 8, 16, 32, 64, 128 };


The debug printout I get is
Code: [Select]

Opening port
Port open
row pattern is 11111111111111111111111111110111
column pattern is 1
interval period is 353 ms

row pattern is 11111111111111111111111111101111
column pattern is 10000
interval period is 988 ms

row pattern is 11111111111111111111111111011111
column pattern is 100000
interval period is 1763 ms

row pattern is 11111111111111111111111101111111
column pattern is 10000
interval period is 1573 ms

row pattern is 11111111111111111111111101111111
column pattern is 1000
interval period is 216 ms

row pattern is 11111111111111111111111111110111
column pattern is 10000000
interval period is 358 ms


Why does row pattern show as four bytes?  Is it that feral tilde ('~')?
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977)

6v6gt

I'm a bit confused by your variable names and an apparent mismatch between the debug statements in your code snippet and the output generated.

Usually, it is best to post your entire code.

It is true that an int datatype on an Arduino occupies two bytes. If you are doing bit manipulation, then you are better off using unsigned int rather than int.  You can try casting the expression ~myInt to an integer type of your choice before printing it, say (unsigned int) ~myInt

vagulus

It is true that an int datatype on an Arduino occupies two bytes.
I changed the type to byte, but that didn't fix it.

FTR: deleting the feral '~' fixed it.  I still don't understand why the tilde was there.


This code is modified from Boxall, J. (2013)  Arduino Workshop. San Francisco (CA) USA: No Starch Press. p.143
Code: [Select]

// debug switch
const bool DEBUG = true;

// specify pins
#define dataPin 6 // connect to pin 14 on 74HC595
#define latchPin 8 // connect to pin 12 on 74HC595
#define clockPin 11 // connect to pin 11 on 74HC595

// define data to write to matrix
byte coordinateByte[] = {0, 1, 2, 4, 8, 16, 32, 64, 128 };

// matrix position variables
int row, column, interval = 0;


/*==========================================*/


// forward declaration(s)

// send lighting pattern to matrix
void setLEDs(int row, int column, int interval);


/*==========================================*/


void setup()
{
// start Serial
Serial.begin(9600);

// set up pin status
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

// set up random seed
randomSeed(analogRead(0));
}  // end setup()

void loop()
{
// C++ does not use named parameters so, to make things more clear,
//  we get the parameter values individually.
// the 0..9 range references the variable coordinateByte[]
row = random(0, 9);
column = random(0, 9);

// interval is any old time lapse
interval = random(200, 2000);

if (DEBUG)
{
Serial.print("row array number is ");
Serial.println(row, DEC);
Serial.print("row pattern is ");
Serial.println(coordinateByte[row], BIN);
Serial.print("column array number is ");
Serial.println(column, DEC);
Serial.print("column pattern is ");
Serial.println(coordinateByte[column], BIN);
Serial.print("interval period is ");
Serial.print(interval, DEC);
Serial.println(" ms");
Serial.println();
}

// send lighting pattern to matrix
setLEDs(row, column, interval);
}  // end loop()


/*==========================================*/


// send lighting pattern to matrix
void setLEDs(int row, int column, int interval)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, coordinateByte[column]);
shiftOut(dataPin, clockPin, MSBFIRST, coordinateByte[row]);
digitalWrite(latchPin, HIGH);
delay(interval);
}  //  end setLEDs()



It gives the debug printout
Code: [Select]

Opening port
Port open
row array number is 0
row pattern is 0
column array number is 2
column pattern is 10
interval period is 855 ms

row array number is 8
row pattern is 10000000
column array number is 6
column pattern is 100000
interval period is 870 ms

row array number is 0
row pattern is 0
column array number is 2
column pattern is 10
interval period is 1065 ms

row array number is 8
row pattern is 10000000
column array number is 2
column pattern is 10
interval period is 1705 ms

row array number is 4
row pattern is 1000
column array number is 5
column pattern is 10000
interval period is 665 ms

row array number is 7
row pattern is 1000000
column array number is 5
column pattern is 10000
interval period is 1943 ms


BTW: the identifier coordinateBtye uses the word coordinate as in Cartesian coordinate.  That might be the confusion with the identifiers.
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977)

UKHeliBob

Make row an unsigned int and try again.

From the reference page for ~ (bitwise NOT)
Quote
At times, the sign bit in a signed integer expression can cause some unwanted surprises.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up