Last active
April 12, 2022 01:52
-
-
Save Will-Firgelli/41ec87433f0aaa1abc33e79168076b3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Firgelli Automations | |
* Limited or no support: we do not have the resources for Arduino code support | |
* | |
* Program enables momentary direction control of actuator using push button | |
*/ | |
#include <elapsedMillis.h> | |
elapsedMillis timeElapsed; | |
int RPWM = 10; | |
int LPWM = 11; | |
int sensorPin = A0; | |
int potPin = A1; | |
int potVal; | |
int sensorVal; | |
int Speed = 255; | |
int Buffer = 4; | |
int maxAnalogReading; | |
int minAnalogReading; | |
void setup() { | |
pinMode(RPWM, OUTPUT); | |
pinMode(LPWM, OUTPUT); | |
pinMode(sensorPin, INPUT); | |
pinMode(potPin, INPUT); | |
Serial.begin(9600); | |
maxAnalogReading = moveToLimit(1); | |
minAnalogReading = moveToLimit(-1); | |
} | |
void loop(){ | |
potVal = map(analogRead(potPin), 0, 1023, minAnalogReading, maxAnalogReading); | |
sensorVal = analogRead(sensorPin); | |
if(potVal > (sensorVal+Buffer)){ //addition gives buffer to prevent actuator from rapidly vibrating due to noisy data inputs | |
driveActuator(1, Speed); | |
} | |
else if(potVal < (sensorVal-Buffer)){ | |
driveActuator(-1, Speed); | |
} | |
else{ | |
driveActuator(0, Speed); | |
} | |
Serial.print("Potentiometer Reading: "); | |
Serial.print(potVal); | |
Serial.print("\tActuator reading: "); | |
Serial.println(sensorVal); | |
delay(10); | |
} | |
int moveToLimit(int Direction){ | |
int prevReading=0; | |
int currReading=0; | |
do{ | |
prevReading = currReading; | |
driveActuator(Direction, Speed); | |
timeElapsed = 0; | |
while(timeElapsed < 200){ delay(1);} //keep moving until analog reading remains the same for 200ms | |
currReading = analogRead(sensorPin); | |
}while(prevReading != currReading); | |
return currReading; | |
} | |
void driveActuator(int Direction, int Speed){ | |
switch(Direction){ | |
case 1: //extension | |
analogWrite(RPWM, Speed); | |
analogWrite(LPWM, 0); | |
break; | |
case 0: //stopping | |
analogWrite(RPWM, 0); | |
analogWrite(LPWM, 0); | |
break; | |
case -1: //retraction | |
analogWrite(RPWM, 0); | |
analogWrite(LPWM, Speed); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment