Arduino代码:输入末尾应为'}'

时间:2019-02-24 16:51:05

标签: c++ arduino

当我使用Arduino进行编码时,我的代码的最后几行出现了问题。该程序说是

  

'}'令牌之前的预期声明

但我不确定如何解决此问题。该代码在下面,并且错误在代码的最后一行。我不确定为什么有问题的最后一个括号不在“框”或代码中,但问题仍然存在。

void loop(){

 if(digitalRead(pirPin) == HIGH){
   digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
   if(lockLow){  
     //makes sure we wait for a transition to LOW before any further output is made:
     lockLow = false;            
     Serial.println("---");
     Serial.print("motion detected at ");
     Serial.print(millis()/1000);
     Serial.println(" sec"); 
     delay(50);
     }         
     takeLowTime = true;
   }

 if(digitalRead(pirPin) == LOW){       
   digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

   if(takeLowTime){
    lowIn = millis();          //save the time of the transition from high to LOW
    takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    }
   //if the sensor is low for more than the given pause, 
   //we assume that no more motion is going to happen
   if(!lockLow && millis() - lowIn > pause){  
       //makes sure this block of code is only executed again after 
       //a new motion sequence has been detected
       lockLow = true;  

            for (int pos1 = 0; pos1 <= 89; pos1 += 1)
          {
            servo1.write(pos1);
            delay(10);
          }
          for (int pos1 = 89; pos1 >= 1; pos1 -= 1)
          {
            servo1.write(pos1);
            delay(10);            
          }
       Serial.print("motion ended at ");      //output
       Serial.print((millis() - pause)/1000);
       Serial.println(" sec");
       delay(50);








       {

 if(digitalRead(pirPin) == HIGH){
   digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
   if(lockLow){  
     //makes sure we wait for a transition to LOW before any further output is made:
     lockLow = false;            
     Serial.println("---");
     Serial.print("motion detected at ");
     Serial.print(millis()/1000);
     Serial.println(" sec"); 
     delay(50);
     }         
     takeLowTime = true;
   }

 if(digitalRead(pirPin) == LOW){       
   digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

   if(takeLowTime){
    lowIn = millis();          //save the time of the transition from high to LOW
    takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    }
   //if the sensor is low for more than the given pause, 
   //we assume that no more motion is going to happen
   if(!lockLow && millis() - lowIn > pause){  
       //makes sure this block of code is only executed again after 
       //a new motion sequence has been detected
       lockLow = true;  

            for (int pos1 = 0; pos1 <= 89; pos1 += 1)
          {
            servo1.write(pos1);
            delay(10);
          }
          for (int pos1 = 89; pos1 >= 1; pos1 -= 1)
          {
            servo1.write(pos1);
            delay(10);            
          }
       Serial.print("motion ended at ");      //output
       Serial.print((millis() - pause)/1000);
       Serial.println(" sec");
       delay(50);



       }
   }
}       //THE ISSUE IS HERE

2 个答案:

答案 0 :(得分:0)

我通过UNIX indent工具运行了您的代码。它适用于大多数平台。 缩进不仅使外观漂亮,还使代码保持可读性。另外,还有许多程序员编辑器会找到“匹配的”括号/括号(例如:vim中的'%')。

在我看来,代码似乎缺少if条件或类似if ( digitalRead( pirPin ) == HIGH )之类的条件,因为在{中有一个块-或至少有一段没有明确的原因。

基本上,您的代码缺少3个},其中之一正在关闭上述代码块。仔细阅读后,应该不难修复。但是如果失败,只需将它们添加到最后即可。

void loop(  )
{
    if ( digitalRead( pirPin ) == HIGH )
    {
        digitalWrite( ledPin, HIGH );   //the led visualizes the sensors output pin state
        if ( lockLow )
        {
            //makes sure we wait for a transition to LOW before any further output is made:
            lockLow = false;
            Serial.println( "---" );
            Serial.print( "motion detected at " );
            Serial.print( millis(  ) / 1000 );
            Serial.println( " sec" );
            delay( 50 );
        }
        takeLowTime = true;
    }

    if ( digitalRead( pirPin ) == LOW )
    {
        digitalWrite( ledPin, LOW );    //the led visualizes the sensors output pin state

        if ( takeLowTime )
        {
            lowIn = millis(  ); //save the time of the transition from high to LOW
            takeLowTime = false;        //make sure this is only done at the start of a LOW phase
        }
        //if the sensor is low for more than the given pause, 
        //we assume that no more motion is going to happen
        if ( !lockLow && millis(  ) - lowIn > pause )
        {
            //makes sure this block of code is only executed again after 
            //a new motion sequence has been detected
            lockLow = true;

            for ( int pos1 = 0; pos1 <= 89; pos1 += 1 )
            {
                servo1.write( pos1 );
                delay( 10 );
            }
            for ( int pos1 = 89; pos1 >= 1; pos1 -= 1 )
            {
                servo1.write( pos1 );
                delay( 10 );
            }
            Serial.print( "motion ended at " ); //output
            Serial.print( ( millis(  ) - pause ) / 1000 );
            Serial.println( " sec" );
            delay( 50 );

            {

                if ( digitalRead( pirPin ) == HIGH )
                {
                    digitalWrite( ledPin, HIGH );       //the led visualizes the sensors output pin state
                    if ( lockLow )
                    {
                        //makes sure we wait for a transition to LOW before any further output is made:
                        lockLow = false;
                        Serial.println( "---" );
                        Serial.print( "motion detected at " );
                        Serial.print( millis(  ) / 1000 );
                        Serial.println( " sec" );
                        delay( 50 );
                    }
                    takeLowTime = true;
                }

                if ( digitalRead( pirPin ) == LOW )
                {
                    digitalWrite( ledPin, LOW );        //the led visualizes the sensors output pin state

                    if ( takeLowTime )
                    {
                        lowIn = millis(  );     //save the time of the transition from high to LOW
                        takeLowTime = false;    //make sure this is only done at the start of a LOW phase
                    }
                    //if the sensor is low for more than the given pause, 
                    //we assume that no more motion is going to happen
                    if ( !lockLow && millis(  ) - lowIn > pause )
                    {
                        //makes sure this block of code is only executed again after 
                        //a new motion sequence has been detected
                        lockLow = true;

                        for ( int pos1 = 0; pos1 <= 89; pos1 += 1 )
                        {
                            servo1.write( pos1 );
                            delay( 10 );
                        }
                        for ( int pos1 = 89; pos1 >= 1; pos1 -= 1 )
                        {
                            servo1.write( pos1 );
                            delay( 10 );
                        }
                        Serial.print( "motion ended at " );     //output
                        Serial.print( ( millis(  ) - pause ) / 1000 );
                        Serial.println( " sec" );
                        delay( 50 );
                    }
                }
            }                   //THE ISSUE IS HERE

答案 1 :(得分:-1)

online tool可以帮助您解决。我可以看到至少缺少两个右括号。

人们在这里建议的缩进代码也是一个很好的建议。

另一种方法可能是将代码重构为更小的函数,从内部到外部。