在arduino和for循环中访问数组

时间:2017-04-17 11:54:19

标签: arduino-uno

如何在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 :(得分:0)

在你的代码中几乎没有错误

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

        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在MG上,SSystem在屏幕上的GRID上,那么它必须放在双引号内Serial.println( "SSystem is on MG");Serial.println( "SSystem is on GRID");