目的
安価に温度を測る。
v = -8.2 * t + 1705
式の変形
t = (v - 1705.0) / -8.2;
// 207.92 = (0 - 1705.0) / -8.2;
// -401.82 = (5000 - 1705.0) / -8.2;
int t = map(s,0,1024,2079,-4018);
プログラム
//SER_S-8120C_map_UNO_1
#include <Arduino.h>
//初期化処理
void setup()
{
//シリアルの初期化
Serial.begin(9600);
} //setup
//メインループ
void loop()
{
//センサーの値を読み込む
int s = analogRead(A0); // センサーの読み取り値
//センサーのADC値を温度に変換 温度の10倍を出力 ex 20.1 => 201
// v = -8.2 * t + 1705 => t = (v - 1705.0) / -8.2;
// 207.92 = (0 - 1705.0) / -8.2;
// -401.82 = (5000 - 1705.0) / -8.2;
int t = map(s,0,1024,2079,-4018);
//温度の表示
Serial.print(t / 10);
Serial.print('.');
Serial.print(t % 10);
Serial.println();
//1秒の待ち
delay(1000);
} //loop
テストプログラム
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#include <stdio.h>
int main(void){
int s;
float v;
float t;
for(int ii=0;ii<=1024;ii++){
printf("[%d]= ",ii);
s = ii;
v = s * (5000.0 / 1024);
printf("V[%f]= ",v);
t = (v - 1705.0) / -8.2;
printf("T[%f]= ", t );
printf("T[%d]= ", map(ii,0,1024,2079,-4018) );
printf("\r\n");
}
}