Skip to content

Instantly share code, notes, and snippets.

@TakesTheBiscuit
Last active December 3, 2022 09:18
Show Gist options
  • Save TakesTheBiscuit/8da30ae5d1358c86681d54270f095b9f to your computer and use it in GitHub Desktop.
Save TakesTheBiscuit/8da30ae5d1358c86681d54270f095b9f to your computer and use it in GitHub Desktop.
Continuous servo until limit switch arduino
#include <Servo.h>
#define TURN_TIME 1200
Servo toolheadServo;
int toolheadHome = 0;
boolean toolheadGoingHome = false;
int discContact = 0;
boolean toolheadGoingAway = false;
char receivedChar;
boolean newData = false;
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin 2 and 3 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(13, OUTPUT); //LED for switch debug
// PIN 9 is vertical toolhead movement servo
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
//read the pushbutton value into a variable
toolheadHome = digitalRead(2); //
discContact = digitalRead(3); // tbc!!
//print out the value of the pushbutton
//Serial.println(toolheadHome);
digitalWrite(13, discContact);
if (toolheadHome && toolheadGoingHome) {
toolheadServo.detach();
Serial.print("Welcome home, toolhead.");
toolheadGoingHome = false;
}
if (discContact && toolheadGoingAway) {
toolheadServo.detach();
Serial.print("Toolhead arrived at destination DVD");
toolheadGoingAway = false;
}
recvOneChar();
showNewData();
}
void toolheadGoHome() {
// will default to HIGH with pullup (i.e if switch dies then motor should cut out safety)
if (toolheadHome == 1) {
// good.
// stop!
toolheadServo.detach();
delay(120);
toolheadGoingHome = false;
Serial.print("Welcome home, toolhead.");
} else{
// send it home.
toolheadServo.attach(9);
delay(120);
toolheadServo.write(0);
toolheadGoingHome = true;
Serial.print("Toolhead going home.");
}
}
void toolheadGoExtend() {
// will default to HIGH with pullup (i.e if switch dies then motor should cut out safety)
if (discContact == 0) {
// drive out until disk contact is made
toolheadServo.attach(9);
delay(120);
toolheadServo.write(180);
toolheadGoingAway = true;
Serial.print("Toolhead going away, goodbye");
} else {
// stop when we hit a disc
toolheadServo.detach();
delay(120);
toolheadGoingAway = false;
Serial.print("Welcome to the DVD surface, toolhead.");
}
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
Serial.println("---");
if (receivedChar == 'A') {
Serial.println("toolheadGoHome");
toolheadGoHome();
}
if (receivedChar == 'B') {
Serial.println("toolheadGoExtend");
toolheadGoExtend();
}
newData = false;
}
}
@TakesTheBiscuit
Copy link
Author

Send A or B over serial to Arduino; it will run the continuous servo (modified from a futaba standard servo) until;

If going home:
toolhead home - switch pin 2 is hot/high

if going to disc:
toolhead disc - switch pin 3 goes high

reports back status over serial
fairly basic, no error checking or timers to verify operations aren't taking too long; thinking rapsberry pi could handle that ?

@djpronk
Copy link

djpronk commented Dec 3, 2022

Thanks Paul, this is really good! I have used your code to adapt and rewrite it for use in a Quadcopter. In my situation a landing gear, using a 360 degrees servo and two limit switches, is controlled by an Arduino Pro Mini. It works very well. Best rgds, Dirk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment