arrays - Arduino Sketch - 头文件中的对象不包含任何值

标签 arrays arduino header-files

我在 Codes.h 中有以下内容,我可以通过单击 Arduino 软件中的选项卡来访问它。所以我知道草图正在使用头文件正确加载。

#ifndef __CODES_H__
#define __CODES_H__

PROGMEM prog_uint16_t show_hide_info[] = { 4216, 8900, 4380, 580, 500, 600, 500, 580, 1620, 580, 500, 600, 500, 580, 500, 600, 480, 600, 500, 580, 1620, 580, 1620, 600, 500, 580, 1620, 580, 1620, 600, 1600, 600, 1620, 580, 1620, 600, 500, 580, 1620, 580, 500, 600, 1600, 600, 500, 580, 1620, 580, 500, 600, 1620, 580, 1620, 600, 480, 600, 1620, 580, 500, 600, 1600, 600, 500, 580, 1620, 580, 500, 600, 39300, 8860, 2160, 580 };

#endif

然后,我的代码中有一个使用 show_hide_info[] 数组的方法。

问题是,当我尝试访问头文件中的数组时,它不包含任何值。

相反,如果我在 setup() 方法上方声明上述 PROGMEM,它确实包含值。

不太清楚为什么会遇到这个问题。我可以通过在草图中执行以下操作来确认我已正确声明我的头文件。

#include“Codes.h”

这是我如何使用数组的示例:

void sendCode(prog_uint16_t inArray[], int nLimit) {
  unsigned int arr[nLimit];
  unsigned int c;
  int index = 0;

  while ((c = pgm_read_word(inArray++))) {
    arr[index] = c;
    index++;
  }

  for (int i = 0; i < nLimit; i=i+2) {
    Serial.println(arr[i]);
    Serial.println(arr[i+1]);
  }
}

如果我尝试使用头文件中的数组,控制台中不会输出任何内容。仅当我在实际的主草图程序中声明它时它才有效。

我知道这不是数组本身的问题,也不是我的方法的问题,因为简单的字符串或 int 也不起作用。很奇怪。

最佳答案

这些内容对我有用:

在草图的顶部,只需:

#include "Codes.h"

Codes.h 需要如下所示才能工作:

#ifndef __CODES_H__
#define __CODES_H__

// Implicitly includes <avr/pgmspace.h> to provide access to progmem features
#include <Arduino.h>

// Split up declaration and definition to remove warning
extern const prog_uint16_t show_hide_info[] PROGMEM;
const prog_uint16_t show_hide_info[] = { 
  4216, 8900, 
  4380, 580, 
  500, 600, 
  500, 580, 
  1620, 580, 
  /* and so on... */
  0 // Need this to prevent sendCode reading beyond the end of this array
};

#endif

关于arrays - Arduino Sketch - 头文件中的对象不包含任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952046/

相关文章:

python - 根据值将数组拆分为多个新数组

javascript - 比较两个数组并插入空值

c# - 如何在变量声明 (CS0270) 中强制执行数组的长度?

python - 在 Python 中解压包含 bool 值的结构

arrays - 带有 JSON 的 Mule 集合拆分器

c - Arduino 中断替代品

Arduino Ethernet shield v1.1 ENC28J60 MAC地址

c++ - 如何继承一个已经提前声明的类

c++ - 对C++头文件的基本了解

c++ - 为什么有头文件和 .cpp 文件?