-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpotWelder.ino
166 lines (132 loc) · 4.06 KB
/
SpotWelder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <LiquidCrystal.h>
#include <Encoder.h>
#define weld_button_pin 6
#define weld_output 7
#define probePin A0
#define capacitorPin A1
#define capacitorVoltageMultiplier 0.01523
unsigned long lastVoltageReadingTime = 0;
int lastCapacitorVoltage = 0;
//LCD pins
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Encoder Pins
#define encoder_button_pin 2
#define encoder_A 3
#define encoder_B 4
Encoder myEnc(encoder_A, encoder_B);
//menu values
//[man/auto,first_pulse_ms,pulse_delay_ms,second_pulse_ms,cooldown_ms]
//man/auto 0=manual, 1=automatic
int menu_values[] = {0,1,50,10,1000};
int menu_max_values[] = {1,100,500,100,6000};
char* menu_text[] = {"auto: ","clean:","delay:","pulse:","wait: "};
//selection menu vars
#define menu_size 2
int menu_n = 0; //menu item selection number
int menu_order[] = {3,0,1,2};
//menu layout:
//pulse: 0-100ms
//automatic: 0/1
int menu_level = 0; //0=top level menu, 1=adjusting a parameter
int encoder0Pos = 0;
int encoder0PinALast = LOW;
long oldPosition = -999;
void setup() {
pinMode(weld_button_pin, INPUT);
pinMode(weld_output, OUTPUT);
pinMode(encoder_button_pin, INPUT);
pinMode(encoder_A, INPUT);
pinMode(encoder_B, INPUT);
//Serial.begin (9600);
lcd.begin(16, 2);
}
int armed = 1; //variable to stop triggering immidietly after
void loop() {
int encoder_dir = readEncoder();
int autoTrigger = menu_values[0] && analogRead(probePin)>100;
if(menu_values[0]==0){//man
armed = armed || digitalRead(weld_button_pin); //arm if button is not pressed
}else{//auto
int notTouching = analogRead(probePin)<100 && ((getCapVoltage()-lastCapacitorVoltage)<0.1); // wait until not touching and not charging
armed = armed || (notTouching && (digitalRead(weld_button_pin)));
}
if((!digitalRead(weld_button_pin) || autoTrigger) && armed){
armed=0;
if(autoTrigger){delay(500);}
weld();
}
displayMenu();
if(menu_level==0){
menu_n+=encoder_dir;
menu_n=constrain(menu_n,0,1);
}else{//menu_level==1
int value_n = menu_order[menu_n];
menu_values[value_n]+=encoder_dir;
if(menu_values[value_n]>20 && encoder_dir==1){menu_values[value_n]+=4;}
if(menu_values[value_n]>25 && encoder_dir==-1){menu_values[value_n]-=4;}
menu_values[value_n]=constrain(menu_values[value_n],0,menu_max_values[value_n]);
}
if(!digitalRead(encoder_button_pin)){
menu_level=!menu_level;
delay(50);
while(!digitalRead(encoder_button_pin));
delay(50);
}
if(millis()-lastVoltageReadingTime>100){
lastCapacitorVoltage = getCapVoltage();
lastVoltageReadingTime = millis();
}
}
int readEncoder(){
int encoder_dir=0;
long newPosition = myEnc.read()/4;
if (newPosition != oldPosition) {
encoder_dir=(oldPosition>newPosition)*2-1;
oldPosition = newPosition;
}
return encoder_dir;
}
float getCapVoltage(){
return analogRead(capacitorPin)*capacitorVoltageMultiplier;
}
void displayMenu(){
for(int i=0;i<menu_size;i++){
int value_n = menu_order[i];
lcd.setCursor(0, i);
if(menu_n==i){
if(menu_level==0){
lcd.print(">");
}else{
lcd.print("\x7E");//arrow
}
}else{
lcd.print(" ");
}
lcd.print(menu_text[value_n]);
int value = menu_values[value_n];
lcd.print(value);
if(value_n!=0){
lcd.print("ms ");
}
}
lcd.print(" ");
lcd.print(getCapVoltage(),1);
lcd.print("V ");
}
void weld(){
int first_pulse_ms = menu_values[1];
int pulse_delay = menu_values[2];
int second_pulse_ms = menu_values[3];
int cooldown_ms = menu_values[4];
lcd.setCursor(0, 0);
lcd.print("Welding! ");
digitalWrite(weld_output, HIGH);
delay(first_pulse_ms);
digitalWrite(weld_output, LOW);
delay(pulse_delay);
digitalWrite(weld_output, HIGH);
delay(second_pulse_ms);
digitalWrite(weld_output, LOW);
delay(cooldown_ms);
}