Arduino:在 loop() 和自定义函数中发出 运行 相同的代码

Arduino: issue running the same code in loop() and in custom function

在练习一些 Arduino 代码时,我遇到了一个意想不到的行为,试图 运行 在 loop() 和自定义 myfunction().

中使用相同的代码

经过长时间的反复试验,我准备了这个最小代码来重现问题(使用 Arduino UNO 测试)。 本质上,对于给定字符串的每个字符,代码应该 return char 数组中的相应索引。对于给定的索引,我还通过函数 getCharByIndex.

return 数组值
char* dict[] = {
  "a", "b", "c"
};

char txtorigin[] = "abc";

void setup() {
  Serial.begin(115200);
}

void loop() {

  Serial.println( "" );
  Serial.println( "My function: first call" );
  myfunction(1);
  
  int run_loop = 1;
  if(run_loop == 1){
    Serial.println( "" );
    Serial.println( "Loop code" );
    uint8_t sizeofarray = sizeof(txtorigin) - 1 ;
    char c;
    uint8_t idx;
    for(unsigned int i = 0; i<sizeofarray; i++) {
      c = txtorigin[i];
      idx = getIndexOf( &c );
      Serial.print(c);
      Serial.print(" index: ");
      Serial.print( idx );
      Serial.print(" char: ");
      Serial.print( getCharByIndex( idx ) );
      Serial.println( "" );
    }
  }

  Serial.println( "" );
  Serial.println( "My function: second call" );
  myfunction(1);
  
  delay(1000 * 60);

}

void myfunction(const int something)
{

  uint8_t sizeofarray = sizeof(txtorigin) - 1 ;
  char c;
  int idx;
  for(unsigned int i = 0; i<sizeofarray; i++) {
    c = txtorigin[i];
    idx = getIndexOf( &c );
    Serial.print(c);
    Serial.print(" index: ");
    Serial.print( idx );
    Serial.print(" char: ");
    Serial.print( getCharByIndex( idx ) );
    Serial.println( "" );
  } 
}

int getIndexOf( const char * dict_val )
{
  for ( uint8_t i = 0; i < sizeof( dict ) / sizeof( char * ); i++ )
    if ( !strcmp( dict_val, dict[i] ) )
      return i;
      
  return -1;
}

char* getCharByIndex( int any_idx) 
{
   int idx = any_idx % sizeof(dict);
   return dict[idx];
}

函数 getIndexOf 应该 return 给定字符 * 的 dict[] 索引。

run_loop 为 1 时,我得到以下输出:

My function: first call
a index: -1 char: 
b index: -1 char: 
c index: -1 char: 

Loop code
a index: 0 char: a
b index: 1 char: b
c index: 2 char: c

My function: second call
a index: -1 char: 
b index: -1 char: 
c index: -1 char:

run_loop 为 0 时,我得到以下输出:

My function: first call
a index: 0 char: a
b index: 1 char: b
c index: 2 char: c

My function: second call
a index: 0 char: a
b index: 1 char: b
c index: 2 char: c

老实说,我不明白那里发生了什么。我怀疑函数 getIndexOf 可能是造成问题的原因,但我没有解释。

如有任何帮助,我们将不胜感激。


编辑:每个函数应该做什么? (根据 Gabriel Staples 的评论)

getIndexOf:我需要一个函数,给定一个值(来自 char txtorigin[] = "abc";),returns 是 char* dict[] 的相应索引。 理想情况下:

char c;
int idx;
c = txtorigin[i];
idx = getIndexOf( &c );

getCharByIndex:这个函数应该returndict[]的值对应的索引可以大于dict[]的大小。 理想情况下:

getCharByIndex( 5 ) -> "b"

myfunction:这只是为了检查一切是如何工作的。

Arduino: issue running the same code in loop() and in custom function

关键是代码不是真的一样。

loop()你有

uint8_t idx;

而在 myfunction() 你有

int idx;

由于 int 的大小因平台而异,因此您尝试存储的内容很可能不在该范围内。

见: https://arduino.stackexchange.com/questions/30749/int-vs-uint8-t-vs-uint16-t

Essentialy, for each character of a given string, the code should return the corresponding index in a char array.

const char* dict = "*abcdefghijklmnopqrstuvwxyz"; // ( a -> 1 ... z -> 26 )

int find (char c, const char* dict) {
  const char *p = dict;
  while (*p) { 
    if (*p == c) return p-dict; 
    else p++;
  }
  return -1; 
}

const char* test = "hello";   

void setup() {
   Serial.begin(9600);
   int len = strlen(test);
   for (int i = 0; i < len; i++) {
      int idx = find(test[i], dict);
      if (idx > 0) Serial.println(idx); 
   } 
}

void loop() { }