增量索引在for循环结束时不会改变

时间:2017-08-03 04:35:42

标签: arduino arduino-uno

我希望将Arduino与PLC接口以提取一些信息。

我的问题出在功能3:设置警报标志/重置标志。此函数用于比较历史值和现值。我试图处理一些整数(test_number)并像二进制16位数据一样处理,以便在某个位找到1。我在Findbit函数中找到了for循环,它应重复16次,无限运行。它不会更改仍然停留在1的增量索引(变量名称bit_1)。

这是我的代码:

int test_number_array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int test_number = 0;
int bit_1 = 0;
int Andbit = 0;
const char* message;
int flagAlarm[2][16] = {};
int flagReset[2][16] = {};


void setup()
{
  // put your setup code here, to run once:
  Serial.begin( 9600 );
}

void loop()
{
  // put your main code here, to run repeatedly:
  for (int j = 1; j <= 2; j++)
  {
    for (int i = 1; i <= 2; i++)   // Example with 2 modbus address
    {
      unsigned int address = 40000 + i;
      Serial.print ("Modbus address = ");
      Serial.println(address, DEC);
      pull_data(i);
      Serial.print("Test number is ");
      Serial.println(test_number);
      Findbit(i);
      Serial.println("------------------------------------------------- ");
    }
  }
  while (1)
  {

  }
}

// ---------------Function 1 : Function finding alarm bit-----------------//

void Findbit(int i)
{
  for (bit_1 = 0; bit_1 <= 15; bit_1++)
  {
    Andbit = test_number & 1;
    Serial.print("Test number (BINARY) is ");
    Serial.println(test_number, BIN);
    Serial.print("Check at bit number ");
    Serial.println(bit_1);
    Serial.print("And bit is ");
    Serial.println(Andbit, BIN);
    Serial.print("flagAlarm(Before1) = ");
    Serial.println(flagAlarm[i][bit_1]);
    Serial.print("flagreset(Before1) = ");
    Serial.println(flagReset[i][bit_1]);

    if (Andbit == 1)                //found "1" pass into loop
    {
       flagAlarm[i][bit_1] = 1;
    }

    else
    { 

    }
    Serial.print("flagAlarm(Before2) = ");
    Serial.println(flagAlarm[i][bit_1]);
    Serial.print("flagreset(Before2) = ");
    Serial.println(flagReset[i][bit_1]);
    Set_reset_flag(i,bit_1);
    test_number = test_number >> 1;
    Serial.print("flagAlarm(After) = ");
    Serial.println(flagAlarm[i][bit_1]);
    Serial.print("flagreset(After) = ");
    Serial.println(flagReset[i][bit_1]);
    Serial.println(" ");
   }
 }

// -----------------------Function 2 : Pull data------------------------- //

int pull_data(int i)
{
  i = i - 1;
  test_number = test_number_array[i];
  return test_number;
}


// -------------Function 3 : Set alarm flag / reset flag ---------------- //

void Set_reset_flag(int i, int bit_1)
{
  Serial.print("i = ");
  Serial.println(i);
  Serial.print("bit_1 = ");
  Serial.println(bit_1);

  if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 0)
  {
    Serial.print("Alarm at bit ");
    Serial.println(bit_1);
    flagAlarm[i][bit_1] = 0;
    flagReset[i][bit_1] = 1;
  }

  else if (flagAlarm[i][bit_1] == 0 && flagReset[i][bit_1] == 1)
  {
    Serial.print("Reset Alarm at bit ");
    Serial.println(bit_1);
    flagReset[i][bit_1] = 0;
  }

  else if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 1)
  {
    Serial.print("Alarm still active at bit ");
    Serial.println(bit_1);
    flagAlarm[i][bit_1] = 0;
    flagReset[i][bit_1] = 1;
  }
  else
  {

  }

}

1 个答案:

答案 0 :(得分:1)

您的bit_1变量是否可以从此处未提及的其他代码中修改,或者根本不进行优化?另外,是否有必要使循环计数器成为全局变量?你能在Findbit函数中声明它吗?