Howdy folks,
I wrote a little program for a lamination CNC machine that worked properly with some cheap, fake, TB6006 drivers from China. Code is posted below. I didn't change anything in the code, but I switched over to gecko 203V drivers and switched from my breadboard to a proper PCB. The LCD is working properly, but there is no motion. The motors are NEMA 23 bipolar stepper motors and I am using a 70.5k ohm set resistor. Does anyone else here have experience using Gecko Drivers (specifically) that they can lend to me? Any help is much appreciated. I do not want to have to disassemble my setup and hello world each part again, but I will if I absolutely have to.
#include <AccelStepper.h>
#include <LiquidCrystal.h>
#include <StateMachine.h>
//LCD Screen Setup
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned int lcdStatus;
bool advanceHasRun = false;
int reverseHasRun = false;
int idleingHasRun = false;
//motor decleration
AccelStepper sprayer(1, 35, 33); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 . ////37 now 33, 39 now 35
AccelStepper table1(1, 43, 41); // 1, pulse pin, direction pin
AccelStepper table2(1, 47, 45); // 1, pulse pin, direction pin
/////////////////////////start button
int laminateButton = 52;
int laminateButtonstate = 0;
/////////////////////////boolean switches
bool sprayerOff = true;
bool tableOff = true;
bool tableHomeOff = true;
bool sprayHomeOff = true;
bool advance = false;
bool reverse = false;
/////////////////////////table1 manual control
int forwardButton = 49;
int reverseButton = 51;
int forwardButtonStatus = 0;
int reverseButtonStatus = 0;
/////////////////////////reset buttons
int sprayerResetButton = 50;
int tableResetButton = 53;
int sprayerResetStatus = 0;
int tableResetStatus = 0;
//////////////////////////table tuning
int tableSpeed = 1000; //table overall speed // previously set to 900
int tableAccel = 5000; //changed from 1000 to 5000 to get more table speed
int spraySpeed = 1000; //sprayer overall speed
int sprayAccel = 2000; //accelleration rate . //previously set to 10000
//////////////////////////Time based longs and ints
long previousMillis = 0;
long sprayHomeTime = 1000;
long tableHomeTime = 80000;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// SETUP ///////////////////////////////////////////////////////////////
void setup() {
//Initalize LCD screen
//first set the LCD's number of colums and rows:
lcd.begin(16, 2);
Serial.begin(9600);
startUpScreen();
//////////////////////////////////////INITALIZE STEPPERS////////////////////////////////////
sprayer.setAcceleration(sprayAccel);
sprayer.setMaxSpeed(spraySpeed);
sprayer.moveTo(1800); //sets limit on reverse distance
table1.setMaxSpeed(tableSpeed);
table1.setAcceleration(tableAccel);
table2.setMaxSpeed(tableSpeed);
table2.setAcceleration(tableAccel);
pinMode (laminateButton, INPUT);
pinMode (forwardButton, INPUT);
pinMode (reverseButton, INPUT);
pinMode (sprayerResetButton, INPUT);
pinMode (tableResetButton, INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// LOOP ///////////////////////////////////////////////////////////////
void loop() {
/////////////////////////// MANUAL CONTROL ////////////////////////////
//check status of forward button, if high, advance
forwardButtonStatus = digitalRead(forwardButton);
if (forwardButtonStatus == HIGH) {
advance = true;
while (advance == true){
table1Advance();
table2Advance();
if (advanceHasRun == false){
advanceScreen();
}
}
}
//check status of reverse button, if high, reverse
reverseButtonStatus = digitalRead(reverseButton);
if (reverseButtonStatus == HIGH) {
reverse = true;
while (reverse == true){
table1Reverse();
table2Reverse();
if (reverseHasRun == false){
reverseScreen();
}
}
}
/////////////////////////////// RESET ////////////////////////////////
tableResetStatus = digitalRead(tableResetButton);
if (tableResetStatus == HIGH) {
tableHomeOff = !tableHomeOff;
resetScreen();
}
while (tableHomeOff == false){
tableHome();
}
//send the sprayer to the starting position if right left button is pressed
sprayerResetStatus = digitalRead(sprayerResetButton);
if (sprayerResetStatus == HIGH) {
sprayHomeOff = !sprayHomeOff;
}
while (sprayHomeOff == false) {
sprayHome();
}
//////////////////////// START SEQUENCE /////////////////////////////////
laminateButtonstate = digitalRead(laminateButton);
if (laminateButtonstate == HIGH) {
tableOff = false;
sprayerOff = false;
laminationScreen();
}
while (sprayerOff == false and tableOff == false) {
spray();
driveTable();
}
if (forwardButtonStatus == LOW and reverseButtonStatus == LOW and laminateButtonstate == LOW and tableResetStatus == LOW){
idleScreen();
//rebootORreset();
}
}
//advance function, table one
void table1Advance() {
table1.setSpeed(10000);
table1.run();
advance = false;
}
//reverse function, table one
void table1Reverse() {
table1.setSpeed(-10000);
table1.run();
reverse = false;
}
//advance function, table two
void table2Advance() {
table2.setSpeed(10000);
table2.run();
}
//reverse function, table two
void table2Reverse() {
table2.setSpeed(-10000);
table2.run();
}
int t2RewindDistance = -7200;
int t1RewindDistance = -8200; ////////-25900;
int sprayHomeDistance = -500;
void tableHome() {
//unsigned long currentTableResetTime = millis();
table1.setMaxSpeed(tableSpeed);
table2.setMaxSpeed(tableSpeed);
table1.setAcceleration(tableAccel);
table2.setAcceleration(tableAccel);
table1.moveTo(t2RewindDistance); //driver set to 8 microsteps // previously set to -24500
table2.moveTo(t1RewindDistance); //driverr set to 8 microsteps
table1.run();
table2.run();
if (table1.distanceToGo() == 0){
Serial.println(F("reset complete, material is now in its starting position"));
tableHomeOff = true;
} while (tableHomeOff == true){
pleaseRebootScreen();
}
}
void sprayHome(){
sprayer.setMaxSpeed(spraySpeed);
sprayer.moveTo(sprayHomeDistance);
sprayer.run();
Serial.println(F("sprayReset is working"));
sprayHomeOff = true;
}
unsigned long sprayTime = 43900; // changed from 45000 to 30000 to see if table movement distance is affected, it shortened the overall movement by roughly three feet, readjusted to 43000, very close, again to 53900
unsigned long tableTime = 80000; // change from 90000 to 80000 did not affect distance of table movement
unsigned long sprayDelay = 4000; //delay sprayer until table has correctly positioned materials // CHANGED TO 6000 from 7000
/////////////////build out idle state ()
int idleSpeed = 0;
int idleAccel = 0;
int idleDistance = 0;
//function that controls sprayer in "start" state
void spray() {
unsigned long currentSprayMillis = millis();
if (currentSprayMillis - previousMillis > sprayDelay) {
sprayer.setMaxSpeed(spraySpeed);
sprayer.setAcceleration(sprayAccel);
sprayer.run();
//Serial.println("sprayer is running"); //program responded here, motors slowed down immediately
if (sprayer.distanceToGo() == 0){
sprayer.moveTo(-sprayer.currentPosition());
sprayer.run();
Serial.println(F("sprayer is running")); //program responded here, did not slow motors
}
if (currentSprayMillis - previousMillis > sprayTime + sprayDelay) { //deleted "+ spray delay) from end of perentheses
sprayerOff = true;
Serial.println(F("sprayer is off"));
Serial.println(F("Lamination Complete"));
previousMillis = currentSprayMillis;
}
}
}
//function that controls table in "start" state
void driveTable() {
table1.setMaxSpeed(tableSpeed);
table1.setAcceleration(tableAccel);
table1.moveTo(20000); // driver set to 8 microsteps //previously set to 16500 //table1.moveTo(33000); // driver set to 4 microsteps
table2.setMaxSpeed(tableSpeed);
table2.setAcceleration(tableAccel);
table2.moveTo(27000); //driverr set to 8 microsteps // previously 24700 //table2.moveTo(48000); // driver set to 4 microsteps
table1.run();
table2.run();
if (table1.distanceToGo() == 0 and table2.distanceToGo() == 0){
rebootORreset();
//idleScreen();
tableOff = true;
}
}
void idleScreen(){
//lcd.clear();
lcd.print(F("Status:"));
lcd.setCursor(0, 2);
lcd.print(F("Ready to proceed"));
}
void rebootORreset(){
lcd.clear();
lcd.print(F("Reboot b4 next"));
lcd.setCursor(0, 2);
lcd.print(F("run, or reset"));
}
void pleaseRebootScreen(){
lcd.print(F("Status:"));
lcd.setCursor(0, 2);
lcd.print(F("Press Reboot! "));
}
void startUpScreen(){
lcd.print(F("SPRAY TABLE V2.0"));
lcd.setCursor(0, 2);
lcd.print(F("INITALIZING..."));
Serial.println(F("Startup complete"));
delay(500);
idleScreen();
Serial.println(F("Idling, ready to proceed"));
}
void advanceScreen(){
lcd.print(F("STATUS: "));
lcd.setCursor(0, 2);
lcd.print(F("Advancing "));
advanceHasRun = true;
reverseHasRun = false;
Serial.println(F("advancing"));
}
void reverseScreen(){
lcd.clear();
lcd.print(F("STATUS: "));
lcd.setCursor(0, 2);
lcd.print(F("Reversing "));
reverseHasRun = true;
advanceHasRun = false;
Serial.println(F("reversing"));
}
void laminationScreen(){
lcd.print(F("Status:"));
lcd.setCursor(0,2);
lcd.print(F("Laminating "));
Serial.println(F("Laminating"));
}
void resetScreen(){
lcd.clear();
lcd.print(F("Status:"));
lcd.setCursor(0, 2);
lcd.print(F("Table Resetting"));
Serial.println(F("Table resetting"));
}
Code summary :
manual advance/reverse - two momentary pushbuttons to adjust the position of the material.
laminate functions - driveTable() and spray() happen simultaneously to pull a sheet through the machine and spray the material as it passes under the spray gantry.
reset function - brings the material back to the starting position if the spray is insufficient.





