Hi Sterretje, I will repost my code, but I have four tabs. I will paste them after one another. The reason I am using the Software serial is because of the fact that the Leonardo is inside the PLC and I am not too familiar with it. There is a RS-232 port on the PLC but I am not sure what the voltage of the port is as the Nextion is apparently very sensitive to voltages.
Main code:
#include <SoftwareSerial.h>
/*------------------Encoder----------------------*/
//-------------------Pins-------------------------
#define ClockPin I0_0
#define DataPin I0_1
#define Multiplier 60000.0
volatile long EncoderCounter = 0;
long Counter = 0;
volatile float SpeedInRPM = 0;
/*------------------CONTROLS----------------------*/
//-------------------Feeder------------------------
//-------------------Inputs------------------------
#define InForward I0_5 //Digital pin for direction forward
#define InReverse I0_4 //Digital pin for direction reverse
#define InStartStop I0_6 //Digital pin for direction start/stop
#define Emergency I0_9 //Digital pin for emergency
//-------------------Output------------------------
#define Run Q0_0 //Digital output pin for direction forward
#define Dir Q0_1 //Digital output pin for direction reverse
#define Speed A0_6 //Analog out for speed
//-------------------Cutter------------------------
#define CutterPin I0_7 //Input pin to activate cutter
#define CutOut Q0_3 //Output pin to activate cutter
bool buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0;// the last time the output pin was toggled
unsigned long debounceDelay = 1000;// the debounce time; increase if the output
unsigned long LastCutTime = 0; // the last time the output pin was toggled
unsigned long CutDelay = 2500; // the debounce time; increase if the output
bool RunPhase;
bool ButtonGate = 0;
bool CutGate = 0;
/*----------------Calculations------------------*/
//------------Length to go and speeds--------------
float Length = 0;
float Circum = 565.48;
int RunSpeed = 255;
bool LengthGate = 0;
bool EmGate = 0;
bool BatchGate = 0;
bool RunState = 0;
boolean newButtonState;
static boolean oldButtonState = false;
//--------------------Data from Nextion------------
float MMPerPulse;
int ActLenCal;
int SetLength;
int SetBatch;
float RunPer = 50;
float InchPer = 10;
float CrawlPer = 05;
int InchDis = 1000;
int CrawlDis = 500;
int SetLenCal = 1000;
int PulsePerRev = 1000;
//---------------------Data to Nextion--------------
float LUpdate = 0;
int BUpdate = 0;
int AdjustPer;
int AdjustMM;
float StepsPerMM;
float MMPerRev;
static unsigned long SpamTimer;
void onPin2CHANGECallBackFunction(uint32_t Time, uint32_t PinsChanged, uint32_t Pins){
static uint32_t lTime; // Saved Last Time of Last Pulse
uint32_t cTime; // Current Time
cTime = micros(); // Store the time for RPM Calculations
int32_t dTime; // Delta in time
// Encoder Code
bool DataPinVal = digitalRead(DataPin);
EncoderCounter += (DataPinVal) ? 1 : -1;
// calculate the DeltaT between pulses
dTime = cTime - lTime;
lTime = cTime;
SpeedInRPM = Multiplier / ((DataPinVal) ? dTime: (-1 * dTime)); // Calculate the RPM Switch DeltaT to either positive or negative to represent Forward or reverse RPM
}
SoftwareSerial mySerial(14,15); // RX, TX pins for 15,14
union{
char charByte[4];
long valLong;
}value;
String dfd = "";
void setup() {
Serial.begin(9600);
pinMode(ClockPin, INPUT);
pinMode(DataPin, INPUT);
pinMode(InForward, INPUT);
pinMode(InReverse, INPUT);
pinMode(InStartStop, INPUT);
pinMode(CutterPin, INPUT);
pinMode(Speed, INPUT);
pinMode(Emergency, INPUT);
pinMode(CutOut, OUTPUT);
pinMode(Run, OUTPUT);
pinMode(Dir, OUTPUT);
attachInterrupt(digitalPinToInterrupt(ClockPin),onPin2CHANGECallBackFunction,RISING);
Serial.println("Welcome to FreeStock Mini Cut-to-length!");
mySerial.begin(9600);
}
void loop() {
if(mySerial.available()) {
dfd+=char(mySerial.read());
NexRead();
}
//EmergencyButton();
Jog();
//ButtonCheck();
//LengteCheck();
Update();
//Feed = (SpeedInRPM*Circum)/1000;
//if(RunPhase == 1 && BatchGate == 0){
LengteCheck();
// }
}
Functions:
void LengthToGo(){
long TargetLength = SetLength - Length;
int FSpeed;
if((TargetLength - CrawlDis) < Length && LengthGate == 0){
FSpeed = (RunSpeed*CrawlPer)/100;
digitalWrite(Dir,HIGH);
digitalWrite(Run,HIGH);
analogWrite(Speed,FSpeed);
Serial.print(" Crawl-");Serial.print(FSpeed);Serial.print(" Length-");Serial.println(Length);
Update();
LengthGate = 1;
}else if((TargetLength - InchDis) < Length && LengthGate == 1){
FSpeed = (RunSpeed*InchPer)/100;
digitalWrite(Dir,HIGH);
digitalWrite(Run,HIGH);
analogWrite(Speed,FSpeed);
Serial.print(" Inch-");Serial.print(FSpeed);Serial.print(" Length-");Serial.println(Length);
Update();
}else if(Length >= SetLength){
analogWrite(Speed,0);
digitalWrite(Dir,LOW);
digitalWrite(Run,LOW);
Serial.print(" On Length-");Serial.println(Length);
//CUT();
//Update();
delay(1000);
//BatchCheck();
}
}
void LengteCheck(){
Counter = EncoderCounter;
Length = (Counter*(Circum/PulsePerRev))+(Counter*((Circum/PulsePerRev)*AdjustPer));
}
void Calibration(){
float i;
if(SetLenCal > ActLenCal){
i = (((float)SetLenCal-(float)ActLenCal)/SetLenCal);
AdjustPer = i;
}else{
i = (((float)ActLenCal-(float)SetLenCal)/SetLenCal);
AdjustPer = i*-1; //Not tested on screen
}
}
void EmergencyButton(){
if(digitalRead(Emergency) == LOW && EmGate == 0){
mySerial.print("page page5");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
EmGate = 1;
Counter = 0;
EncoderCounter = 0;
digitalWrite(CutOut,LOW);
digitalWrite(Dir,LOW);
digitalWrite(Run,LOW);
analogWrite(Speed,0);
}else if(digitalRead(Emergency) == HIGH && EmGate == 1){
mySerial.print("page page1");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
EmGate = 0;
}
}
void ButtonCheck(){
//int reading = digitalRead(InStartStop);
if(digitalRead(InStartStop) == HIGH && ButtonGate == 0){
ButtonGate = 1;
lastDebounceTime = millis();
}
if((lastDebounceTime + debounceDelay) < millis() && ButtonGate == 1){
buttonState = !buttonState;
//lastDebounceTime = millis();
ButtonGate = 0;
RunPhase = 1;
//Serial.print(" BatchGate-");Serial.print(BatchGate);Serial.print(" RunPhase-");Serial.print(RunPhase);Serial.print(" State-");Serial.println(buttonState);
}
if(digitalRead(CutterPin) == HIGH && CutGate == 0){
CutGate = 1;
LastCutTime = millis();
digitalWrite(CutOut,HIGH);
}
if((LastCutTime + CutDelay) < millis() && CutGate ==1){
digitalWrite(CutOut,LOW);
CutGate = 0;
}
}
Nextion write:
void Update(){
if((millis() - SpamTimer) >= (250)) {
SpamTimer = millis();
mySerial.print("LUpdate.val="+ String(Length));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
Serial.print(" LUpdate-");Serial.println(Length);
//Serial.print(" Feed-");Serial.println(Feed);
mySerial.print("BUpdate.val="+ String(BUpdate));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
//Serial.print(" BUpdate-");Serial.print(BUpdate);
mySerial.print("SetBatch.val="+ String(SetBatch));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
//Serial.print(" SetBatch-");Serial.print(SetBatch);
mySerial.print("AdjustPer.val="+ String(AdjustPer*100)); //for HMI screen float
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
//Serial.print(" AdjustPer-");Serial.println(AdjustPer);
mySerial.flush();
}
}
Nextion Read:
void NexRead(){
if((dfd.substring(0,3)=="L&Q") && (dfd.length()==11)){ //Length and Quantity
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
SetLength = (value.valLong); //Set Length
Serial.print(" SetLength-");Serial.println(SetLength);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
SetBatch = (value.valLong); //Set Batch
Serial.print(" SetBatch-");Serial.println(SetBatch);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="S&D") && (dfd.length()==23)){ //*************Speeds and distances***************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
RunPer = value.valLong; //Run percentage
Serial.print(" RunPer-");Serial.println(RunPer);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
CrawlPer = value.valLong; //Crawl percentage
Serial.print(" CrawlPer-");Serial.println(CrawlPer);
value.charByte[0] = char(dfd[11]);
value.charByte[1] = char(dfd[12]);
value.charByte[2] = char(dfd[13]);
value.charByte[3] = char(dfd[14]);
InchPer = value.valLong; //Inch percentage
Serial.print(" InchPer-");Serial.println(InchPer);
value.charByte[0] = char(dfd[15]);
value.charByte[1] = char(dfd[16]);
value.charByte[2] = char(dfd[17]);
value.charByte[3] = char(dfd[18]);
InchDis = value.valLong; //Inch distance
Serial.print(" InchDis-");Serial.println(InchDis);
value.charByte[0] = char(dfd[19]);
value.charByte[1] = char(dfd[20]);
value.charByte[2] = char(dfd[21]);
value.charByte[3] = char(dfd[22]);
CrawlDis = value.valLong; //Crawl distance
Serial.print(" CrawlDis-");Serial.println(CrawlDis);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="CAL") && (dfd.length()==11)){ //***************Calibration********************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
SetLenCal = value.valLong; //Length of test run
Serial.print(" SetLenCal-");Serial.println(SetLenCal);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
ActLenCal = value.valLong; //Actual length of test
Serial.print(" ActLenCal-");Serial.println(ActLenCal);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="P&D") && (dfd.length()==11)){ //*********************Pulse and distance***************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
PulsePerRev = value.valLong; //PPR of the encoder
Serial.print(" PulsePerRev-");Serial.println(PulsePerRev);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
MMPerPulse = (value.valLong)/100;
Serial.print(" MMPerPulse-");Serial.println(MMPerPulse);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="CPR") && (dfd.length()==3)){
Serial.println(dfd);
Calibration();
Serial.flush();
dfd="";
}
}
Maths:
void LengthToGo(){
long TargetLength = SetLength - Length;
int FSpeed;
if((TargetLength - CrawlDis) < Length && LengthGate == 0){
FSpeed = (RunSpeed*CrawlPer)/100;
digitalWrite(Dir,HIGH);
digitalWrite(Run,HIGH);
analogWrite(Speed,FSpeed);
Serial.print(" Crawl-");Serial.print(FSpeed);Serial.print(" Length-");Serial.println(Length);
Update();
LengthGate = 1;
}else if((TargetLength - InchDis) < Length && LengthGate == 1){
FSpeed = (RunSpeed*InchPer)/100;
digitalWrite(Dir,HIGH);
digitalWrite(Run,HIGH);
analogWrite(Speed,FSpeed);
Serial.print(" Inch-");Serial.print(FSpeed);Serial.print(" Length-");Serial.println(Length);
Update();
}else if(Length >= SetLength){
analogWrite(Speed,0);
digitalWrite(Dir,LOW);
digitalWrite(Run,LOW);
Serial.print(" On Length-");Serial.println(Length);
//CUT();
//Update();
delay(1000);
//BatchCheck();
}
}
void LengteCheck(){
Counter = EncoderCounter;
Length = (Counter*(Circum/PulsePerRev))+(Counter*((Circum/PulsePerRev)*AdjustPer));
}
void Calibration(){
float i;
if(SetLenCal > ActLenCal){
i = (((float)SetLenCal-(float)ActLenCal)/SetLenCal);
AdjustPer = i;
}else{
i = (((float)ActLenCal-(float)SetLenCal)/SetLenCal);
AdjustPer = i*-1; //Not tested on screen
}
}
I will look into the Leonardo and see if I can figure out what other serial I can use.