极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20367|回复: 6

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

[复制链接]
发表于 2015-11-22 20:23:40 | 显示全部楼层 |阅读模式

定义了一个二维数组,但是后面输出的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[70][2] 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[0][i]);
  b=pgm_read_byte_near(RP[0][i]);
  if((a<=Rt_Value)&&(Rt_Value<b))
  temp_Value=pgm_read_byte_near(RP[i][0]);
break;}
  Serial.print("temperature  is: ");
  Serial.print(temp_Value,2);  //发送温度数据
  Serial.println("  C");
delay(2000);   //一秒刷新一次
}

回复

使用道具 举报

发表于 2015-11-23 00:08:36 | 显示全部楼层
pgm_read_byte_near 是个啥函数?

这句 temp_Value=pgm_read_byte_near(RP[i][0]);   你确定最后的是 [i][0] 而不是 [0][i]?
回复 支持 反对

使用道具 举报

发表于 2015-11-23 00:31:48 | 显示全部楼层
本帖最后由 Super169 于 2015-11-23 00:38 编辑
  1.   a=pgm_read_byte_near(RP[0][i]);
  2.   b=pgm_read_byte_near(RP[0][i]);
  3.   if((a<=Rt_Value)&&(Rt_Value<b))
  4.   temp_Value=pgm_read_byte_near(RP[i][0]);
  5.   break;}
复制代码
因為 a = b, 除非 a=Rt_Value=b, 否則這個 if condition 永不成立.
而 Rt_Value 為 0, 只有數刑中為 0 時才成立.

而且, 如上面說 RP 設定為 RP[72][0], RP[0] 當 i>1 時, 你期望是怎樣, 當成當單的列, 去到第 i 個?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-11-23 17:43:56 | 显示全部楼层
shihaipeng04 发表于 2015-11-23 00:08
pgm_read_byte_near 是个啥函数?

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

网上说const定义的数组,读取的时候加 pgm_read_byte_near ,新手并不清楚其中的奥妙
每行的第一个数是temp_Value,不是[0]吗
回复 支持 反对

使用道具 举报

发表于 2015-11-23 18:07:22 | 显示全部楼层
Super169 发表于 2015-11-23 00:31
因為 a = b, 除非 a=Rt_Value=b, 否則這個 if condition 永不成立.
而 Rt_Value 為 0, 只有數刑中為 0 時才 ...

那 if 这段该怎么写呢
是希望它读到每行的第一个数
谢谢
回复 支持 反对

使用道具 举报

发表于 2015-11-23 21:25:57 | 显示全部楼层
可以用一位数组替代二维,j*N+i 的方式。
回复 支持 反对

使用道具 举报

发表于 2015-12-4 11:05:40 | 显示全部楼层
现在运行正确了吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 12:49 , Processed in 0.040207 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表