小七2015 发表于 2015-11-22 20:23:40

arduino 怎么读取二维数组【新手求教】


定义了一个二维数组,但是后面输出的temp_Value不变且都为0,求高手帮忙改一下程序



#include <avr/pgmspace.h>
int Digital_Value=0;
float Voltage_Value=0;
float Rt_Value=0;
float temp_Value=0;
float a=0;
float b=0;
const float RP PROGMEM ={-20,80.59,-19,76.32,-18,72.31,-16,64.99,-15,61.65,-14,58.51,-13,55.55,-12,52.76,-11,50.13,-10,47.65,-9,45.31,-8,43.10,-7,39.05,-6,37.19,-5,35.43,-4,41.02,-3,33.76,-2,32.19,-1,30.69,0,29.28,1,27.95,2,26.68,3,25.48,4,24.34,5,23.26,6,22.23,7,21.26,8,20.34,9,10,19.46,11,18.62,12,17.83,13,17.08,14,16.36,15,15.68,16,15.03,17,14.41,18,13.82,19,13.26,20,12.73,21,12.22,22,11.73,23,11.27,24,10.83,25,10.40,26,10.00,27,9.62,28,9.25,29,8.90,30,8.57,31,8.24,32,7.93,33,7.64,34,7.36,35,7.09,36,6.83,37,38,6.59,39,6.35,41,5.90,42, 5.70,43,5.50,44,5.31,45,48,4.62,49,4.46,50,4.31,65,2.49,69,2.21,115,0.66,119,0.60};
void setup(){
Serial.begin(9600);      //波特率设置为9600
}
void loop(){
   Digital_Value =analogRead(2);   //读取电压值(数字量)
   Serial.println(Digital_Value,2);
   Voltage_Value=(3.3* Digital_Value)/1023;
   Serial.println(Voltage_Value,2);
   Rt_Value=10*Voltage_Value/(3.3-Voltage_Value);
   Serial.println(Rt_Value,2);
   
for(int i=0;i<75;i++)
{
a=pgm_read_byte_near(RP);
b=pgm_read_byte_near(RP);
if((a<=Rt_Value)&&(Rt_Value<b))
temp_Value=pgm_read_byte_near(RP);
break;}
Serial.print("temperatureis: ");
Serial.print(temp_Value,2);//发送温度数据
Serial.println("C");
delay(2000);   //一秒刷新一次
}

shihaipeng04 发表于 2015-11-23 00:08:36

pgm_read_byte_near 是个啥函数?

这句 temp_Value=pgm_read_byte_near(RP);   你确定最后的是 而不是 ?

Super169 发表于 2015-11-23 00:31:48

本帖最后由 Super169 于 2015-11-23 00:38 编辑

a=pgm_read_byte_near(RP);
b=pgm_read_byte_near(RP);
if((a<=Rt_Value)&&(Rt_Value<b))
temp_Value=pgm_read_byte_near(RP);
break;}因為 a = b, 除非 a=Rt_Value=b, 否則這個 if condition 永不成立.
而 Rt_Value 為 0, 只有數刑中為 0 時才成立.

而且, 如上面說 RP 設定為 RP, RP 當 i>1 時, 你期望是怎樣, 當成當單的列, 去到第 i 個?

小七2015 发表于 2015-11-23 17:43:56

shihaipeng04 发表于 2015-11-23 00:08 static/image/common/back.gif
pgm_read_byte_near 是个啥函数?

这句 temp_Value=pgm_read_byte_near(RP);   你确定最后的是 而 ...

网上说const定义的数组,读取的时候加 pgm_read_byte_near ,新手并不清楚其中的奥妙:(
每行的第一个数是temp_Value,不是吗

给我镇定剂 发表于 2015-11-23 18:07:22

Super169 发表于 2015-11-23 00:31 static/image/common/back.gif
因為 a = b, 除非 a=Rt_Value=b, 否則這個 if condition 永不成立.
而 Rt_Value 為 0, 只有數刑中為 0 時才 ...

那 if 这段该怎么写呢
是希望它读到每行的第一个数
谢谢

hubertdong 发表于 2015-11-23 21:25:57

可以用一位数组替代二维,j*N+i 的方式。

10423008 发表于 2015-12-4 11:05:40

现在运行正确了吗?
页: [1]
查看完整版本: arduino 怎么读取二维数组【新手求教】