日本と海外

日本と海外の国の比較と名古屋愛知情報

arduino アナログピンの値

アナログピンの値をモニターに表示するスケッチで、変数化している。

Unoであればアナログピンすべて(A0~A5)を網羅している。

また、なるべく短くした。

変数が表すものはスケッチで//以降で説明した。

変数の値は可変であるが、通信速度は変えると表示に影響するので、詳しくない人は変えないほうがいい。

 

//----------------------------------------
const int analogInPin[] = {A0, A1, A2, A3, A4, A5}; // アナログピンの番号
int sensorValue;//センサーの読取値
int outputValue;//センサーの読取値を変換値
int tusinsokudo = 9600; //モニターとの通信速度
int saitei = 0; //変換値の最低
int saikou = 100; //変換値の最高
int mati = 2; //止めるマイクロ秒
//-----------------------------------------------------
void setup() {// setup **************************************************
  // initialize serial communications at 9600 bps:
  Serial.begin(tusinsokudo);
}
void loop() {// loop **************************************************
  for (int i = 0; i <= 5; i++) {
    sensorValue = analogRead(analogInPin[i]);
    outputValue = map(sensorValue, 0, 1023, saitei, saikou);
    Serial.print(i);
    Serial.print(" sensor = ");
    Serial.print(sensorValue);
    Serial.print("\t output = ");
    Serial.println(outputValue);
    delay(mati);
  }
}

//----------------------------------------

for文と配列変数を使うとプログラムが短くできる。

 

動かない場合や誤動作は管理人まで詳細を知らせてください。