#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <string.h>
#include "HX711.h"
#include<SoftwareSerial.h>
#define trigPin 13
#define echoPin A2
long getDistance(int trigpin,int echopin){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echopin, HIGH);
long distance = duration * 0.034 / 2;
return (distance);
}
SoftwareSerial B(10,11); //10-RX, 11-TX
Servo servoA;
Servo servoB;
Servo servoC;
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1','2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5,4,3,2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9,8,7,6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
scale.begin(A1, A0);
B.begin(9600);
Serial.begin(14400);
}
void lcdStart(char key='o'){
switch(key){
case 'o':
lcd.noBlink();
lcd.setCursor(0,0);
lcd.print("select the ");
lcd.setCursor(0,1);
lcd.print("compartment:A,B");
lcd.setCursor(0,2);
break;
default:
lcd.print(key);
delay (1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("portion en g ");
lcd.setCursor(0,1);
lcd.print("D for confirmation:");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("portion en g ");
}}
//lcdStart WOrks no bugs or changes to do
int vals[3]={0,0,0};
void loop() {
B.print(500);
lcdStart();
char key = keypad.getKey();
//insert code to act on the chosen compart
//done
if(key =='A'||key =='B'){
lcdStart(key);
int i =0;
while(true){
lcd.blink();
const char key2 = keypad.getKey();
if(key2=='#'){
i--;
lcd.setCursor(8+i,1);
lcd.blink();
}
if(key2<=57 &&key2>=48&&i!=3){
lcd.setCursor(8+i,1);
lcd.blink();
int l = int(key2)-48;
vals[i] = l;
lcd.print(l);
i++;
}
if (key2=='D'){
lcd.clear();
for(int k=0;k<3;k++)
lcd.print(vals[k]);
lcd.print(" gramme");
delay(2000);
break;
}
}
int portion = scale.get_units(5)/4 *10;
int input=vals[0]*100 + vals[1]*10+vals[2];
Serial.println(input);
while(portion< input){
Serial.println(portion);
portion = scale.get_units(5)/4 *10;
if(key == 'A'){
servoA.attach(12);
delay(100);
servoA.write(180);
}
if(key == 'B'){
servoB.attach(11);
delay(100);
servoB.write(180);
delay(2000);
}
}
if(portion>= input){
delay(10);
servoA.write(90);
servoB.write(90);
Serial.print('C');
servoC.attach(10);
servoC.write(180);
delay(500);
servoC.write(90);
delay(10);
servoC.detach();
servoB.detach();
servoA.detach();
delay(500);
}
}
}