Arduino-data-types

提供:Dev Guides
移動先:案内検索

Arduino-データ型

Cのデータ型は、さまざまな型の変数または関数を宣言するために使用される広範なシステムを指します。 変数のタイプによって、ストレージ内で占めるスペースの量と、格納されているビットパターンの解釈方法が決まります。

次の表は、Arduinoプログラミング中に使用するすべてのデータ型を示しています。

void Boolean char Unsigned char byte int Unsigned int word
long Unsigned long short float double array String-char array String-object

void

voidキーワードは、関数宣言でのみ使用されます。 関数は、呼び出し元の関数に情報を返さないと予想されることを示します。

Void Loop ( ) {
  //rest of the code
}

ブール値

ブール値は、trueまたはfalseの2つの値のいずれかを保持します。 各ブール変数は1バイトのメモリを占有します。

boolean val = false ;//declaration of variable with type boolean and initialize it with false
boolean state = true ;//declaration of variable with type boolean and initialize it with true

Char

文字値を格納するメモリの1バイトを占有するデータ型。 文字リテラルは「A」のように一重引用符で記述され、複数の文字の場合、文字列は二重引用符「ABC」を使用します。

ただし、文字は数字として保存されます。 特定のエンコーディングはhttps://www.arduino.cc/en/Reference/ASCIIchart[ASCIIチャート]で確認できます。 これは、文字の算術演算を実行できることを意味します。この場合、文字のASCII値が使用されます。 たとえば、大文字AのASCII値は65であるため、 'A' + 1の値は66です。

Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with character a
Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97

ASCII文字テーブル

符号なし文字

*Unsigned char* は、1バイトのメモリを占有する符号なしデータ型です。 unsigned charデータ型は、0〜255の数値をエンコードします。

Unsigned Char chr_y = 121 ;//declaration of variable with type Unsigned char and initialize it with character y

byte

バイトには、0〜255の8ビット符号なし数値が格納されます。

byte m = 25 ;//declaration of variable with type byte and initialize it with 25

int

整数は、数値ストレージの主要なデータ型です。 intは16ビット(2バイト)値を格納します。 これにより、-32,768〜32,767の範囲が得られます(最小値は-2 ^ 15、最大値は(2 ^ 15)-1)。

*int* サイズはボードごとに異なります。 たとえば、Arduino Dueでは、 *int* に32ビット(4バイト)値が格納されます。 これにより、-2,147,483,648から2,147,483,647の範囲が得られます(最小値は-2 ^ 31、最大値は(2 ^ 31)-1)。

int counter = 32 ;//declaration of variable with type int and initialize it with 32

符号なし整数

符号なし整数(符号なし整数)は、2バイト値を格納する方法がintと同じです。 ただし、負の数を格納する代わりに、正の値のみを格納するため、有効な範囲は0〜65,535(2 ^ 16)-1)になります。 Dueは、0〜4,294,967,295(2 ^ 32-1)の範囲の4バイト(32ビット)値を格納します。

Unsigned int counter = 60 ;//declaration of variable with
   type unsigned int and initialize it with 60

Word

Unoおよび他のATMEGAベースのボードでは、1ワードに16ビットの符号なし数値が格納されます。 Due and Zeroでは、32ビットの符号なし数値を格納します。

word w = 1000 ;//declaration of variable with type word and initialize it with 1000

Long

ロング変数は、数値ストレージ用の拡張サイズ変数で、-2,147,483,648から2,147,483,647までの32ビット(4バイト)を格納します。

Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346

符号なしロング

符号なしlong変数は、数値ストレージ用の拡張サイズ変数であり、32ビット(4バイト)を格納します。 標準のlongとは異なり、unsigned longは負の数値を格納せず、その範囲は0〜4,294,967,295(2 ^ 32-1)になります。

Unsigned Long velocity = 101006 ;//declaration of variable with
   type Unsigned Long and initialize it with 101006

ショート

ショートは16ビットのデータ型です。 すべてのArduino(ATMegaおよびARMベース)では、shortは16ビット(2バイト)値を格納します。 これにより、-32,768〜32,767の範囲が得られます(最小値は-2 ^ 15、最大値は(2 ^ 15)-1)。

short val = 13 ;//declaration of variable with type short and initialize it with 13

浮く

浮動小数点数のデータ型は、小数点を持つ数値です。 浮動小数点数は、整数よりも解像度が高いため、アナログ値と連続値を近似するためによく使用されます。

浮動小数点数は最大3.4028235E + 38から最小-3.4028235E + 38まで可能です。 これらは32ビット(4バイト)の情報として保存されます。

float num = 1.352;//declaration of variable with type float and initialize it with 1.352

ダブル

Unoおよび他のATMEGAベースのボードでは、倍精度浮動小数点数は4バイトを占有します。 つまり、double実装はfloatとまったく同じであり、精度は向上しません。 Arduino Dueでは、doubleの精度は8バイト(64ビット)です。

double num = 45.352 ;//declaration of variable with type double and initialize it with 45.352