Skip to content

Instantly share code, notes, and snippets.

@chrisconlon-klaviyo
Created August 11, 2019 22:43
Show Gist options
  • Save chrisconlon-klaviyo/f27d3ea2be58bf89e518da5c20fa8d39 to your computer and use it in GitHub Desktop.
Save chrisconlon-klaviyo/f27d3ea2be58bf89e518da5c20fa8d39 to your computer and use it in GitHub Desktop.
A brief, novice Arduino hardware sketch to
#include <Stepper.h>
const int steps_per_revolution = 200;
const int step_pin = 3;
const int direction_pin = 4;
const int end_stop_pin = 5;
const int steps_per_motion = steps_per_revolution / 2;
const int motor_speed = 50;
const int led_pin = 13;
String current_command = "";
bool is_ready = true;
bool next_command_ready = false;
int end_stop_state = 0;
unsigned long previousMillis = 0;
long interval = 50;
Stepper stepController(steps_per_revolution, step_pin, direction_pin);
void setup() {
Serial.begin(9600);
Serial.setTimeout(50);
pinMode(led_pin, OUTPUT);
pinMode(end_stop_pin, INPUT);
stepController.setSpeed(motor_speed);
}
void loop() {
unsigned long current_millis = millis();
if (next_command_ready) {
if (current_millis - previous_millis > interval) {
current_command.trim();
if (current_command.startsWith("STOPPED_TOUCHING_CUP")) {
removeFingerFromCup();
}
if (current_command.startsWith("BEGIN_TOUCHING_CUP")) {
putFingerOnCup();
}
current_command = "";
next_command_ready = false;
}
}
}
void putFingerOnCup() {
digitalWrite(led_pin, HIGH);
stepController.step(-steps_per_motion);
}
void removeFingerFromCup() {
digitalWrite(led_pin, LOW);
stepController.step(steps_per_motion);
}
void serialEvent() {
while (Serial.available()) {
char chunk = (char)Serial.read();
current_command += chunk;
if (chunk== '\n') {
next_command_ready = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment