Go Down

Topic: adjust servo from current position using a pot (Read 936 times) previous topic - next topic

grantastley

#15
Jul 09, 2013, 12:42 pm Last Edit: Jul 09, 2013, 12:45 pm by grantastley Reason: 1
Would something like this work?
Code: [Select]

while(digitalRead(Setup)==HIGH){

   
//********************************************************************
//Servo 1 lower end point
if(digitalRead(WingFoldSetup)==HIGH&&digitalRead(SetTrim1)==HIGH){
 //Pin=LeftLowerTrim;
 //LeftLowerTrim=digitalRead(SetTrim1);
 Pin=digitalRead(SetTrim1);
 Val=LeftLowerTrimVal;
 ValE=LeftLowerTrimValE;
 servo=WingFoldLeft;
}

//Servo1 upper end point
if(digitalRead(WingFoldSetup)==HIGH&&digitalRead(SetTrim2)==HIGH){
 //Pin=LeftUpperTrim;
 //LeftUpperTrim=digitalRead(SetTrim2);
 Pin=digitalRead(SetTrim2);
 Val=LeftUpperTrimVal;
 ValE=LeftUpperTrimValE;
 servo=WingFoldLeft;
}
//Repeat for each servo
//********************************************************************
//Only problem I can see is if 2 dip switches are accidently set to high what would happen?

//Then

//Common setup loop
while(Pin)==High){

//Timer  
if((millis()-Timer)>=TimerVal){   //reset timer once accept is pressed (Timer=milis())
 if(FirstAccept==true){
   FirstLowMap=true;
   FirstUpperMap=true;
   InitialTrimVal=TrimVal; //Setup something to compare values to
   servo.write(Val);  //write servo to current lower trim value stored in EEPROM
   FirstAccept=false;  //reset accept first when accept is pressed.
 }
}  

AcceptVal=digitalRead(Accept);    //Read the digital input 9 - Accept  
TrimVal=analogRead(Trims);  //Read the trim pot value
Val=EEPROM.read(ValE);

//Only allow to happen once.
 if(FirstTime==true){
   InitialTrimVal=TrimVal; //Setup something to compare values to
   servo.write(Val);  //write servo to current val stored in EEPROM
   FirstTime=false;
 }
 
LowerMap=map(TrimVal, 0,InitialTrimVal, 0, Val);   //Map from 0 to the current trim pot value and current servo location (Lower end)
LowerMap=constrain(LowerMap, 0, 90);
UpperMap=map(TrimVal, InitialTrimVal, 1023, Val, 90); //Map from 90 to the curretn trim pot value and current servo location (Upper end)
UpperMap=constrain(UpperMap, 0, 90);

//use the lower map if the initial trim value is larger then the current trim value (pot has been turnd down and input is less)
if (InitialTrimVal>TrimVal&&FirstLowMap==true){  
servo.write(LowerMap);

//need to only happen once
 if(FirstLowMap==true){
   if(AcceptVal==HIGH){
     EEPROM.write(ValE, LowerMap);
     FirstLowMap=false;
     FirstAccept=true;
     Timer=millis();
   }
 }
}

//use the upper map if the initial trim value is less then the current trim value (pot has been turnd up and input is more)
if (InitialTrimVal<TrimVal&&FirstUpperMap==true){  
servo.write(UpperMap);

//need to only happen once
 if(FirstUpperMap==true){
   if(AcceptVal==HIGH){
     EEPROM.write(ValE, UpperMap);
     FirstUpperMap=false;
     FirstAccept=true;
     Timer=millis();
   }
 }
}

Pin;                   //Check to see if setup selections conditions have changed
delay(500); //allow time for servo to move to new location
}
}

Go Up