/* Code to monitor the current amp draw of the actuator, and to cut power if it rises above a certain amount. Written by Progressive Automations August 19th, 2015 Hardware: - RobotPower MegaMoto control boards - Arduino Uno - 2 pushbuttons */ const int EnablePin = 8; const int PWMPinA = 11; const int PWMPinB = 3; // pins for Megamoto const int buttonLeft = 4; const int buttonRight = 5; const int CPin1 = A5; // motor feedback int leftlatch = LOW; int rightlatch = LOW;//motor latches int hitLimits = 0; int hitLimitsmax = 10;//values to know if travel limits were reached long lastfeedbacktime = 0; int firstfeedbacktimedelay = 750; //first delay to ignore current spike int feedbacktimedelay = 50; //delay between feedback cycles long currentTimefeedback = 0; int debounceTime = 300; //amount to debounce long lastButtonpress = 0; // timer for debouncing long currentTimedebounce = 0; int CRaw = 0; // raw A/D value int maxAmps = 0; // trip limit bool dontExtend = false; bool firstRun = true; bool fullyRetracted = false;//program logic void setup() { Serial.begin(9600); pinMode(EnablePin, OUTPUT); pinMode(PWMPinA, OUTPUT); pinMode(PWMPinB, OUTPUT);//Set motor outputs pinMode(buttonLeft, INPUT); pinMode(buttonRight, INPUT);//buttons digitalWrite(buttonLeft, HIGH); digitalWrite(buttonRight, HIGH);//enable internal pullups pinMode(CPin1, INPUT);//set feedback input currentTimedebounce = millis(); currentTimefeedback = 0;//Set initial times maxAmps = 15;//set max limit }//end setup void loop() { Serial.println("Main Loop"); latchButtons();//check buttons, see if we need to move moveMotor();//check latches, move motor in or out }//end main loop void latchButtons() { if (digitalRead(buttonLeft)==LOW)//left is forwards { currentTimedebounce = millis() - lastButtonpress; if (currentTimedebounce > debounceTime && dontExtend == false)//once you've tripped dontExtend, ignore all forwards presses { leftlatch = !leftlatch; firstRun = true; fullyRetracted = false; // once you move forwards, you are not fully retracted lastButtonpress = millis(); return; }//end if }//end btnLEFT if (digitalRead(buttonRight)==LOW)//right is backwards { currentTimedebounce = millis() - lastButtonpress; if (currentTimedebounce > debounceTime) { rightlatch = !rightlatch; firstRun = true; lastButtonpress = millis(); return; }//end if }//end btnRIGHT }//end latchButtons void moveMotor() { if (leftlatch == HIGH) motorForward(255); //speed = 0-255 if (leftlatch == LOW) motorStop(); if (rightlatch == HIGH) motorBack(255); //speed = 0-255 if (rightlatch == LOW) motorStop(); }//end moveMotor void motorForward(int speeed) { while (dontExtend == false && leftlatch == HIGH) { Serial.println("Moving forwards"); digitalWrite(EnablePin, HIGH); analogWrite(PWMPinA, speeed); analogWrite(PWMPinB, 0);//move motor if (firstRun == true) delay(firstfeedbacktimedelay); else delay(feedbacktimedelay); //small delay to get to speed getFeedback(); firstRun = false; latchButtons(); }//end while }//end motorForward void motorBack (int speeed)//could be simplified { while (rightlatch == HIGH) { Serial.println("Moving Back"); digitalWrite(EnablePin, HIGH); analogWrite(PWMPinA, 0); analogWrite(PWMPinB, speeed);//move motor if (firstRun == true) delay(firstfeedbacktimedelay); else delay(feedbacktimedelay); //small delay to get to speed getFeedback(); firstRun = false; latchButtons(); }//end while dontExtend = false;//allow motor to extend again, after it has been retracted }//end motorBack void motorStop() { analogWrite(PWMPinA, 0); analogWrite(PWMPinB, 0); digitalWrite(EnablePin, LOW); firstRun = true;//once the motor has stopped, reenable firstRun to account for startup current spikes }//end stopMotor void getFeedback() { CRaw = analogRead(CPin1); Serial.println(CRaw); if (CRaw == 0 && hitLimits < hitLimitsmax) hitLimits = hitLimits + 1; else hitLimits = 0; // check to see if the motor is at the limits and the current has stopped if (hitLimits == hitLimitsmax && rightlatch == HIGH) { rightlatch = LOW; fullyRetracted = true; Serial.println("all the way back"); }//end if else if (hitLimits == hitLimitsmax && leftlatch == HIGH) { leftlatch = LOW; hitLimits = 0; Serial.println("all the way front"); }//end if if (CRaw > maxAmps) { dontExtend = true; leftlatch = LOW; //stop if feedback is over maximum }//end if lastfeedbacktime = millis();//store previous time for receiving feedback }//end getFeedback