退出状态 1 预期“if”之前的主要表达式

问题描述 投票:0回答:1

我一直在做一个Arduino项目,在验证中遇到了这个问题:退出状态1 'if' 之前的预期主要表达式

我在网上搜索了解决方案,但唯一的解决方案是确保正确定义事物,但据我所知,它们很好......

我的代码是:

//Code for Machine

int sensorPin1 = A0; //select pin input for LDR1//
int sensorValue1 = 0; //variable to store value from LDR1//
int sensorPin2 = A1; //pin for LDR2//
int sensorValue2 = 0; //variable to store value from LDR2//
int pistonNum = 0; //variable to determin which piston to activate//

#define piston1Out 1 // is defined as pin #1//
#define piston1In 2 // is defined as pin #2//
#define piston2Out 3 // is defined as pin #3//
#define piston2In 4 // is defined as pin #4//
#define relay1 5 //is defined as pin #5//
#define relay2 6 //is defined as pin #6//

void setup() { //Setup runs once//

pinMode(piston1Out, OUTPUT); //Set  as an output//
pinMode(piston1In, OUTPUT); //Set  as an output//
pinMode(piston2Out, OUTPUT); //Set  as an output//
pinMode(piston2In, OUTPUT); //Set  as an output//
pinMode(relay1, OUTPUT); //set as an output//
pinMode(relay2, OUTPUT); //set as an output//
}

void loop() {

  //read value from sensor:
  sensorValue1 = analogRead(sensorPin1);  
  sensorValue2 = analogRead(sensorPin2); 

if((sensorValue1 < 820) && if(sensorValue2 > 820)); { //If first sensor is below threshold and second is above then activate first piston//
    digitalWrite(piston1In,HIGH);
digitalWrite(piston1Out,LOW); //Pistons Out
digitalWrite(relay1,HIGH); //turns the relay for the first piston on//

delay(100);

digitalWrite(relay1,LOW); //turns the relay for the first piston off//
digitalWrite(piston1Out,HIGH);
digitalWrite(piston1In,LOW); //piston IN//
digitalWrite(relay1,HIGH); //relay on - activate piston//

delay(100);

digitalWrite(relay1, LOW); //piston off//
   }
  else if((sensorValue1 < 820) && if(sensorValue2 < 820)) { //If both sensors are below threshold then activate the second piston//
   digitalWrite(piston1In,HIGH);
digitalWrite(piston1Out,LOW); //Pistons Out
digitalWrite(relay1,HIGH); //turns the relay for the first piston on//

delay(100);

digitalWrite(relay1,LOW); //turns the relay for the first piston off//
digitalWrite(piston1Out,HIGH);
digitalWrite(piston1In,LOW); //piston IN//
digitalWrite(relay1,HIGH); //relay on - activate piston//

delay(100);

digitalWrite(relay1On, LOW); //piston off//
  }  
  else if((sensorValue1 > 820) && if(sensorValue2 > 820)) { //If both are above threshold then activate no pistons//
    pistonNum = 1
   }
else {
  pistonNum = 0
}


delay(50);
}

帮忙?

arduino
1个回答
0
投票

你的if写错了。你写的

if((sensorValue1 < 820) && if(sensorValue2 > 
820));

这有两个问题。首先,通过简单地删除它来更正第二个 if 语句。其次,删除分号。它可以编译,但会导致 if 语句中的所有代码都无法运行。 这是更正后的行:

if((sensorValue1 < 820) && (sensorValue2 > 
820))

很高兴我能帮忙!

© www.soinside.com 2019 - 2024. All rights reserved.