Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include "Nextion.

h"
#include "NexText.h"

int stepPin = 9;
int dirPin = 10;
int Limit = 14;
int Limit_GND = 15;
float MaxDistance = 62.001;
float HomeOffset = 2.515;
double Distance = 0;
double Position = 0;
int Next = 0;
double StepGoal; // goal (steps)
double Goal = 0; //goal (thousandths of an inch)
double SPR = 2902;
double StepNum = 0;

NexButton b14 = NexButton(1,15,"b14"); //Calibrate


NexButton b15 = NexButton(1, 16, "b15");//Home Button
NexButton b12 = NexButton(1, 13, "b12");//Stop Button
NexButton b13 = NexButton(1, 14,"b13"); //Start Button
NexText t3 = NexText(1,21,"t3");//Position Text
NexText t2 = NexText(1,20,"t2");//Next Text
NexText t4 = NexText(1,23,"t4");//Message Text

char buffer[100]={0};
NexTouch *nex_listen_list[] = {
&b14,
&b15,
&b13,
&b12,
&t2,
&t3,
&t4,
NULL
};
void b14PushCallback(void *ptr){//Calibrate
char buffer[7];
char text_char[7];
t2.getText(text_char,7);
Distance = atof(text_char);

if (Distance > MaxDistance || Distance < HomeOffset)


{
t4.setText("Outside Limits");
delay(1500);
t2.setText("");
t4.setText("");
}
else
{
double dVal = Distance;
dtostrf(dVal,0,3,buffer);
t3.setText(buffer);
t2.setText("");

}
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}

void t3PushCallback(void *ptr){

}
void t2PushCallback(void *ptr){

}
void t4PushCallback(void *ptr){

}
void b15PushCallback(void *ptr) {//Homing
uint16_t number;
t4.setText(" ");
while (digitalRead(Limit)) {

digitalWrite(dirPin, LOW);
for (int x = 0; x < 100; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(300);
digitalWrite(stepPin, LOW);
delayMicroseconds(300);
}
}
while (!digitalRead(Limit)) {
digitalWrite(dirPin, HIGH);
for (int x = 0; x < 100; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(300);
digitalWrite(stepPin, LOW);
delayMicroseconds(300);
}}

t4.setText("Homed");
delay(1500);
t4.setText(" ");
t3.setText("2.515");//Home Position
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);

void b12PushCallback(void *ptr) {//Stop


t4.setText("STOP");

digitalWrite (13, LOW);


delay(2000);
digitalWrite (13, HIGH);
t4.setText("");
t4.setText("Press Home");
}
void b13PopCallback(void *ptr){ //Start
char buffer1[7];
char text_char1[7];
t3.getText(text_char1,7);
Position = atof(text_char1);

char buffer[7];
char text_char[7];
t2.getText(text_char,7);
Distance = atof(text_char);

if (Distance > MaxDistance || Distance < HomeOffset)


{
t4.setText("Outside Limits");
delay(1500);
t2.setText("");
t4.setText("");

}
else
{

double dVal = Distance;


dtostrf(dVal,0,3,buffer);
Next=buffer;
t3.setText(Next);
t2.setText("");
uint16_t number;
t4.setText(" ");

StepGoal = ( Distance-Position )*SPR;


StepNum = Goal ;

while (StepNum < StepGoal){


StepNum=StepNum + 5;

digitalWrite(dirPin, HIGH);
for (int x = 0; x < 2; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(50);
digitalWrite(stepPin, LOW);
delayMicroseconds(50);

}}

while (StepNum > StepGoal){


StepNum = StepNum - 5;
digitalWrite(dirPin, LOW);
for (int x = 0; x < 2; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(50);
digitalWrite(stepPin, LOW);
delayMicroseconds(50);
}}

Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}}

void b13PushCallback(void *ptr){//Start


Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);

void setup() {
Serial2.begin(9600);
delay(100);

long StepNum = 0; //counter to know what step the system is on


double SPR = 1203; //Steps per Output Revolution (divide setting on driver by the
gear ratio (2))
int Pulse = 15; //Pulse Input
int Direction = 14; //Direction Input
long Goal = 0; //goal (thousandths of an inch)
long StepGoal; // goal (steps)
float MaxDistance = 90.501; // Sets limit for travel
float HomeOffset = 11.780;

pinMode(Limit, INPUT_PULLUP);
pinMode(Limit_GND, OUTPUT); digitalWrite(Limit_GND, LOW);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
Serial2.begin(9600);
delay(100);

b13.attachPop(b13PopCallback);
b14.attachPush(b14PushCallback);
b15.attachPush(b15PushCallback);
b13.attachPush(b13PushCallback);
b12.attachPush(b12PushCallback);
t2.attachPush(t2PushCallback);
t3.attachPush(t3PushCallback);
t4.attachPush(t3PushCallback);
}
void loop() {
// put your main code here, to run repeatedly:
nexLoop(nex_listen_list);

You might also like