Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help digitalread limit switch

Status
Not open for further replies.

OldCoalMiner

New Member
Hi guys,
I do ok if I do digitalread to do one thing.But this project does a litte more.The code works good if I release the limit switch but I need it to work while the switch is still pressed. On line 67 the servo stops but then it locks up and does nothing. The switch is hooked up with pull-down resistor .Don't know how to fix it any suggestions? Here is my code.




















Code:
#include <Servo.h>
Servo myservo;
int val = 0;
int val1 = 0;
int topLimitswitch = 2;
int bottomLimitswitch = 3;
int inPir = 4;
int outPir = 5;
int inLed = 6;
int outLed = 7;


boolean goingOut = false;      //boolean if true to disable inPir
boolean goingIn = false;       //boolean if true to disable outPir
void setup(){
  myservo.attach(9);              
pinMode(topLimitswitch,INPUT);
pinMode(bottomLimitswitch,INPUT);
pinMode(inLed,OUTPUT);              
pinMode(outLed,OUTPUT);             
pinMode(inPir,INPUT);               
pinMode(outPir,INPUT);              

}




void loop()
{
  if(digitalRead(inPir)== HIGH && goingOut == false){
goingIn = true;                                    
comein();                                       

  }else if(digitalRead(outPir) == HIGH && goingIn == false){  
goingOut = true;                                            
goout();                                                
  }
}
  void comein(){                                      
myservo.writeMicroseconds(1000);                                   
digitalWrite(inLed,HIGH);                             
  val = 1;                                           
controlin();                                        

}

void goout(){                                        
myservo.writeMicroseconds(1000);                                
digitalWrite(outLed,HIGH);                              
  val1 = 1;                                             
controlout();







}                                                              
      void controlin(){                                          
      while(val == 1)
           if(digitalRead(topLimitswitch) == HIGH){
              
           myservo.writeMicroseconds(1500);                             
            delay(6000);                                   
          myservo.writeMicroseconds(2000);                           
              }else
            if(digitalRead(bottomLimitswitch) == HIGH){          
              myservo.writeMicroseconds(1500);                          
               digitalWrite(inLed,LOW);                      
               val = 0;                                     
               goingIn = false;                                  
               return;                                      
}
  }

    void controlout(){
                while(val1 == 1)
          if(digitalRead(topLimitswitch) == HIGH){
          
          myservo.writeMicroseconds(1500);                                
           delay(6000);                                      
        myservo.writeMicroseconds(2000);
   
        }else if(digitalRead(bottomLimitswitch) == HIGH){                
            myservo.writeMicroseconds(1500);                                    
            digitalWrite(outLed,LOW);                                
            val1 = 0;                                   
            goingOut = false;                                          
            return;                                               
    }
    }
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top