forked from hiveeyes/hanimandl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhani-mandl_v0.0.1.ino
254 lines (220 loc) · 11.9 KB
/
hani-mandl_v0.0.1.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Abfuellwaage Version 0.0.1
--------------------------
Copyright (C) 2018 by Marc Vasterling
2018-05 Marc Vasterling | initial version,
published in the Facebook group "Imkerei und Technik. Eigenbau",
Marc Vasterling: "meinen Code kann jeder frei verwenden, ändern und hochladen wo er will, solange er nicht seinen eigenen Namen drüber setzt."
This code is in the public domain.
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <HX711.h>
#include <ESP32_Servo.h>
Servo myservo;
HX711 scale;
#include <Preferences.h>
Preferences preferences;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);
const int POTI_PIN = 39;
const int SERVO_PIN = 4;
const int start_button = 13;
const int stop_button = 12;
const int betrieb_button = 14;
const int setup_button = 27;
const int HX711_DT = 17;
const int HX711_SCK = 16;
const char* gewicht_char = "";
int pos;
int gewicht;
int menu_kalib;
int menu_korrek;
int tara;
int tara_raw;
int gewicht_raw;
float faktor;
int faktor2;
int fmenge;
int korrektur;
int autostart;
int winkel;
int winkel_min=0;
int winkel_max=155;
int winkel_dosier_min=65;
float fein_dosier_gewicht=50;
int i;
int u;
int a;
int z;
void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(start_button, INPUT);
pinMode(stop_button, INPUT);
pinMode(betrieb_button, INPUT);
pinMode(setup_button, INPUT);
Serial.begin(115200); while (!Serial) { }
u8g2.begin();
scale.begin(HX711_DT,HX711_SCK);
scale.power_up();
myservo.attach(SERVO_PIN, 750, 2500);
// EEPROM //
preferences.begin("EEPROM", false); //faktor und tara aus eeprom lesen
faktor2 = preferences.getUInt("faktor2", 0);
// if (faktor2 == 0) {Serial.println("Waage ist nicht kalibiert!"); for (int i=0; i < 200; i++) {delay(50); digitalWrite(LED, LOW); delay(50); digitalWrite(LED, HIGH);}}
faktor = (faktor2 /10000.00);
tara = preferences.getUInt("tara", 0);
tara_raw = preferences.getUInt("tara_raw", 0);
fmenge = preferences.getUInt("fmenge", 0);
korrektur = preferences.getUInt("korrektur", 0);
autostart = preferences.getUInt("autostart", 0);
Serial.print("faktor = "); Serial.println(faktor);
Serial.print("tara_raw = "); Serial.println(tara_raw);
Serial.print("tara = "); Serial.println(tara);
preferences.end();
}
void loop() {
if ((digitalRead(setup_button))==HIGH){
pos = (map(analogRead(POTI_PIN), 0,4095,1,5));
u8g2.setFont(u8g2_font_courB10_tf);
u8g2.clearBuffer();
u8g2.setCursor(10,8);
u8g2.print("Tara");
u8g2.setCursor(10,22);
u8g2.print("Kalibrieren");
u8g2.setCursor(10,36);
u8g2.print("Korrektur");
u8g2.setCursor(10,50);
u8g2.print("Fuellmenge");
u8g2.setCursor(10,64);
u8g2.print("Autostart");
// Tara //
if (pos==1) {u8g2.setCursor(0,8); u8g2.print("*"); if ((digitalRead(start_button))==HIGH){
tara=((int(scale.read_average(10))-tara_raw)/faktor);
u8g2.setCursor(100,12); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000);
preferences.begin("EEPROM", false);
preferences.putUInt("tara", tara);
preferences.end();
}}
// Kalibrieren //
if (pos==2) {u8g2.setCursor(0,22); u8g2.print("*"); if ((digitalRead(start_button))==HIGH){i=1; delay(300);
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
u8g2.setCursor(10,12); u8g2.print("Bitte 500g"); u8g2.setCursor(10,28); u8g2.print("aufstellen");u8g2.setCursor(10,44); u8g2.print("& mit Start");u8g2.setCursor(10,60); u8g2.print("bestaetigen"); u8g2.sendBuffer();
while(i>0){
if ((digitalRead(start_button))==HIGH){gewicht_raw=(int(scale.read_average(10))); delay(2000); i=0;}
}
i=1;
u8g2.clearBuffer();
u8g2.setCursor(10,12); u8g2.print("Bitte Waage"); u8g2.setCursor(10,28); u8g2.print("leeren");u8g2.setCursor(10,44); u8g2.print("& mit Start");u8g2.setCursor(10,60); u8g2.print("bestaetigen"); u8g2.sendBuffer();
while(i>0){
if ((digitalRead(start_button))==HIGH){tara_raw=(int(scale.read_average(10)));delay(2000); i=0;
faktor=((gewicht_raw-tara_raw)/500.000);
preferences.begin("EEPROM", false); //faktor und tara ins eeprom schreiben
preferences.putUInt("faktor2", (faktor*10000));
preferences.putUInt("tara_raw", tara_raw);
preferences.end();
}
}
}
}
// Korrektur //
if (pos==3) {u8g2.setCursor(0,36); u8g2.print("*");
if ((digitalRead(start_button))==HIGH){i=1; delay(300);
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
while(i>0){
pos = (map(analogRead(POTI_PIN), 0,4095,-25,25));
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
u8g2.setCursor(10,12); u8g2.print("Korrektur"); u8g2.setCursor(40,28); u8g2.print(pos);u8g2.sendBuffer();
if ((digitalRead(start_button))==HIGH){korrektur=pos ; u8g2.setCursor(100,28); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0;}
preferences.begin("EEPROM", false);
preferences.putUInt("korrektur", korrektur);
preferences.end();
}
}
}
// Füllmenge //
if (pos==4) {u8g2.setCursor(0,50); u8g2.print("*"); if ((digitalRead(start_button))==HIGH){i=1; delay(200);
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
while(i>0){
pos = (map(analogRead(POTI_PIN), 0,4095,1,4));
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
u8g2.setCursor(10,12); u8g2.print(" 500g"); u8g2.setCursor(10,28); u8g2.print(" 250g"); u8g2.setCursor(10,44); u8g2.print(" 50g"); u8g2.setCursor(10,60); u8g2.print(" 20g"); u8g2.sendBuffer();
if (pos==1) {u8g2.setCursor(0,12); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){fmenge=500 ; u8g2.setCursor(100,12); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("fmenge", fmenge); preferences.end(); }}
if (pos==2) {u8g2.setCursor(0,28); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){fmenge=250 ; u8g2.setCursor(100,28); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("fmenge", fmenge); preferences.end(); }}
if (pos==3) {u8g2.setCursor(0,44); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){fmenge=50 ; u8g2.setCursor(100,44); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("fmenge", fmenge); preferences.end(); }}
if (pos==4) {u8g2.setCursor(0,60); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){fmenge=20 ; u8g2.setCursor(100,60); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("fmenge", fmenge); preferences.end(); }}
}
}
}
// Autostart //
if (pos==5) {u8g2.setCursor(0,64); u8g2.print("*"); if ((digitalRead(start_button))==HIGH){i=1; delay(200);
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
while(i>0){
pos = (map(analogRead(POTI_PIN), 0,4095,1,2));
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.clearBuffer();
u8g2.setCursor(10,12); u8g2.print("Auto EIN"); u8g2.setCursor(10,28); u8g2.print("Auto AUS"); u8g2.sendBuffer();
if (pos==1) {u8g2.setCursor(0,12); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){autostart=1 ; u8g2.setCursor(105,12); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("autostart", autostart); preferences.end(); }}
if (pos==2) {u8g2.setCursor(0,28); u8g2.print("*"); u8g2.sendBuffer(); if ((digitalRead(start_button))==HIGH){autostart=2 ; u8g2.setCursor(105,28); u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); i=0; preferences.begin("EEPROM", false); preferences.putUInt("autostart", autostart); preferences.end(); }}
}
}
}
u8g2.sendBuffer();
a=0;
}
// Betrieb //
if ((digitalRead(betrieb_button))==HIGH){
pos = (map(analogRead(POTI_PIN), 0,4095,0,100));
gewicht=((((int(scale.read()))-tara_raw)/faktor)-tara);
if ((autostart==1)&&(gewicht<=5)&&(gewicht>=-5)&&(a==0)){delay(1000); if((gewicht<=5)&&(gewicht>=-5)){u8g2.clearBuffer(); u8g2.setFont(u8g2_font_courB24_tf); u8g2.setCursor(20,43); u8g2.print("START"); u8g2.sendBuffer(); delay(3000); a=1;}}
if ((autostart==1)&&(gewicht<-20)){winkel=winkel_min; a=0;}
if ((digitalRead(start_button))==HIGH){a=1;}
if ((digitalRead(stop_button))==HIGH){(winkel=winkel_min); a=0;}
if (a==1){winkel=((winkel_max*pos)/100);}
if ((a==1)&&(fmenge+korrektur-gewicht<=fein_dosier_gewicht)){winkel=((((winkel_max*pos)/100)*((fmenge+korrektur-gewicht)/fein_dosier_gewicht)));}
if ((a==1)&&(winkel<=winkel_dosier_min)){winkel=winkel_dosier_min;}
if ((a==1)&&((gewicht-korrektur)>=fmenge)){winkel=winkel_min; a=0;}
if ((digitalRead(stop_button))==HIGH){(winkel=winkel_min); a=0;}
myservo.write(winkel);
float y = ((fmenge+korrektur-gewicht)/fein_dosier_gewicht);
Serial.println(y);
// Serial.print(scale.read_average(3));Serial.print(" Tara_raw:"); Serial.print(tara_raw);Serial.print(" Faktor "); Serial.print(faktor); ;Serial.print(" Gewicht "); Serial.println(gewicht);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_courB24_tf);
if (gewicht<100) {u8g2.setCursor(55,43);} if (gewicht<10) {u8g2.setCursor(75,43);} if (gewicht>=100) {u8g2.setCursor(35,43);} if (gewicht>=1000) {u8g2.setCursor(15,43);} if (gewicht<0) {u8g2.setCursor(55,43);} if (gewicht<=-10) {u8g2.setCursor(35,43);} if (gewicht<=-100) {u8g2.setCursor(15,43);} u8g2.print(gewicht); u8g2.setCursor(95,43); u8g2.print("g");
u8g2.setFont(u8g2_font_courB14_tf);
// u8g2.setCursor(0,13); u8g2.print("t="); u8g2.setCursor(24,13); u8g2.print(tara);
u8g2.setCursor(0,13); u8g2.print("W="); u8g2.setCursor(24,13); u8g2.print(winkel);
if (autostart==1){u8g2.setCursor(58,13); u8g2.print("AS");}
u8g2.setCursor(0,64); u8g2.print("k="); u8g2.setCursor(24,64); u8g2.print(korrektur);
u8g2.setCursor(73,64); u8g2.print("f="); u8g2.setCursor(97,64); u8g2.print(fmenge);
if (pos<100) {u8g2.setCursor(98,13);} if (pos<10) {u8g2.setCursor(108,13);} if (pos>=100) {u8g2.setCursor(88,13);} u8g2.print(pos); u8g2.setCursor(120,13); u8g2.print(char(37));
u8g2.sendBuffer();
}
// Handbetrieb //
if ((digitalRead(betrieb_button)==LOW) && (digitalRead(setup_button)==LOW)){
pos = (map(analogRead(POTI_PIN), 0,4095,0,100));
gewicht=((((int(scale.read()))-tara_raw)/faktor)-tara);
if ((digitalRead(start_button))==HIGH){a=1;}
if ((digitalRead(stop_button))==HIGH){(winkel=winkel_min); a=0;}
if ((digitalRead(stop_button))==HIGH){(winkel=winkel_min);}
if (a==1){winkel=((winkel_max*pos)/100);}
myservo.write(winkel);
// Serial.print(scale.read_average(3));Serial.print(" Tara_raw:"); Serial.print(tara_raw);Serial.print(" Faktor "); Serial.print(faktor); ;Serial.print(" Gewicht "); Serial.println(gewicht);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_courB24_tf);
if (gewicht<100) {u8g2.setCursor(55,43);} if (gewicht<10) {u8g2.setCursor(75,43);} if (gewicht>=100) {u8g2.setCursor(35,43);} if (gewicht>=1000) {u8g2.setCursor(15,43);} if (gewicht<0) {u8g2.setCursor(55,43);} if (gewicht<=-10) {u8g2.setCursor(35,43);} if (gewicht<=-100) {u8g2.setCursor(15,43);} u8g2.print(gewicht); u8g2.setCursor(95,43); u8g2.print("g");
u8g2.setFont(u8g2_font_courB14_tf);
u8g2.setCursor(0,13); u8g2.print("W="); u8g2.setCursor(24,13); u8g2.print(winkel);
if (pos<100) {u8g2.setCursor(98,13);} if (pos<10) {u8g2.setCursor(108,13);} if (pos>=100) {u8g2.setCursor(88,13);} u8g2.print(pos); u8g2.setCursor(120,13); u8g2.print(char(37));
u8g2.sendBuffer();
//a=0;
}
}