Hello,
i have a very important project i am working on currently. The ide is to rewire an existing rollforming machine in a simple way. I just input the length of the piece and the quantity of the pieces. Arduino then starts the motor of the machine, once it gets the desired length data from the rotary encoder it stops the machine, triggers the cutters and then restarts the motor…
Everything works (look at the VIDEO) with the exception of one problem. Each piece is a bit different in length. Meaning the encoder and arduino are not comunicating properly.
I have tried using the following solutions:
- 2 × 10 kΩ resistors (pull-ups)
- 2 × 220 Ω series resistors - on encoder signal wires
- 0.1 µF ceramic + 100 µF electrolytic capacitors (power decoupling)
Here is my code:
#include <LiquidCrystal.h>
#include <Encoder.h>
Encoder myEnc(2, 3); // (CLK, DT)
// =======================
// LCD KEYPAD Shield
// =======================
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const int buttonPin = A0;
// =======================
// Releji
// =======================
const int relayPin = 22; // start the škarje
const int relayPin2 = 20; // start/stop da machin
// =======================
// Rotary Encoder (KY-040)
// =======================
const int encoderPinA = 2; //
const int encoderPinB = 3; //
volatile long encoderCount = 0;
long lastEncoderCount = 0;
// =======================
// Spremenljivke
// =======================
float mmPerTick = 0.25; // kalibracija enkodarja : mm per encoder tick
int pieceLength = 1250; // mm
int pieceQuantity = 20;
int producedPieces = 0;
int menuStage = 0; // 0 = set length, 1 = set quantity, 2 = calibration
bool running = false;
// =======================
// Encoder Interrupt
// =======================
void readEncoder() {
int b = digitalRead(encoderPinB);
if (b == HIGH) {
encoderCount++;
} else {
encoderCount--;
}
}
// =======================
// Beri - LCD Keypad Button
// =======================
int readLCDKey() {
int x = analogRead(buttonPin);
if (x < 50) return 0; // Right
if (x < 110) return 1; // Up
if (x < 270) return 2; // Down
if (x < 420) return 3; // Left
if (x < 660) return 4; // Select
return 5; // None
}
// =======================
// Setup
// =======================
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, LOW); //
digitalWrite(relayPin, HIGH);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPinA), readEncoder, CHANGE);
lcd.begin(16, 2);
lcd.print("Cutting Machine");
delay(1500);
lcd.clear();
}
// =======================
// Loop
// =======================
void loop() {
long position = myEnc.read();
if (!running) {
// Menu navigation
lcd.setCursor(0,0);
if (menuStage == 0) {
lcd.print("Len(mm):");
lcd.print(pieceLength);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Set piece length ");
}
else if (menuStage == 1) {
lcd.print("Qty:");
lcd.print(pieceQuantity);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Set total pieces ");
}
else if (menuStage == 2) {
lcd.print("mm/tick:");
lcd.print(mmPerTick, 2); //
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Calibrate encoder");
}
int key = readLCDKey();
if (key == 1) { // Up
if (menuStage == 0) pieceLength += 10;
else if (menuStage == 1) pieceQuantity++;
else if (menuStage == 2) mmPerTick += 0.01;
}
else if (key == 2) { // Down
if (menuStage == 0 && pieceLength > 10) pieceLength -= 10;
else if (menuStage == 1 && pieceQuantity > 1) pieceQuantity--;
else if (menuStage == 2 && mmPerTick > 0.1) mmPerTick -= 0.01;
}
else if (key == 0 || key == 3) { // Right/Left switch menu
menuStage = (menuStage + 1) % 3;
lcd.clear();
}
else if (key == 4) { // Select = Start job
lcd.clear();
lcd.print("Starting..");
delay(2000);
digitalWrite(relayPin2, HIGH);
running = true;
producedPieces = 0;
encoderCount = 0;
lastEncoderCount = 0;
lcd.clear();
}
delay(200);
}
else {
// Running cutting process
lcd.setCursor(0,0);
lcd.print("Cutting ");
lcd.print(producedPieces);
lcd.print("/");
lcd.print(pieceQuantity);
float traveled = abs(encoderCount - lastEncoderCount) * mmPerTick;
lcd.setCursor(0,1);
lcd.print(" "); // clear line
lcd.setCursor(0,1);
lcd.print("Len:");
lcd.print(traveled,1);
lcd.print("/");
lcd.print(pieceLength);
if (traveled >= pieceLength) {
// Trigger relay2
digitalWrite(relayPin2, LOW);
delay(1000); // relay pulse
// Trigger relay
digitalWrite(relayPin, LOW);
delay(500); // relay pulse
digitalWrite(relayPin, HIGH);
delay (1000);
digitalWrite(relayPin2, HIGH);
producedPieces++;
lastEncoderCount = encoderCount; // reset distance
if (producedPieces >= pieceQuantity) {
running = false;
digitalWrite(relayPin2, LOW);
lcd.clear();
lcd.print("Job Finished!");
// Stop motor
lcd.clear();
}
}
}
}
I am using arduino mega and keyestudio 1602 LCD Keypad, rotary encoder is 12volt 5 wire encoder (ground, +, -, signal1, signal 2)
Anything you guys might come up with?


