boolean (8 bit) - simple logical true/false
 byte (8 bit) - unsigned number from 0-255
 char (8 bit) - signed number from -128 to 127.
 unsigned char (8 bit) - same as ‘byte’
 word (16 bit) - unsigned number from 0-65535
 unsigned int (16 bit)- the same as ‘word’.
 int (16 bit) - signed number from -32768 to 32767.
 unsigned long (32 bit) - unsigned number from 0-4,294,967,295.
 long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
 float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38.



資料型態 說明 記憶體長度 數值範圍
boolean 布林 8 bits true (1, HIGH), false (0, LOW)
byte 位元組 8 bits 0~255
char 字元 8 bits -128~127
short 短整數 16 bits -32768~32767
int 整數 16 bits -32768~32767
word 字 16 bits 0~65535
long 長整數 32 bits -2147483648~2147483647
float 浮點數 32 bits +-3.4028235e+38
double 倍精度浮點數 32 bits >+-3.4028235e+38


C,C++ 跨平台:  (現代 C 語言的寫法)

帶正負號的整數使用 int8_t、int16_t、int32_t、int64_t
不帶號的整數使用 uint8_t、uint16_t、uint32_t、uint64_t
32 與 64 位元長的浮點數使用 float 與 double



void setup() {   
  Serial.begin(9600);
}

void loop() {
 Serial.println("--- Variable type: Size in SRAM (bytes) ---");

/*
  C standard data type "unsigned int length 8 bits".
  Occupies one byte of memory.
  http://arduino.cc/forum/index.php/topic,41590.0.html
  http://en.wikipedia.org/wiki/Stdint.h#Fixed_width_integer_types
*/
 Serial.print("uint8_t: ");
 Serial.println(sizeof(uint8_t));

/*
 A boolean holds one of two values, true or false.
 Each boolean variable occupies one byte of memory.
 http://arduino.cc/en/Reference/BooleanVariables
*/
 Serial.print("boolean: ");
 Serial.println(sizeof(boolean));
/*
 A data type that takes up 1 byte of memory that stores a character value.
 Character literals are written in single quotes, like this: 'A' (for multiple
 characters - strings - use double quotes: "ABC").
 http://arduino.cc/en/Reference/Char
*/
 Serial.print("char: ");
 Serial.println(sizeof(char));

/*
 An unsigned data type that occupies 1 byte of memory. Same as the byte datatype.
 The unsigned char datatype encodes numbers from 0 to 255.
 For consistency of Arduino programming style, the byte data type is to be preferred.
 http://arduino.cc/en/Reference/UnsignedChar
*/
 Serial.print("unsigned char: ");
 Serial.println(sizeof(unsigned char));
 
/*
 A byte stores an 8-bit unsigned number, from 0 to 255.
 http://arduino.cc/en/Reference/Byte
*/
 Serial.print("byte: ");
 Serial.println(sizeof(byte));
 
/*
 Integers are your primary datatype for number storage, and store a 2 byte value.
 This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
 http://arduino.cc/en/Reference/Int
*/
 Serial.print("int: ");
 Serial.println(sizeof(int));

/*
 Unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value.
 Instead of storing negative numbers however they only store positive values, yielding a
 useful range of 0 to 65,535 (2^16) - 1).
 http://arduino.cc/en/Reference/UnsignedInt
*/
 Serial.print("unsigned int: ");
 Serial.println(sizeof(unsigned int));

 // A word stores a 16-bit unsigned number, from 0 to 65535.
 // Same as an unsigned int.
 // http://arduino.cc/en/Reference/Word
 Serial.print("word: ");
 Serial.println(sizeof(word));
 
/*
 Long variables are extended size variables for number storage, and store 32 bits (4 bytes),
 from -2,147,483,648 to 2,147,483,647.
 http://arduino.cc/en/Reference/Long
*/
 Serial.print("long: ");
 Serial.println(sizeof(long));

/*
 Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes).
 Unlike standard longs unsigned longs won't store negative numbers, making their range from 0
 to 4,294,967,295 (2^32 - 1).
 http://arduino.cc/en/Reference/UnsignedLong
*/
 Serial.print("unsigned long: ");
 Serial.println(sizeof(unsigned long));

/*
 Datatype for floating-point numbers, a number that has a decimal point.
 Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38.
 They are stored as 32 bits (4 bytes) of information.
 http://arduino.cc/en/Reference/Float
*/
 Serial.print("float: ");
 Serial.println(sizeof(float));
 
/*
 Double precision floating point number. Occupies 4 bytes.
 The double implementation on the Arduino is currently exactly the same as the float,
 with no gain in precision.
 http://arduino.cc/en/Reference/Double
*/
 Serial.print("double: ");
 Serial.println(sizeof(double));
 

 delay(8500);
}








unsigned int a=(data>>4) & 0xF;  //top 4 bits of data
unsigned int b=data & 0xF;// bottom 4 bits of data

Why “& 0xF” in the first line? Because if your data is more than 8 bits,
you want to remove the higher ones from the result (called masking),
leaving only the single nybble of 4 bits.


send the 16-bit value as two 8-bit values
Serial.write(0xCC);//send the start byte
Serial.write(sensorVal>>8 &0xff);//send the higher byte
Serial.write(sensorVal & 0xff);// send the lower byte



uint16_t  data = 1000;
uint8_t  buf[2];
buf[0] = data & 255;
buf[1] = (data >> 8)  & 255;
Serial.write(buf, 2);



使用union




#define Led  12
union uData
{
byte b[2];
int value;
};
union uData data;
#define Led  8
void setup() {
pinMode(Led,OUTPUT);
digitalWrite(Led,0);
data.b[0]=0x34;
data.b[1]=0x12;
if(data.value==0x1234)
digitalWrite(Led,1);
}

void loop() {
  }


2個Arduino
使用Serial.write() 與Serial.read()傳送2 Byte data

傳送端

union uData
{
byte b[2];
int value;
};


union uData data;
void setup() {
Serial.begin(9600);
delay(1000);
}

void loop() {
data.value=0x3456;
 //while(!(UCSR0A & (1<<UDRE0)));// 若UDRE0=0 等待
Serial.write(0x21);//起始 字元
 //while(!(UCSR0A & (1<<UDRE0)));// 若UDRE0=0 等待
Serial.write(data.b[0]);
// while(!(UCSR0A & (1<<UDRE0)));// 若UDRE0=0 等待
Serial.write(data.b[1]);
delay(1000);
}
或
union uData
{
byte b[2];
int value;
};

union uData data;
void setup() {
Serial.begin(9600);
delay(1000);
}

void loop() {
data.value=0x3456;
Serial.write(0x21);//起始 字元
Serial.write(data.b,2);
delay(1000);
}



接收端

#define Led  12
union uData
{
byte b[2];
int value;
};

union uData data;
 
void setup() {
 Serial.begin(9600);
pinMode(Led,OUTPUT);
digitalWrite(Led,1);
}

void loop() {
  if(Serial.available()>0)
{
  if(Serial.read()==0x21) //判斷是否為起始字元
  {
   //delay(10);//若資料傳遞不正確 可再加長
   // Wait until data is received  (不用delay)
   while ( !(UCSR0A & (1 << RXC0)) )  ;
   data.b[0]=Serial.read();
  
//delay(10);//若資料傳遞不正確 可再加長
  // Wait until data is received  (不 用delay) 
   while ( !(UCSR0A & (1 << RXC0)) )  ;
   data.b[1]=Serial.read();
   if(data.value==0x3456)
    { //Toggle D12
        digitalWrite(Led,1-digitalRead(Led));
      //asm("SBI 0x03,4");
    }
  }
 
  }
 }

或

#define Led  12
union uData
{
byte b[2];
int value;
};

union uData data;
 
void setup() {
 Serial.begin(9600);
pinMode(Led,OUTPUT);
digitalWrite(Led,1);
}

void loop() {
  if(Serial.available()>0)
{
  if(Serial.read()==0x21) //判斷是否為起始字元
  {
 // delay(10);//若資料傳遞不正確 可再加長
  Serial.readBytes(data.b,2);
   if(data.value==0x3456)
    {
     //Toggle D12
        digitalWrite(Led,1-digitalRead(Led));
      //asm("SBI 0x03,4");
    }
  }
 
  }
 }














Arduino序列埠通訊程式Serial.read()讀取到 ÿ 字元(web)

Arduino 傳兩筆資料到 Android (app inventor)手機(web)