Hi Wildbill yes sorry here is my code, Mach 3 will send an integer to Arduino each time it encounters the M6 G-code. But with the way mach3 communicates I am going to integrate some excellent code for modbus posted by Zafar on the mach3 forums. But before I do this it is essential to remove the Delay() functions and replace them with something more suitable.
// **********************************************
// * *
// * Automatic, 12-Tool Toolchanger *
// * 2022-01-10 *
// * Christiaan von Stade *
// * *
// **********************************************
//Include the Stepper library
#include <AccelStepper.h>
//Include the Button library
#include <ezButton.h>
//Include the Seria1 library
#include <SoftwareSerial.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
String readString;
SoftwareSerial Bluetooth(50, 49); // RX, TX
ezButton button(48); // create ezButton object that attach to pin 48;
int loopState = LOOP_STATE_STOPPED;
int Tool[]={1,2,3,4,5,6,7,8,9,10,11,12}; //An array to store the amount of tools
int toolCount = 12;
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;
const int solenoidPin = 53; //This is the solenoid output pin
const int home_switch = 52; // Pin 52 connected to Homing Switch (Sensor)
float counter = 0;
float CalibrationValue = 1.006927083; // 1600 steps per revolution / 12 = 133.3333333 + 60 steps to pass the pawl = 193.33/192 = 1.006927083 with 0 included in the first run
static int TurretPosition = 1;
int delayTime = 1; // decrease the delay to increase the speed
static int Input = 1; //Initial value for the bluetooth received or serial monitor
int StepsBack = 70;
AccelStepper x_stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup()
{
Serial.begin(9600);
Bluetooth.begin(9600);
Bluetooth.println("Choose between 1 to 12 for a tool position");
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
pinMode(solenoidPin, OUTPUT);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
//delay(1000);
while (!digitalRead(home_switch)==LOW) { // Do this until the switch is activated
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
}
digitalWrite(dirPin, LOW);
for (int lp=0;lp<StepsBack;lp++)
{
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
}
delay(2000);
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop()
{
ReceiveData();
BluetoothInput();
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
if (loopState == LOOP_STATE_STOPPED)
loopState = LOOP_STATE_STARTED;
else // if(loopState == LOOP_STATE_STARTED)
loopState = LOOP_STATE_STOPPED;
}
if (counter>193.2){
loopState = LOOP_STATE_STOPPED;
counter = 0;
Input = 0;
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
}
if (loopState == LOOP_STATE_STARTED) {
if ((solenoidPin) != HIGH)
{
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
}
else
{
}
while (counter<193.2) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
counter=counter+CalibrationValue;
}
if (counter>193.2){
digitalWrite(dirPin, LOW);
for (int lp=0;lp<StepsBack;lp++)
{
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
}
delay(1000);
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
Input = 0;
Serial.print("Turret Position ");
Serial.println(TurretPosition);
if (TurretPosition != 12)
{
TurretPosition = TurretPosition + 1;
}
else
{
TurretPosition = 1;
}
}
}
if (Input >= 1 && Input < 13)
{
ToolChange();
Input == 0;
}
if (Input == 0)
{
Serial.print("Turret Position ");
Serial.println(TurretPosition);
}
if (Input < 0 || Input > 12)
{
Serial.println("No such tool - Please choose between 1 to 12");
}
}
void ReceiveData() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
//int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
Input = atoi(carray);
readString="";
}
}
void BluetoothInput() {
while (Bluetooth.available()) {
delay(1);
if (Bluetooth.available() >0) {
char c = Bluetooth.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
//int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
Input = atoi(carray);
if (Input >= 1 && Input < 13)
{
Bluetooth.print("Turret Position ");
Bluetooth.println(Input);
}
readString="";
}
}
void ToolChange(){
Serial.print("Input ");
Serial.println(Input);
Serial.print("Turret Position ");
Serial.println(TurretPosition);
for (int thisTool = 0 ; thisTool < toolCount ; thisTool++)
{
if (Tool[thisTool] = Input)
{
while (TurretPosition != Input)
{
counter = 0;
//Serial.print("Input ");
//Serial.println(Input);
//Serial.print("Turret Position ");
//Serial.println(TurretPosition);
if ((solenoidPin) != HIGH)
{
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
}
else
{
//do nothing
}
while (counter<193.2) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
counter=counter+CalibrationValue;
}
if (counter>193.2){
digitalWrite(dirPin, LOW);
for (int lp=0;lp<StepsBack;lp++)
{
digitalWrite(stepPin, LOW);
delay(delayTime);
digitalWrite(stepPin, HIGH);
delay(delayTime);
}
delay(1000);
digitalWrite(solenoidPin, LOW); //Switch Solenoid ON
if (TurretPosition != 12)
{
TurretPosition = TurretPosition + 1;
}
else
{
TurretPosition = 1;
}
}
}
}
}
}