2010-06-18 423 views
2

嘿,我有以下代碼:幫助Arduino的和模擬最小值最大值設置

sVal = analogRead(potPin); // read the value from the sensor 
    valMin = min(sVal, 1); 
    valMax = max(sVal, 128); 
    constrain(sVal,valMin,valMax); 

    itoa(sVal, res, 10); 
    println(res); 
    println(" "); 
    delay(150); 
    clearScreen(); 

現在,出於某種原因,在GLCD屏幕上的輸出幾乎是不斷1023 我想最小值電位器爲1,最大值爲128.

回答

5

您的代碼表示對最小,最大和約束函數缺乏瞭解。我建議你仔細閱讀文檔。

在此期間,這裏是我認爲你後:

sVal = analogRead(potPin); 
sVal = sVal/8 + 1; //scale value [0.. 1023] to [1.. 128] 

itoa(sVal, res, 10); 
println(res); 
println(" "); 
delay(150); 
clearScreen(); 
+0

謝謝,我重讀它;) – 2010-06-21 08:29:26

3

也有一個範圍映射功能已經在API,如:

res = map(analogRead(potPin), 0,1023, 1,128); 
相關問題