Skip to content

Instantly share code, notes, and snippets.

@OMikeGray
Last active June 9, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OMikeGray/4dec9e075a8fe41efaea001fa1e98d70 to your computer and use it in GitHub Desktop.
Save OMikeGray/4dec9e075a8fe41efaea001fa1e98d70 to your computer and use it in GitHub Desktop.
float pos; // Actuator Position
float conNum = 0.00441; // Constant to convert ADC to Inches
// Equal to (951 (ADC at 4") - 44 (ADC at 0")/4")^-1
void setup() {
pinMode(A1, INPUT); // Configure Analog In pin 1 as an Input
pinMode(10, OUTPUT); // Configure pin 10 as an Output
pinMode(11, OUTPUT); // Configure pin 11 as an Output
pinMode(2, INPUT_PULLUP); // Input for Button
pinMode(3, INPUT_PULLUP); // Input for Button
Serial.begin(9600);
}
void loop() {
if(digitalRead(2) == HIGH & digitalRead(3) == LOW){
// Retract Actuator
analogWrite(10, 0);
analogWrite(11, 255);
pos = readPotentiometer(); // Print position value to the Serial Display
Serial.println(pos);
delay(1);
}
else if(digitalRead(2) == LOW & digitalRead(3) == HIGH){
// Extend Actuator
analogWrite(10, 255);
analogWrite(11, 0);
pos = readPotentiometer();
Serial.println(pos); // Print position value to the Serial Display
delay(1);
}
else{
// Stop Actuator
analogWrite(10, 0);
analogWrite(11, 0);
}
}
/*Function to Read Potentiometer and Convert it to Inches*/
float readPotentiometer(void){
float pos;
pos = conNum*(analogRead(A1) - 44); // 44 ADC is equal to 0"
return pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment