Create a gist now

Instantly share code, notes, and snippets.

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 800
#define ACCELERATION 4000
#define ZERO_SPEED 400
#define MODE_CUT 0
#define MODE_ZERO 1
#define MODE_SET_BLADE_WIDTH 2
#define MODE_SET_BLADE_ZERO 3
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 = 1;
int cutstep = 0;
float bladewidth = 0;
float bladezero = 10;
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();
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 moveStep(){
moveTo(cutstep*bladewidth+bladezero);
}
/**************************************************************/
void handleZero(){
zeromove = false;
stepper.setCurrentPosition(0);
moveTo(bladezero);
cutstep = 0;
displayMode();
}
void handleOk(){
if(mode == MODE_SET_BLADE_WIDTH && changed) writeBladeWidthToEEPROM();
if(mode == MODE_SET_BLADE_ZERO && changed) writeBladeZeroToEEPROM();
changed = false;
mode = (mode+1)%4;
displayMode();
}
//---------------------
void handleUp(){
switch(mode){
case MODE_CUT:
cutstep ++;
moveStep();
break;
case MODE_ZERO:
break;
case MODE_SET_BLADE_WIDTH:
bladewidth += 0.01;
break;
case MODE_SET_BLADE_ZERO:
bladezero += 0.01;
moveStep();
break;
}
changed = true;
displayUpdate();
}
//---------------------
void handleDown(){
switch(mode){
case MODE_CUT:
if(cutstep > 0) cutstep --;
moveStep();
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 > 2) bladezero -= 0.01;
moveStep();
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;
moveStep();
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 > 2) bladezero -= 0.01;
moveStep();
break;
}
changed = true;
displayUpdate();
}
//---------------------
void displayMode(){
lcd.clear();
switch(mode){
case MODE_CUT:
lcd.print("Cut");
lcd.setCursor(0, 1);
lcd.print("Step: ");
lcd.print(cutstep);
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;
}
}
//---------------------
void displayUpdate(){
switch(mode){
case MODE_CUT:
lcd.setCursor(6, 1);
lcd.print(cutstep);
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;
}
}
/**************************************************************/
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, 2);
}
void writeBladeZeroToEEPROM(){
int b = bladezero*100;
EEPROM.write(2, (byte)(b >> 8));
EEPROM.write(3, (byte)b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment