Pages: [1]   Go Down
Author Topic: Doubts about the BYTE command  (Read 691 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!

as by the title, I have a doubt about the BYTE command.

I know that, as of Arduino 1.0, it should be substituted by the Serial.write () command, but does this apply to every situation? I mean, if I use the BYTE command to declare a variable (as in byte variableName) should I just use Serial.write (variableName) instead?

I've been looking in many places about this, but couldn't figure it out by myself

paolo
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50888
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the argument passed to the Serial.write() function is of type byte, it is not necessary to cast the argument as a byte in the call.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS - maybe my question was not that clear though!

I'll try to explain myself better with the actual situation:

the following line is at the beginning of a sketch I've found on the internet for use with an accelerometer:

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device

however, when verifying the code, the debugger prompts me to substitute the BYTE command with the Serial.write() one, so should I type     Serial.write(buff[TO_READ])     instead of the above line?

thanks!
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably, the error message does not relate to the code you're looking at. You need to post the whole sketch and the error message produced when you compile it.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no, BYTE (in caps) was a macro that was usually passed to the Serial.print commands to print an actual number rather than a character. If the compiler is warning you about byte (lowercase), ignore it. If it's error'ing you, post your code that doesn't compile. I've used byte (lowercase) many times in 1.0
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the following line is at the beginning of a sketch I've found on the internet for use with an accelerometer:

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device

however, when verifying the code, the debugger prompts me to substitute the BYTE command with the Serial.write() one

It's always nice to post code, and the actual error message. Is this it?

Code:
const byte TO_READ = 6;

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device

void setup ()
{
  Serial.write(buff);
}

void loop () {}

Message:

Code:
sketch_jan28a.cpp: In function 'void setup()':
sketch_jan28a:6: error: call of overloaded 'write(byte [6])' is ambiguous
/Applications/Arduino 1.0.app/Contents/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.h:58: note: candidates are: virtual size_t HardwareSerial::write(uint8_t) <near match>
/Applications/Arduino 1.0.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note:                 size_t Print::write(const char*) <near match>

Try:

Code:
const byte TO_READ = 6;

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device

void setup ()
{
  Serial.write(buff, TO_READ);
}

void loop () {}

Although I'm not sure TO_READ is a good name for the number of bytes you are planning to write, but whatever.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for the replies. First of all, here's the (first part of) the code I'm working on:

Code:
#include <Wire.h>

#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

byte buff[TO_READ]; //6 bytes buffer for saving data read from the device
char str[512];                      //string buffer to transform data before sending it to the serial port

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output

  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);     
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}

void loop()
{
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  int x, y, z;

  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345

  //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  //thus we are converting both bytes in to one int
  x = (((int)buff[1]) << 8) | buff[0];   
  y = (((int)buff[3])<< 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];

  //we send the x y z values as a string to the serial port
  sprintf(str, "%d %d %d", x, y, z); 
  Serial.print(str);
  Serial.print(10, BYTE);

  //It appears that delay is needed in order not to clog the port
  delay(50);
}


And the error message:

Code:
ADXL345_Example.cpp: In function 'void loop()':
ADXL345_Example.pde:-1: error: 'BYTE' was not declared in this scope

As of Arduino 1.0, the 'BYTE' keyword is no longer supported.
Please use Serial.write() instead.

ADXL345_Example.cpp: In function 'void writeTo(int, byte, byte)':
ADXL345_Example.pde:-1: error: 'class TwoWire' has no member named 'send'

As of Arduino 1.0, the Wire.send() function was renamed to Wire.write() for consistency with other libraries.

ADXL345_Example.pde:-1: error: 'class TwoWire' has no member named 'send'

As of Arduino 1.0, the Wire.send() function was renamed to Wire.write() for consistency with other libraries.

ADXL345_Example.cpp: In function 'void readFrom(int, byte, int, byte*)':
ADXL345_Example.pde:-1: error: 'class TwoWire' has no member named 'send'

As of Arduino 1.0, the Wire.send() function was renamed to Wire.write() for consistency with other libraries.

ADXL345_Example.pde:-1: error: 'class TwoWire' has no member named 'receive'

As of Arduino 1.0, the Wire.receive() function was renamed to Wire.read() for consistency with other libraries.

What I get out of it + your replies:

Quote
Probably, the error message does not relate to the code you're looking at.

Yes, than you! I can see now that the error relates to the Serial.print(10, BYTE) near the end of the code and not to the lower case byte at the beginning of the sketch.

Quote
no, BYTE (in caps) was a macro that was usually passed to the Serial.print commands to print an actual number rather than a character. If the compiler is warning you about byte (lowercase), ignore it. If it's error'ing you, post your code that doesn't compile. I've used byte (lowercase) many times in 1.0

thanks - this clears my doubt

Quote
It's always nice to post code, and the actual error message. Is this it?

It is not that, but thanks for the hint on posting (I hadn't seen the 'code posting' button, so didn't know how to correctly post code  smiley-red )

Quote
Although I'm not sure TO_READ is a good name for the number of bytes you are planning to write, but whatever.

As you can see in the code, the value of TO_READ was defined earlier, don't know if that's a valid argument though.

Thank you again!
Paolo


Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like the error message nailed it, replace
Code:
  Serial.print(10, BYTE);
with
Code:
  Serial.write(10);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50888
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The replacement code needs to be
Code:
Serial.write((byte)10);
Otherwise the compiler will complain that 10 is an int, and it doesn't know which version of write() you want to call.
Logged

Pages: [1]   Go Up
Jump to: