Skip to content

Instantly share code, notes, and snippets.

@gbraad
Created September 11, 2021 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbraad/6976560fd13d04322ea221ecdac52685 to your computer and use it in GitHub Desktop.
Save gbraad/6976560fd13d04322ea221ecdac52685 to your computer and use it in GitHub Desktop.
Spotwelder timer
//Sketch by Robert Dunn - Under Dunn
//Sept-8-2021
//*************************************************************
//the connection pins of the 3461BS 4-digit 7-segment display
//each segment must be connected through a 220 ohm resistor
const int SEG_A = 2;
const int SEG_B = 3;
const int SEG_C = 4;
const int SEG_D = 5;
const int SEG_E = 6;
const int SEG_F = 7;
const int SEG_G = 8;
const int SEG_DOT = 9;
const int DIGIT_1 = 10;
const int DIGIT_2 = 11;
const int DIGIT_3 = 12;
const int DIGIT_4 = 13;
//************************************************************
//connection pins of the rotary potentiometer, spst trigger button, and the external relay
//the rotary pot is the only one required to be connected through an analog pin
const int ROT_POT_PIN = A0;
const int BUTTON_PIN = A1;
const int RELAY_PIN = A2;
//************************************************************
const int GHOST_DELAY = 5; //time in milliseconds
//*************************************************************
void setup()
{
Serial.begin(9600);
pinMode(SEG_A, OUTPUT);
pinMode(SEG_B, OUTPUT);
pinMode(SEG_C, OUTPUT);
pinMode(SEG_D, OUTPUT);
pinMode(SEG_E, OUTPUT);
pinMode(SEG_F, OUTPUT);
pinMode(SEG_G, OUTPUT);
pinMode(DIGIT_1, OUTPUT);
pinMode(DIGIT_2, OUTPUT);
pinMode(DIGIT_3, OUTPUT);
pinMode(DIGIT_4, OUTPUT);
pinMode(SEG_DOT, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(BUTTON_PIN, HIGH); //activate internal pull-up resistor for two pin button
digitalWrite(RELAY_PIN, LOW);
}
void loop()
{
int millisecondsRoundedToFifty = ((int)(analogRead(ROT_POT_PIN) / 50)) * 50;
displayNumber(millisecondsRoundedToFifty);
if(isButtonPressed()) {
activateRelay(millisecondsRoundedToFifty);
}
}
void activateRelay(int duration)
{
digitalWrite(RELAY_PIN, HIGH);
delay(duration);
digitalWrite(RELAY_PIN, LOW);
}
bool isButtonPressed()
{
return digitalRead(BUTTON_PIN) == LOW;
}
void displayNumber(int num)
{
int digit4 = num % 10;
int digit3 = (num % 100) / 10;
int digit2 = (num % 1000) / 100;
int digit1 = (num / 1000) % 10;
displayDigit(DIGIT_4, digit4);
displayDigit(DIGIT_3, digit3);
displayDigit(DIGIT_2, digit2);
displayDigit(DIGIT_1, digit1);
}
void displayDigit(int whichDigit, int number)
{
digitalWrite(whichDigit, HIGH);
switch (number) {
case 0:
writeSegments(true, true, true, true, true, true, false, false);
break;
case 1:
writeSegments(false, true, true, false, false, false, false, false);
break;
case 2:
writeSegments(true, true, false, true, true, false, true, false);
break;
case 3:
writeSegments(true, true, true, true, false, false, true, false);
break;
case 4:
writeSegments(false, true, true, false, false, true, true, false);
break;
case 5:
writeSegments(true, false, true, true, false, true, true, false);
break;
case 6:
writeSegments(true, false, true, true, true, true, true, false);
break;
case 7:
writeSegments(true, true, true, false, false, false, false, false);
break;
case 8:
writeSegments(true, true, true, true, true, true, true, false);
break;
case 9:
writeSegments(true, true, true, true, false, true, true, false);
break;
default:
writeSegments(false, false, false, false, false, false, false, false);
}
delay(GHOST_DELAY);
digitalWrite(whichDigit, LOW);
}
void writeSegments(bool segAOn, bool segBOn, bool segCOn, bool segDOn, bool segEOn, bool segFOn, bool segGOn, bool segDotOn)
{
digitalWrite(SEG_A, segAOn ? LOW : HIGH);
digitalWrite(SEG_B, segBOn ? LOW : HIGH);
digitalWrite(SEG_C, segCOn ? LOW : HIGH);
digitalWrite(SEG_D, segDOn ? LOW : HIGH);
digitalWrite(SEG_E, segEOn ? LOW : HIGH);
digitalWrite(SEG_F, segFOn ? LOW : HIGH);
digitalWrite(SEG_G, segGOn ? LOW : HIGH);
digitalWrite(SEG_DOT, segDotOn ? LOW : HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment