/* Sample code to control the position of an actuator with potentiometer feedback using a MegaMoto. The main loop of this program checks the potentiometer, and moves the actuator accordingly. Written by Progressive Automations This example code is in the public domain. */ const int feedback = A0; //potentiometer from actuator const int pot = A1; //pot from throttle const int enable = 8; const int PWMA = 11; const int PWMB = 3; int actMax = 760; int actMin = 250;//positions of actuator int potMin = 0; int potMax = 1023; int precision = 2;//how close to final value to get int checkingInterval = 50;//how often position is checked (milliseconds) int rawcurrentPosition = 0; int currentPosition = 0; int rawdestination = 0; int destination = 0; int difference = 0;//values for knowing location void setup() { pinMode(feedback, INPUT);//feedback from actuator pinMode(pot, INPUT);//feedback from potentiometer pinMode(enable, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(PWMB, OUTPUT);//three pins for MegaMoto digitalWrite(enable,HIGH); Serial.begin(9600); } void loop() { destination = getDestination(); currentPosition = analogRead(feedback);//check where you are Serial.print("Position "); Serial.println(analogRead(feedback)); difference = destination - currentPosition;//find out how far you are from the destination if (currentPosition > destination) pullActuatorUntilStop(destination);// choose what action to take else if (currentPosition < destination) pushActuatorUntilStop(destination); else if (difference < precision && difference > -precision) stopActuator(); }//end void loop int getDestination() { rawdestination = analogRead(pot);//read the potentiometer to get the destination destination = map(rawdestination, potMin,potMax,actMin,actMax);//convert the potentiometer feedback to match the actuator return(destination); }//end getDestination void pushActuatorUntilStop(int destination) { destination = getDestination(); int temp = analogRead(feedback); difference = destination - temp;//check difference to see if continue moving, or stop while (difference > precision || difference < -precision) { destination = getDestination(); temp = analogRead(feedback); //continue checking difference difference = destination - temp; pushActuator(); }//end while delay(25); stopActuator(); }//end pushActuatorUntilStop void pullActuatorUntilStop(int destination) { destination = getDestination(); int temp = analogRead(feedback); //check difference to see if continue moving, or stop difference = destination - temp; while (difference > precision || difference < -precision) { destination = getDestination(); temp = analogRead(feedback); //continue checking difference difference = destination - temp; pullActuator(); }//end while delay(25); stopActuator(); }//end pullActuatorUntilStop void stopActuator() { analogWrite(PWMA,0); analogWrite(PWMB,0); }//end stopActuator void pushActuator() { analogWrite(PWMB,255); analogWrite(PWMA,0); }//end pushActuator void pullActuator() { analogWrite(PWMB,0); analogWrite(PWMA,255); }//end pullActuator