Hello. I have 12V Pneumatic Solenoid with TB6600 Stepper Driver connected to a nano. I am trying to use Nextion Screen to display motor speed using Numpad and buttons for forward and reverse motor direction. For whatever reason my code is not working. Please help me out here. Thank you.
#include <Nextion.h>
//Define pins
#define relayPin 5
#define enPin 3
#define stepPin 2
#define dirPin 4
unsigned long mdelay=270;
unsigned long Speed=200;
unsigned long rdelay=200;
unsigned long tdelay=200;
NexNumber n0 = NexNumber(0,2,"n0");
NexButton b0 = NexButton(0,3,"b0");
NexButton b1 = NexButton(0,4,"b1");
NexTouch *nex_listen_list[] = {&n0, &b0, &b1, NULL};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(500);
Serial.print("baud=115200");
Serial.write (0xff);
Serial.write (0xff);
Serial.write (0xff);
Serial.end();
Serial.begin(115200);
pinMode(stepPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(stepPin, HIGH);
digitalWrite(dirPin, HIGH);
// Initialize the Nextion library
nexInit();
// Register the button callback functions with the Nextion objects
b0.attachPush(b0_PushCallback);
b1.attachPush(b1_PushCallback);
//Read the values entered by the user
if (nexSerial.available()){
uint32_t objID =nexObject.getLastNum();
// Check if the value was sent from the numeric keypad component with ID n0
if (objID == 2) {
Speed = nexObject.getNumber("n0");
}
}
}
void loop() {
nexLoop(nex_listen_list);
// put your main code here, to run repeatedly:
for ( int x=0; x <Speed; x++){
digitalWrite(stepPin,LOW);
delayMicroseconds(mdelay); // Use the Speed variable to control the delay
digitalWrite(stepPin,HIGH);
delayMicroseconds(mdelay); // Use the Speed variable to control the delay
}
digitalWrite(relayPin,HIGH);
delay(rdelay);
digitalWrite(relayPin,LOW);
delay(tdelay);
}
//FORWARD BUTTON//
void b0_PushCallback (void *ptr) {
digitalWrite(dirPin, LOW);
}
//REVERSE BUTTON//
void b1_PushCallback (void *ptr) {
digitalWrite(dirPin, HIGH);
}