Skip to content

Instantly share code, notes, and snippets.

@tmuthesius
Last active May 12, 2020 06:16
Show Gist options
  • Save tmuthesius/3002520 to your computer and use it in GitHub Desktop.
Save tmuthesius/3002520 to your computer and use it in GitHub Desktop.
Arduino Firmware of my Table Saw Box Joint Jig
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <Button.h> //https://github.com/carlynorama/Arduino-Library-Button
#include <AccelStepper.h> //http://www.open.com.au/mikem/arduino/AccelStepper/
#define STEPS_MM 486
#define MAX_SPEED 700
#define ACCELERATION 4000
#define ZERO_SPEED 700
#define MIN_BLADE_ZERO 2
#define MODE_CUT_JOINT 0
#define MODE_CUT_TENON 1
#define MODE_ZERO 2
#define MODE_SET_BLADE_WIDTH 3
#define MODE_SET_BLADE_ZERO 4
#define MODE_SET_TENON_START 5
#define MODE_SET_TENON_WIDTH 6
#define MODE_COUNT 7
AccelStepper stepper(1, 3, 2);
LiquidCrystal lcd(9, 8, 13, 12, 11, 10);
Button zero(7, LOW);
Button up(6, LOW);
Button ok(5, LOW);
Button down(4, LOW);
boolean changed = false;
int mode = 2;
int jointstep = 0;
int tenonstep = 0;
float bladewidth = 0;
float bladezero = 10;
float tenonstart = 8;
float tenonwidth = 8;
boolean zeromove = false;
/**************************************************************/
void setup(){
Serial.begin(9600);
Serial.println("ACTSBJJ");
stepper.setMaxSpeed(MAX_SPEED);
stepper.setAcceleration(ACCELERATION);
lcd.begin(16, 2);
lcd.print("ACTSBJJ");
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
delay(1000);
readBladeWidthFromEEPROM();
readBladeZeroFromEEPROM();
readTenonStartFromEEPROM();
readTenonWidthFromEEPROM();
displayMode();
}
//---------------------
void loop(){
zero.listen();
up.listen();
ok.listen();
down.listen();
if(zero.onPress()) handleZero();
if(zeromove){
stepper.runSpeed();
return;
}
if(stepper.run()) return;
if(ok.onPress()) handleOk();
if(up.onPress()) handleUp();
if(down.onPress()) handleDown();
if(up.isHold()) handleUpHold();
if(down.isHold()) handleDownHold();
}
//---------------------
void moveTo(float mmpos){
stepper.moveTo(mmpos * STEPS_MM);
}
void moveBladeZero(){
jointstep = 0;
tenonstep = 0;
moveTo(bladezero);
}
void moveJointStep(){
moveTo(bladezero+jointstep*bladewidth);
}
void moveTenonStep(){
switch(tenonstep){
case 0:
moveTo(bladezero);
break;
case 1:
moveTo(bladezero+tenonstart);
break;
case 2:
moveTo(bladezero+tenonstart+tenonwidth+bladewidth);
break;
}
}
/**************************************************************/
void handleZero(){
zeromove = false;
stepper.setCurrentPosition(0);
moveTo(bladezero);
jointstep = 0;
tenonstep = 0;
displayMode();
}
void handleOk(){
if(mode == MODE_SET_BLADE_WIDTH && changed) writeBladeWidthToEEPROM();
if(mode == MODE_SET_BLADE_ZERO && changed) writeBladeZeroToEEPROM();
if(mode == MODE_SET_TENON_START && changed) writeTenonStartToEEPROM();
if(mode == MODE_SET_TENON_WIDTH && changed) writeTenonWidthToEEPROM();
changed = false;
mode = (mode+1)%MODE_COUNT;
displayMode();
}
//---------------------
void handleUp(){
switch(mode){
case MODE_CUT_JOINT:
jointstep ++;
moveJointStep();
break;
case MODE_CUT_TENON:
if(tenonstep < 2) tenonstep ++;
moveTenonStep();
break;
case MODE_ZERO:
break;
case MODE_SET_BLADE_WIDTH:
bladewidth += 0.01;
break;
case MODE_SET_BLADE_ZERO:
bladezero += 0.01;
moveBladeZero();
break;
case MODE_SET_TENON_START:
tenonstart += 0.01;
break;
case MODE_SET_TENON_WIDTH:
tenonwidth += 0.01;
break;
}
changed = true;
displayUpdate();
}
//---------------------
void handleDown(){
switch(mode){
case MODE_CUT_JOINT:
if(jointstep > 0) jointstep --;
moveJointStep();
break;
case MODE_CUT_TENON:
if(tenonstep > 0) tenonstep --;
moveTenonStep();
break;
case MODE_ZERO:
stepper.setSpeed(-ZERO_SPEED);
zeromove = true;
break;
case MODE_SET_BLADE_WIDTH:
if(bladewidth > 0) bladewidth -= 0.01;
break;
case MODE_SET_BLADE_ZERO:
if(bladezero > MIN_BLADE_ZERO) bladezero -= 0.01;
moveBladeZero();
break;
case MODE_SET_TENON_START:
if(tenonstart > 0) tenonstart -= 0.01;
break;
case MODE_SET_TENON_WIDTH:
if(tenonwidth > 0) tenonwidth -= 0.01;
break;
}
changed = true;
displayUpdate();
}
//---------------------
void handleUpHold(){
switch(mode){
case MODE_SET_BLADE_WIDTH:
bladewidth += 0.01;
break;
case MODE_SET_BLADE_ZERO:
bladezero += 0.01;
moveBladeZero();
break;
case MODE_SET_TENON_START:
tenonstart += 0.01;
break;
case MODE_SET_TENON_WIDTH:
tenonwidth += 0.01;
break;
}
changed = true;
displayUpdate();
}
//---------------------
void handleDownHold(){
switch(mode){
case MODE_SET_BLADE_WIDTH:
if(bladewidth > 0) bladewidth -= 0.01;
break;
case MODE_SET_BLADE_ZERO:
if(bladezero > MIN_BLADE_ZERO) bladezero -= 0.01;
moveBladeZero();
break;
case MODE_SET_TENON_START:
if(tenonstart > 0) tenonstart -= 0.01;
break;
case MODE_SET_TENON_WIDTH:
if(tenonwidth > 0) tenonwidth -= 0.01;
break;
}
changed = true;
displayUpdate();
}
//---------------------
void displayMode(){
lcd.clear();
switch(mode){
case MODE_CUT_JOINT:
lcd.print("Box Joint Cut");
lcd.setCursor(0, 1);
lcd.print("Step: ");
lcd.print(jointstep);
break;
case MODE_CUT_TENON:
lcd.print("Tenon Cut");
lcd.setCursor(0, 1);
lcd.print("Step: ");
switch(tenonstep){
case 0:
lcd.print("zero");
break;
case 1:
lcd.print("first cut");
break;
case 2:
lcd.print("second cut");
break;
}
break;
case MODE_ZERO:
lcd.print("Set zero");
lcd.setCursor(0, 1);
lcd.print("> Press down");
break;
case MODE_SET_BLADE_WIDTH:
lcd.print("Blade width");
lcd.setCursor(0, 1);
lcd.print(bladewidth);
lcd.print(" mm");
break;
case MODE_SET_BLADE_ZERO:
lcd.print("Blade zero");
lcd.setCursor(0, 1);
lcd.print(bladezero);
lcd.print(" mm");
break;
case MODE_SET_TENON_START:
lcd.print("Tenon start");
lcd.setCursor(0, 1);
lcd.print(tenonstart);
lcd.print(" mm");
break;
case MODE_SET_TENON_WIDTH:
lcd.print("Tenon width");
lcd.setCursor(0, 1);
lcd.print(tenonwidth);
lcd.print(" mm");
break;
}
}
//---------------------
void displayUpdate(){
switch(mode){
case MODE_CUT_JOINT:
lcd.setCursor(6, 1);
lcd.print(jointstep);
lcd.print(" ");
break;
case MODE_CUT_TENON:
lcd.setCursor(6, 1);
switch(tenonstep){
case 0:
lcd.print("zero");
break;
case 1:
lcd.print("first cut");
break;
case 2:
lcd.print("second cut");
break;
}
lcd.print(" ");
break;
case MODE_ZERO:
if(zeromove){
lcd.clear();
lcd.print("Setting zero...");
}
break;
case MODE_SET_BLADE_WIDTH:
lcd.setCursor(0, 1);
lcd.print(bladewidth);
lcd.print(" mm ");
break;
case MODE_SET_BLADE_ZERO:
lcd.setCursor(0, 1);
lcd.print(bladezero);
lcd.print(" mm ");
break;
case MODE_SET_TENON_START:
lcd.setCursor(0, 1);
lcd.print(tenonstart);
lcd.print(" mm ");
break;
case MODE_SET_TENON_WIDTH:
lcd.setCursor(0, 1);
lcd.print(tenonwidth);
lcd.print(" mm ");
break;
}
}
/**************************************************************/
void readBladeWidthFromEEPROM(){
int b = (int)EEPROM.read(0) << 8 | (int)EEPROM.read(1);
bladewidth = (float)b/100;
}
void writeBladeWidthToEEPROM(){
int b = bladewidth*100;
EEPROM.write(0, (byte)(b >> 8));
EEPROM.write(1, (byte)b);
}
//---------------------
void readBladeZeroFromEEPROM(){
int b = (int)EEPROM.read(2) << 8 | (int)EEPROM.read(3);
bladezero = max((float)b/100, MIN_BLADE_ZERO);
}
void writeBladeZeroToEEPROM(){
int b = bladezero*100;
EEPROM.write(2, (byte)(b >> 8));
EEPROM.write(3, (byte)b);
}
//---------------------
void readTenonStartFromEEPROM(){
int b = (int)EEPROM.read(4) << 8 | (int)EEPROM.read(5);
tenonstart = (float)b/100;
}
void writeTenonStartToEEPROM(){
int b = tenonstart*100;
EEPROM.write(4, (byte)(b >> 8));
EEPROM.write(5, (byte)b);
}
//---------------------
void readTenonWidthFromEEPROM(){
int b = (int)EEPROM.read(6) << 8 | (int)EEPROM.read(7);
tenonwidth = (float)b/100;
}
void writeTenonWidthToEEPROM(){
int b = tenonwidth*100;
EEPROM.write(6, (byte)(b >> 8));
EEPROM.write(7, (byte)b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment