Skip to content

Instantly share code, notes, and snippets.

@unixunion
Last active July 11, 2021 15:48
Show Gist options
  • Save unixunion/d4589751ec22249f8a2edc0b2c51da69 to your computer and use it in GitHub Desktop.
Save unixunion/d4589751ec22249f8a2edc0b2c51da69 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <EEPROM.h>
#include <Sparkfun_TB6612.h>
// motor driver pins
#define PWMA 9
#define AIN2 8
#define AIN1 7
#define STBY 6
#define BIN1 5
#define BIN2 4
#define PWMB 3
// endstops and buttons
#define BUTTON 10
#define XE1 11
#define XE2 12
unsigned long endstopTriggerTime = 0;
unsigned long endstopDebounceTime = 1000;
unsigned long buttonHoldActionTime = 400;
// button
unsigned long buttonTriggerTime = 0;
unsigned long buttonDebounceTime = 50;
// led
const int LED = 13;
// states
signed int coilerDirection = 1;
signed int winderDirection = 1;
signed int speed = 128;
// states continued
bool stopped = true;
int buttonPresses = 1;
bool ledState = false;
// eprom address
int addr = 0;
// driver
Motor winder = Motor(AIN1, AIN2, PWMA, 1, STBY);
Motor coiler = Motor(BIN1, BIN2, PWMB, 1, STBY);
void setup() {
pinMode(STBY, OUTPUT);
// endstops
pinMode(XE1, INPUT);
digitalWrite(XE1, HIGH);
pinMode(XE2, INPUT);
digitalWrite(XE2, HIGH);
// buttons
pinMode(BUTTON, INPUT);
digitalWrite(BUTTON, HIGH);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
coilerDirection = EEPROM.read(addr);
winderDirection = EEPROM.read(addr+1);
speed = EEPROM.read(addr+2);
if (coilerDirection > 1 || coilerDirection < -1 || coilerDirection == 0) {
coilerDirection = 1;
}
if (winderDirection >1 || winderDirection < -1 || winderDirection == 0) {
winderDirection = 1;
}
if (speed > 128 || speed < 32) {
speed = 128;
}
if (digitalRead(XE1) == LOW) {
coiler.drive(64, 2000);
}
if (digitalRead(XE2) == LOW) {
coiler.drive(-64, 2000);
}
// wait 3 seconds for bootup
delay(3000);
Serial.begin(9600);
}
// check endstop is triggered, if it is, stop motor for coiler and send it the other way
void checkEndstops() {
if ((digitalRead(XE1) == LOW || digitalRead(XE2) == LOW) && (millis() - endstopTriggerTime>endstopDebounceTime) && !stopped) {
coilerDirection=coilerDirection*-1;
EEPROM.update(addr, coilerDirection);
endstopTriggerTime = millis();
}
}
// reverse direction or stop every third press
void checkButtonPress() {
long buttonHoldStartTime = 0;
while (digitalRead(BUTTON) == LOW) {
if (buttonHoldStartTime == 0) {
buttonHoldStartTime = millis();
}
delay(100);
// if (buttonHoldStartTime!=0 && (millis() - buttonHoldStartTime) > buttonHoldActionTime) {
// break;
// }
}
if (buttonHoldStartTime!=0) {
unsigned long buttonHoldTime = millis() - buttonHoldStartTime;
Serial.print("button was pressed for: ");
Serial.println(buttonHoldTime);
if (buttonHoldTime>=buttonHoldActionTime) {
Serial.println("button held, changing speed");
// hold action change speed
int oldspeed = speed;
speed = speed - 32;
if (speed < 32 || speed > 128) {
speed = 128;
}
if (oldspeed != speed) {
EEPROM.update(addr+2, speed);
}
} else {
// press action
//if (digitalRead(BUTTON) == LOW && millis() - buttonTriggerTime > buttonDebounceTime && stopped) {
if (buttonHoldTime - buttonTriggerTime > buttonDebounceTime && stopped) {
Serial.println("button click, starting because was stopped");
stopped = false;
buttonTriggerTime = millis();
buttonPresses++;
// stop
//} else if (digitalRead(BUTTON) == LOW && millis() - buttonTriggerTime > buttonDebounceTime && buttonPresses % 2 == 0) {
} else if (buttonHoldTime - buttonTriggerTime > buttonDebounceTime && buttonPresses % 2 == 0) {
Serial.println("button click, stopping");
stopped = true;
winderDirection = winderDirection*-1;
coilerDirection = coilerDirection*-1;
buttonTriggerTime = millis();
buttonPresses++;
// swich direction
//} else if (digitalRead(BUTTON) == LOW && millis() - buttonTriggerTime > buttonDebounceTime && !stopped) {
} else if (buttonHoldTime - buttonTriggerTime > buttonDebounceTime && !stopped) {
Serial.println("button click, changing dir");
winderDirection = winderDirection*-1;
coilerDirection = coilerDirection*-1;
EEPROM.update(addr, coilerDirection);
EEPROM.update(addr+1, winderDirection);
buttonTriggerTime = millis();
buttonPresses++;
}
}
}
}
void loop() {
checkEndstops();
checkButtonPress();
if (!stopped) {
digitalWrite(STBY, HIGH);
winder.drive((speed * winderDirection));
coiler.drive((speed * coilerDirection));
} else {
digitalWrite(STBY, LOW);
}
if (!stopped) {
if (coilerDirection>0) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
} else {
digitalWrite(LED, ledState);
ledState = !ledState;
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment