Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for my battery spot welder.
#include <Encoder.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Encoder enc(2, 3);
#define ENCODER_SW 4
#define SCR 5
#define TRIG_LED 6
#define TRIG_SW 7
#define INT_LED 13
#define DEBOUNCE_DELAY 50
uint16_t preweld_time; // 0x00
uint16_t pause_time; // 0x02
uint16_t weld_time; // 0x04
bool selected = false; // if changing a parameter or not
int8_t option = 0; // which parameter selected
void setup() {
Serial.begin(115200);
pinMode(SCR, OUTPUT);
pinMode(TRIG_LED, OUTPUT);
pinMode(INT_LED, OUTPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(TRIG_SW, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
// Read saved values from EEPROM
preweld_time = readUInt(0x00);
pause_time = readUInt(0x02);
weld_time = readUInt(0x04);
}
long f, t;
void loop() {
f = millis() - t;
t = millis();
handleEncoder();
display.clearDisplay();
display.setCursor(0, 0);
display.println("BSWelder v1.0");
display.println("By Matthew Tran");
displayParameters();
displayArm();
displaySaveOpt();
display.print("1/FPS: ");
display.print(f);
display.println(" ms");
display.display();
}
void handleEncoder() {
long curr = enc.read();
long diff = curr / 4;
enc.write(curr - diff*4);
if (selected) {
switch (option) {
case 0:
preweld_time += diff * 10;
preweld_time = preweld_time/10 * 10;
break;
case 1:
pause_time += diff * 10;
pause_time = pause_time/10 * 10;
break;
case 2:
weld_time += diff * 10;
weld_time = weld_time/10 * 10;
break;
}
} else {
option += diff;
option %= 5;
if (option < 0) {
option += 5;
}
}
if (!digitalRead(ENCODER_SW)) {
delay(DEBOUNCE_DELAY);
selected = !selected;
while(!digitalRead(ENCODER_SW));
}
}
void displayParameters() {
if (option == 0) {
display.print(selected ? "~" : ">");
}
display.print("Preweld: ");
display.print(preweld_time);
display.println(" ms");
if (option == 1) {
display.print(selected ? "~" : ">");
}
display.print("Pause: ");
display.print(pause_time);
display.println(" ms");
if (option == 2) {
display.print(selected ? "~" : ">");
}
display.print("Weld: ");
display.print(weld_time);
display.println(" ms");
}
void displayArm() {
if (option == 3) {
display.print(selected ? "~" : ">");
}
if (option == 3 && selected) {
armMode();
} else {
display.println("Arm?");
}
}
void displaySaveOpt() {
if (option == 4) {
display.print(">");
}
if (option == 4 && selected) {
saveParams();
display.println("Saved!");
selected = false;
} else {
display.println("Save?");
}
}
void armMode() {
display.println("ARMED!");
display.display();
digitalWrite(TRIG_LED, HIGH);
while (digitalRead(ENCODER_SW)) {
if (!digitalRead(TRIG_SW)) {
delay(DEBOUNCE_DELAY);
digitalWrite(SCR, HIGH);
digitalWrite(INT_LED, HIGH);
delay(preweld_time);
digitalWrite(SCR, LOW);
digitalWrite(INT_LED, LOW);
delay(pause_time);
digitalWrite(SCR, HIGH);
digitalWrite(INT_LED, HIGH);
delay(weld_time);
digitalWrite(SCR, LOW);
digitalWrite(INT_LED, LOW);
while(!digitalRead(TRIG_SW));
delay(DEBOUNCE_DELAY);
}
}
selected = !selected;
digitalWrite(TRIG_LED, LOW);
delay(DEBOUNCE_DELAY);
while(!digitalRead(ENCODER_SW));
}
void saveParams() {
writeUInt(0x00, preweld_time);
writeUInt(0x02, pause_time);
writeUInt(0x04, weld_time);
}
void writeUInt(int addr, uint16_t val) {
EEPROM.write(addr, val);
EEPROM.write(addr + 1, val >> 8);
}
uint16_t readUInt(int addr) {
return EEPROM.read(addr) | (EEPROM.read(addr+1) << 8);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment