The code doesn't look very professional and there are still some fixing to function properly. Anyways the main concern is the interference part occurs when the motor driver is powered.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
bool flagVal1 = 0, flagVal2 = 0;
int val1 = 0; //VTBI Temp name
int val2 = 0;
int val3 = 0; //rename later on
//LCD Menu Logic
const int numOfScreens = 3;
int currentScreen = 0;
String screens[numOfScreens]= {{"VTBI INPUT"}, {"Drop something man"},
{"Press X to Stop"}};
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
}
void loop() {
vtbiCheck();
dripChk();
if(flagVal1==1&&flagVal2==1){
char c;
Serial.println("wew");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(screens[2]);
lcd.setCursor(0,1);
lcd.print("Drop Rate"); //display yung value na needed
do{
c = customKeypad.getKey();
}while(c!='C');
}
}
void vtbiCheck(){
char c;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(screens[0]);
do{
c = customKeypad.getKey();
if(c=='*'||c=='#'){
Serial.println("invalid input");
clearLine(0,1);
lcd.setCursor(2,1);
lcd.print("INVALID INPUT");
delay(1500);
clearLine(0,1);
}
else if(c=='A'){
//increment ml
val1 = val1 + 100;
clearLine(0,1);
lcd.print(val1); // print line here
lcd.print(" ml");
Serial.println("hey");
}
else if (c=='B'){
//decrement value of ml
val1 = val1 - 100;
clearLine(0,1);
lcd.print(val1);
lcd.print(" ml");
Serial.println("hey1");
}
else if(c=='D'||c=='C'){
break;
}
else{
//output value of ml
if(c!=NO_KEY){
val1 = c - '0';
val1 = val1*100;
clearLine(0,1);
lcd.print(val1);
lcd.print(" ml");
Serial.println("hey2");
}
}
}while(true);
if(c=='C'){
flagVal1 = 0;
val1 = 0;
Serial.println("Cancelled");
}
if(val1<=0||val1>1000){
flagVal1 = 0;
Serial.println("Out of bounds");
}
else{
flagVal1 = 1;
Serial.println("Success");
}
}
void dripChk(){
char c;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(screens[1]);
do{
c = customKeypad.getKey();
if(c=='*'||c=='#'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0'){
Serial.println("invalid input");
clearLine(0,1);
lcd.setCursor(2,1);
lcd.print("INVALID INPUT");
delay(1500);
clearLine(0,1);
}
else if(c=='A'){
//increment ml
val2 = val2 + 40;
clearLine(0,1);
lcd.print(val2); // print line here
lcd.print(" drops/hr"); //recheck
Serial.println("hey");
}
else if (c=='B'){
//decrement value of ml
val2 = val2 - 40;
clearLine(0,1);
lcd.print(val2);
lcd.print(" drops/hr"); //check
Serial.println("hey1"); //all serial prints for checking only
}
else if(c=='D'||c=='C'){
break;
}
else{
//output value of ml
if(c!=NO_KEY){
val2 = c - '0';
val2 = val2*40;
clearLine(0,1);
lcd.print(val2);
lcd.print(" drops/hr");
Serial.println("hey2");
}
}
}while(true);
if(c=='C'){
flagVal2 = 0;
val2 = 0;
Serial.println("Cancelled");
}
if(val2<=0||val1>200){
flagVal2 = 0;
Serial.println("Out of bounds");
}
else{
flagVal2 = 2;
Serial.println("Success");
}
}
void pump(){
//stepper
}
void clearLine(int a, int b){
lcd.setCursor(a,b);
lcd.print(" ");
lcd.setCursor(2,1);
}
The code is not the current version that I am using bc its with my classmate but Im telling you, the connections and the code is fine. The project is already functioning well since all codes are direct copies from the library.
The only problem is the random keypad reads (which is connected at pins 22-29) whenever the stepper driver is powered. We've originally used a tb6600 stepper driver but the manual dip switch control for the microstepping is a problem. Originally, the random key problem doesn't occur since (from what Ive searched) the tb6600 is isolated with optocouplers.
Ive searched ground loops interference, power supply coupling and etc etc but none has worked for me so far. I twisted the wires of the stepper motor in pair, placed some variety of capacitor to the 12v supply (i tried series, parallel).
If anyone asks, I'm making an Infusion pump.
And yes, my sketch didn't help i know. Lastly, please bare my english.