loading

Help with Capacity and Inductive Proximity Code for Arduino?

I am a beginner. I'm trying to figure out what wrong with this code. The project is suppose to turn a servo motor left and right based on which sensors are active. Capacity Sensor (cpsensor) and Inductive Proximity Sensor (ipsensor). Here is the code:                                                                                                                                                                                     #include<Servo.h>

int ipsensor = 9;
int servo = 7;
int pos = 90;
int val = 0;
int cpsensor = 5;
int led = 4;
Servo myservo;

void setup(){

  pinMode(ipsensor, INPUT);
  pinMode(servo, OUTPUT);
  pinMode(cpsensor, INPUT);
  pinMode(led, OUTPUT);
  myservo.attach(servo);
  digitalWrite(ipsensor, LOW);
  digitalWrite(cpsensor, LOW);
}
void loop()
{
  int ipsensor = 9;
  int cpsensor = 5;
  if ((digitalRead(ipsensor) == HIGH && digitalRead(cpsensor)) == HIGH){
    for(pos =90; pos < 135; pos+=45)
      delay(15);
    for(pos = 135; pos>=90; pos-=45)
    {
      myservo.write(pos);
    }
  }
  else if ((digitalRead(ipsensor) == LOW && digitalRead(cpsensor) == HIGH)){
    for(pos =90; pos < 45; pos-=45)
      delay(15);
    for(pos = 45; pos>=90; pos+=45)
    {
      myservo.write(pos);
    }
  }
  delay(1);
}

 


roniloapin2 years ago

you must use the || instead of && because the LED would only be lightened when the two cases are true if u use &&..

well, what's wrong with it?