-
Notifications
You must be signed in to change notification settings - Fork 0
/
DT_Scales.ino
179 lines (134 loc) · 3.97 KB
/
DT_Scales.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
#include "HX711.h" //include lib for scale ADC
HX711 scale(7,8); //initiate scale on pins 7 &
#include <LCD.h> //include LCD libs
#include <LiquidCrystal_I2C.h> //i2c LIB for LCD
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin); //Decalre LCD
void lcdREFRESH(){
lcd.clear(); //refresh lcd
lcd.setCursor(1, 0);// set lcd to "home"
}
//TEAR
#define tear_pin 10 //pin for tear button
int tear_offset = 0; //declare the tear veriable as a global
int read_scale_adjusted(bool tear){ //return the adjusteed readering of the scale
//int RAW_read = scale.read(); //read the scale for procesing later
//int MAP_read = (RAW_read+198729);
//MAP_read = MAP_read/-369;
int MAP_read = map(scale.read(), -196601.7354, -208234.47, 0, 41);
delay(100);
if(tear == true){
return(MAP_read+tear_offset);
}else{
return(MAP_read);
}
}
//FOODS
String foods[3] = {"Flower ", "Eggs ", "Suggar3 "};
//SHIFT REGISTER
#define SER 6 // data in
#define SRCLK 5 // shift register clock
#define SRCLR 4 // clear shift register
#define RCLK 3 // storage register
// clear the shift registers without affecting the storage registers.
void clearShiftRegisters() {
digitalWrite(SRCLR,LOW);
digitalWrite(SRCLR,HIGH);
}
// All the data in the shift registers moves over 1 unit and the new data goes in at shift register 0.
// The data that was in the shift register 7 goes to the next register (if any).
void shiftDataIn(int data) {
digitalWrite(SER,data);
digitalWrite(SRCLK,HIGH);
digitalWrite(SRCLK,LOW);
}
// copy the 8 shift registers into the shift registers,
// which changes the output voltage of the pins.
void copyShiftToStorage() {
digitalWrite(RCLK,HIGH);
digitalWrite(RCLK,LOW);
}
//output two numbers to the shift registers.
void outputLED(int v1, int v2){
clearShiftRegisters();
for (int i = 0; i < 10; i++){
if(v1>0){
shiftDataIn(1);
v1=v1-1;
}else{
shiftDataIn(0);
}
}
for (int i = 0; i < 10; i++){
if(v2>0){
shiftDataIn(1);
v2=v2-1;
}else{
shiftDataIn(0);
}
}
copyShiftToStorage();
}//end outputLED
//ROTARY ENCODER
#define DT 2 //pin for rot encoder
#define CLK 9 //pin for rot encoder
unsigned short int encoder0POS = 0; //value for rot encoder mode A
volatile bool turndetected;
volatile bool up;
void encoderisr () {
turndetected = true;
up = (digitalRead(CLK) == digitalRead(DT));
}
void encoderREFRESH(){ //function to update encoder
if (turndetected) {//if isr flag is set
if(up){
encoder0POS++;
}else{
encoder0POS--;
}
turndetected = false; //rest isr flag
#if defined(SERIAL_DEBUG)
Serial.println("ENCODER REFRESHED");
#endif
}
}//end encoderREFRESH
void setup() {
pinMode(13, OUTPUT);
lcd.begin(16, 2); //begin LCD
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home(); // go home
pinMode(SER,OUTPUT);
pinMode(SRCLK,OUTPUT);
pinMode(SRCLR,OUTPUT);
pinMode(RCLK,OUTPUT);
pinMode(tear_pin, INPUT_PULLUP);
clearShiftRegisters();
lcdREFRESH();//clear LCD
tear_offset = 0 - read_scale_adjusted(false);
Serial.begin(9600);
attachInterrupt (digitalPinToInterrupt(2),encoderisr,FALLING); //rotary encoder isr
}
void loop() {
encoderREFRESH();//refresh encoder
//outputLED(map(read_scale_adjusted(true), 0, 50,0,10), 5);
outputLED(10,10);
char msg[21];
lcd.setCursor(0, 0);// Next line
lcd.print("Food: " + foods[encoder0POS % 3]);
sprintf(msg, "Mass: %-7d", encoder0POS);
lcd.setCursor(0, 1);// Next line
lcd.print(msg);
if(digitalRead(tear_pin) == LOW){//if the tear button has been pressed
tear_offset = 0 - read_scale_adjusted(false);
digitalWrite(13,HIGH);
}
}