Last active
October 26, 2020 06:41
-
-
Save TetsuOtter/c9cf5bc7212a6b3c8a1e444b08b5d130 to your computer and use it in GitHub Desktop.
Arduinoで, 2つのアナログピンに対する入力値平均を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define VR1_PIN A2 //可変抵抗1を接続しているアナログピンのピン記号 | |
#define VR2_PIN A5 //可変抵抗2を接続しているアナログピンのピン記号 | |
#define LOOP_TIME 100 //平均導出のためのサンプリング数 | |
#define VAR_TYPE unsigned long long int //平均導出用に記録を保持する変数の型. ここまでしないとオーバーフローを防げなかった. | |
void setup(){ | |
Serial.begin(9600); //シリアル通信開始 | |
} | |
void loop(){ | |
VAR_TYPE vr1 = 0; //VR1用サンプル保持変数 | |
VAR_TYPE vr2 = 0; //VR2用サンプル保持変数 | |
for(int i = 0; i < LOOP_TIME; i++){ //LOOP_TIMEだけサンプリング | |
vr1 += analogRead(VR1_PIN); //VR1をanalogReadし, 結果を記録変数に加算 | |
vr2 += analogRead(VR2_PIN); //VR2をanalogReadし, 結果を記録変数に加算 | |
} | |
Serial.print("VR1:"); | |
Serial.print((double)vr1/LOOP_TIME); //VR1の出力の平均を出す | |
Serial.print("VR2:"); | |
Serial.println((double)vr2/LOOP_TIME); //VR2の出力の平均を出す | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment