在arduino和for循环中访问数组

Array accessing in arduino and for loop

如何在 Arduino 编程中访问数组中的数据值?

程序如下:

 int myArraylt[24]=    {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
 int myArraygt[24]=    {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};

 void setup() {

 for (int i=1;i=1;i++)
   if (myArraygt(i)>myArraylt(i))
   println( SSystem is on MG);
   else
   println( SSystem is on GRID); 
 }

 void loop() {
  // put your main code here, to run repeatedly:

 }

你的代码中几乎没有错误

  1. 数组索引从 0 开始,但在你的 for 循环中你从 1
  2. 开始
  3. 在循环条件表达式中检查为 i=1 它只对数组索引 1
  4. 运行循环
  5. 为了从数组中访问数据值,使用 array[i] 格式(不是数组(i))
  6. 在 arduino 中使用 Serial.println() 进行打印,而不仅仅是 println()

要访问数组值,代码应如下所示

    int myArraylt[24]=      {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
     int myArraygt[24]=    {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};

     void setup() {

     for (int i=0;i<24;i++)
       if (myArraygt[i]>myArraylt[i])
           Serial.println( SSystem is on MG); 
       else
           Serial.println( SSystem is on GRID);
     }

     void loop() {
     // put your main code here, to run repeatedly:
     }

如果你想在屏幕上打印 SSystem is on MG and SSystem is on GRID 那么它必须放在双引号内,如 Serial.println( "SSystem is on MG");Serial.println( "SSystem is on GRID");