I have a 2 year old XIAO One of three that I bought from Amazon, I am now just trying to use them now. Is there a update for the chip boot?, I am experiencing problems with the SPI communication, the SCK seems to quit working etc… My project consists of 4 SPI devices, 5 I2C devices and level shifters for both of those comms.
that sounds like alot of IO for a XIAO… which model… I am assuming SAMD based on the age… what program? arduino IDE?
Yeah, is a SAMD, well I figured that with a large amount and good speed it would be a fit, it’s a good thing that I bread boarded it first. The I2C works okay, but the SPI is terrible, it works and then it doesn’t. I need to use three LS7366 chips and a ethernet shield working on SPI. The I2C would be running two MCP23017 chips and three PCF8574 chips. This all would be running a 3 axis wood CNC Homebrew machine.
I am going to try a STM board next to see what it can and can’t do. I’ve got the time to explore. I use Arduino software, but now I will be learning the Cube.
If you want too you can post your code on gethub or somewhere , or here and provide some pictures and we can try to see if we can help sort it out with you
I gave one more try, I read the SPI WIKI and realized it is not standardized, so I changed the coding as related to the chip selects, now I need to speed up the I2C so that the mpc23017 chips respond quicker. The arduinos that I have used can’t go much over 400khz due to register size. I hope that the SAMD21 uses a larger register so the speed Can be uppast 400khz. The mpc23017 have the three chip selects for the encoders (LS7366). Anyway I got the Ethernet to finally function, which is the heart of this project. The Ethernet communicates all of the positioning from my computer using UDP.
Consider upgrading to the ESP32 XIAO they have wifi capability and also consider these products for the XIAO Ecosystem
XIAO Sale is still on!
code for X, Y, and Z axis - couldn’t post all the code
This code is running on 3 Nano’s, one Nano for each axis, Ethernet is seperate from the Nano’s operation. position communication is relayed to the Nano’s by software serial. I was hoping to use the XIAO to combine all functions. Since I have gotten the Ethernet shield to function, I have found that the string the reads the package buffer never get cleared, just another problem to resolve.
//XIAO M0 processor controlling 3-axis Machine processor 3.3v
//Level shifters for SPI, I2C, and software serial comms
//#include <pcf8574.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
// #include <SoftwareSerial.h>
//SoftwareSerial ssSerial(1, 0); // RX, TX
const int CS = D7; //Ethernet shield CS spi
int before = 0;
Adafruit_MCP23017 mcp1; // I2C
Adafruit_MCP23017 mcp2; // I2C
const int takeastepx = 4;
const int takeastepy = 14;
const int takeastepz = 9;
const int directionx = 6;
const int directiony = 15;
const int directionz = 8;
const int selEncoderx = 15;//was 15// Encoder LS7366 Chip x CS spi pin on MCP1
const int selEncodery = 13;//was 13// Encoder LS7366 Chip y CS spi pin on MCP1
const int selEncoderz = 11;//was 11// Encoder LS7366 Chip z CS spi pin on MCP1
const int axisXenable = 9;
const int axisYenable = 3;
const int axisZenable = 13;
const int enablePoscountx = 14;
const int enablePoscounty = 12;
const int enablePoscountz = 10;
const int step_ms1x = 8;
const int step_ms2x = 7;
const int step_ms3x = 5;
const int step_ms1y = 2;
const int step_ms2y = 1;
const int step_ms3y = 0;
const int step_ms1z = 12;
const int step_ms2z = 11;
const int step_ms3z = 10;
byte mac[] = {
0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF
};
IPAddress ip(192, 168, 15, 220);
unsigned int localPort = 51000;
char packetBuffer[12];
EthernetUDP Udp;
//PCF8574 safex(0x20);// I2C
//PCF8574 safey(0x21);// I2C
//PCF8574 safez(0x22);// I2C
String disc = "";
String diss = "";
int seq = 0;
int seqr = 0;
int Startx = 0;
int Starty = 0;
int Startz = 0;
int Refx = 0;
int Refy = 0;
int Refz = 0;
unsigned long cm = 0;
unsigned long pm = 0;
long iv = 15;
long cvx = 0; //X-axis Encoder Counts
long cvy = 0; //Y-axis Encoder Counts
long cvz = 0; //Z-axis Encoder Counts
long pvx = 0; //X-axis process variable Counts to position
long pvy = 0; //Y-axis process variable Counts to position
long pvz = 0; //Z-axis process variable Counts to position
float cpx = 0; //Recieved command for X-axis position
float cpy = 0; //Recieved command for Y-axis position
float cpz = 0; //Recieved command for Z-axis position
int xspare = 0;
int yspare = 0;
int zspare = 0;
int gohomex = 0;
int JogFwdx = 0;
int JogRevx = 0;
int Stopx = 0;
int exteotx = 0;
int reteotx = 0;
int xhome = 0;
int gohomey = 0;
int JogFwdy = 0;
int JogRevy = 0;
int Stopy = 0;
int exteoty = 0;
int reteoty = 0;
int yhome = 0;
int gohomez = 0;
int JogDwnz = 0;
int JogUpz = 0;
int Stopz = 0;
int exteotz = 0;
int reteotz = 0;
int zhome = 0;
int Tmrx = 0;
int Tmry = 0;
int Tmrz = 0;
int Nopz = 0;
int R1x = 100;
int R2x = 50;
int R3x = 10;
int R4x = 3;
int D1x = 1000;
int D2x = 750;
int W1x = 2;
int W2x = 4;
int R1y = 100;
int R2y = 50;
int R3y = 10;
int R4y = 3;
int D1y = 1000;
int D2y = 750;
int W1y = 2;
int W2y = 4;
int R1z = 100;
int R2z = 50;
int R3z = 10;
int R4z = 3;
int D1z = 1000;
int D2z = 750;
int D3z = 500;
int D4z = 250;
int D5z = 75;
int D6z = 35;
int D7z = 20;
int D8z = 10;
int W1z = 2;
int W2z = 4;
int R = 0;
int LL = 0;
int initEncoders = 0;
int RampTimez = 0;
void setup() {
pinMode (CS, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
delay (100);
Ethernet.init(CS); // pin 7 on XIAO 3.3v to level shifter to 5v
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Wire.begin();
Wire.setClock(400000);
mcp1.begin(3);
mcp2.begin(4);
mcp1.pinMode(selEncoderx, OUTPUT); //Encoder X Counter Chip select pin
mcp1.pinMode(selEncodery, OUTPUT); //Encoder Y Counter Chip select pin
mcp1.pinMode(selEncoderz, OUTPUT); //Encoder Z Counter Chip select pin
mcp1.pinMode(enablePoscountx, OUTPUT);// Enable X Encoder Counting
mcp1.pinMode(enablePoscounty, OUTPUT);// Enable Y Encoder Counting
mcp1.pinMode(enablePoscountz, OUTPUT);// Enable Z Encoder Counting
mcp1.pinMode(takeastepx, OUTPUT);// step pin X
mcp2.pinMode(takeastepy, OUTPUT);// step pin Y
mcp2.pinMode(takeastepz, OUTPUT);// step pin Z
mcp1.pinMode(directionx, OUTPUT);// stepperX direction
mcp2.pinMode(directiony, OUTPUT);// stepperY direction
mcp2.pinMode(directionz, OUTPUT);// stepperZ direction
mcp1.pinMode(axisXenable, OUTPUT);// Enable stepper X movement
mcp1.pinMode(axisYenable, OUTPUT);// Enable stepper Y movement
mcp2.pinMode(axisZenable, OUTPUT);// Enable stepper Z movement
mcp1.pinMode(step_ms1x, OUTPUT);// stepper step select ms1x
mcp1.pinMode(step_ms2x, OUTPUT);// stepper step select ms2x
mcp1.pinMode(step_ms3x, OUTPUT);// stepper step select ms3x
mcp1.pinMode(step_ms1y, OUTPUT);// stepper step select ms1y
mcp1.pinMode(step_ms2y, OUTPUT);// stepper step select ms2y
mcp1.pinMode(step_ms3y, OUTPUT);// stepper step select ms3y
mcp2.pinMode(step_ms1z, OUTPUT);// stepper step select ms1z
mcp2.pinMode(step_ms2z, OUTPUT);// stepper step select ms2z
mcp2.pinMode(step_ms3z, OUTPUT);// stepper step select ms3z
/*
pinMode(safex,6,INPUT_PULLUP); //Ref PB x
pinMode(safey,6,INPUT_PULLUP); //Ref PB y
pinMode(safez,6,INPUT_PULLUP); //Ref PB z
pinMode(safex,5,INPUT_PULLUP); //jogfwd_pbX
pinMode(safey,5,INPUT_PULLUP); //jogfwd_pbY
pinMode(safez,5,INPUT_PULLUP); //jogdwn_pbZ
pinMode(safex,7,INPUT_PULLUP);//jogrev_pbX
pinMode(safey,7,INPUT_PULLUP);//jogrev_pbY
pinMode(safez,7,INPUT_PULLUP);//jogup_pbZ
pinMode(safex,2,INPUT_PULLUP);//ref_swX
pinMode(safey,2,INPUT_PULLUP);//ref_swY
pinMode(safez,2,INPUT_PULLUP);//ref_swZ
pinMode(safex,4,INPUT_PULLUP);//stop_pbX
pinMode(safey,4,INPUT_PULLUP);//stop_pbY
pinMode(safez,4,INPUT_PULLUP);//stop_pbZ
pinMode(safex,0,INPUT_PULLUP);//eot_ext_swX
pinMode(safey,0,INPUT_PULLUP);//eot_ext_swY
pinMode(safez,0,INPUT_PULLUP);//eot_ext_swZ
pinMode(safex,1,INPUT_PULLUP);//eot_ret_swX
pinMode(safey,1,INPUT_PULLUP);//eot_ret_swY
pinMode(safez,1,INPUT_PULLUP);//eot_ret_swZ
pinMode(safex,3,INPUT_PULLUP);//spare X
pinMode(safey,3,INPUT_PULLUP);//spare Y
pinMode(safez,3,INPUT_PULLUP);//spare Z
*/
mcp1.digitalWrite(step_ms1x, LOW); //was 8
mcp1.digitalWrite(step_ms1y, LOW); //was 2
mcp2.digitalWrite(step_ms1z, LOW); //was 12
mcp1.digitalWrite(step_ms2x, LOW); //was 7
mcp1.digitalWrite(step_ms2y, LOW); //was 1
mcp2.digitalWrite(step_ms2z, LOW); //was 11
mcp1.digitalWrite(step_ms3x, LOW); //was 5
mcp1.digitalWrite(step_ms3y, LOW); //was 0
mcp2.digitalWrite(step_ms3z, LOW); //was 10
mcp1.digitalWrite(selEncoderx, HIGH); //was 15
mcp1.digitalWrite(selEncodery, HIGH); //was 13
mcp1.digitalWrite(selEncoderz, HIGH); //was 11
Serial.begin(9600);
//ssSerial.begin(9600);
mcp1.digitalWrite(takeastepx, LOW);// takeastepx
mcp2.digitalWrite(takeastepy, LOW);// takeastepy
mcp2.digitalWrite(takeastepz, LOW);//takeastepz
}
void loop() {
if (initEncoders == 1 || initEncoders == 3 || initEncoders == 5) {
before = 0;
}
if (initEncoders == 2 && before == 0) {
initEncoderX();
before = 1;
}
if (initEncoders == 4 && before == 0) {
initEncoderY();
before = 1;
}
if (initEncoders == 6 && before == 0) {
initEncoderZ();
before = 1;
}
Timer();
if (seq == 1) {
recUdp();
}
if (seq == 3) {
mcp1.digitalWrite(selEncoderx, LOW);
readEncoderX();
if (R == 0) {
Serial.println(pvx);
R = 1;
}
}
if (seq == 5) {
mcp1.digitalWrite(selEncodery, LOW);
readEncoderY();
}
if (seq == 7) {
mcp1.digitalWrite(selEncoderz, LOW);
readEncoderZ();
}
if (seqr == 0 && seq >= 8) {
seq = 0;
}
if (seq == 10 && seqr == 4) {
Serial.println(seq);
clearEncoderCountX();
seqr = 0;
}
if (seq == 12 && seqr == 5) {
clearEncoderCountY();
seqr = 0;
}
if (seq == 14 && seqr == 6) {
clearEncoderCountZ();
seqr = 0;
}
if (seq > 14 ) {
seq = 0;
}
if (Serial.available() > 0 ) {
String sew;
sew = Serial.readString();
if (sew.endsWith("X")) {
seqr = sew.toInt();
Serial.println(sew);
sew = "";
initEncoders = 0;
}
if (sew.endsWith("T")) {
Serial.println(pvx);
}
}
// determine position movement for X axis
if (cvx > cpx) {
pvx = (cvx - cpx);
}
if (cpx > cvx) {
pvx = (cpx - cvx);
}
//getioX();
//start of X axis
if (Tmrx < 20 && Stopx == 1) {
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
//pvx = process variable, cpx = Command Position
if (pvx > W2x && Startx == 1 && Refx == 0) {
Startx = 2;
}
if (Refx == 1) {
Reffx(); //remote Ref == 5 comes from remote
}
if (gohomex == 0 && Refx == 0 || Refx == 5) {
mcp1.digitalWrite(directionx, LOW); //manual or remote
Refx = 1;
Startx = 20;
cpx = -1;
}
if (Stopx == 0 && exteotx == 1 && reteotx == 1) {
Startx = 2;
mcp1.digitalWrite(takeastepx, HIGH);
}
if (Stopx == 0) {
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (exteotx == 0) {
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (reteotx == 0) {
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (JogFwdx == 0) {
Startx = 30;
}
if (JogRevx == 0) {
Startx = 40;
}
if (exteotx == 1 && reteotx == 1) {
if (Startx == 2 && Refx == 0) {
positionX();
}
if (Startx == 35 || Startx == 45 || Startx == 30 || Startx == 40) {
jogX();
}
}
//start of Y axis
// determine position movement for Y axis
if (cvy > cpy) {
pvy = (cvy - cpy);
}
if (cpy > cvy) {
pvy = (cpy - cvy);
}
//getioY();
if (Tmry < 20 && Stopy == 1) {
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
}
//pvy = process variable, cpy = Command Position
if (pvy > W2y && Starty == 1 && Refy == 0) {
Starty = 2;
}
if (Refy == 1) {
Reffy(); //remote Ref == 5 comes from remote
}
if (gohomey == 0 && Refy == 0 || Refy == 5) {
mcp2.digitalWrite(directiony, LOW); //manual or remote
Refy = 1;
Starty = 20;
cpy = -1;
}
if (Stopy == 0 && exteoty == 1 && reteoty == 1) {
Starty = 2;
mcp2.digitalWrite(takeastepy, HIGH);
}
if (Stopy == 0) {
Starty = 0;
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
}
if (exteoty == 0) {
Starty = 0;
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
}
if (reteoty == 0) {
Starty = 0;
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
}
if (JogFwdy == 0) {
Starty = 30;
}
if (JogRevy == 0) {
Starty = 40;
}
if (exteoty == 1 && reteoty == 1) {
if (Starty == 2 && Refy == 0) {
positionY();
}
if (Starty == 35 || Starty == 45 || Starty == 30 || Starty == 40) {
jogY();
}
}
//start of Z axis
// determine position movement for Z axis
if (cvz > cpz) {
pvz = (cvz - cpz);
}
if (cpz > cvz) {
pvz = (cpz - cvz);
}
//getioZ();
if (Tmrz < 20 && Stopz == 1) {
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
}
//pvz = process variable, cpz = Command Position
if (pvz > W2z && Startz == 1 && Refz == 0) {
Startz = 2;
}
if (Refz == 1) {
Reffz(); //remote Refz == 5 comes from remote
}
if (gohomez == 0 && Refz == 0 || Refz == 5) {
mcp2.digitalWrite(directionz, LOW); //manual or remote
Refz = 1;
Startz = 20;
cpz = -1;
}
if (Stopz == 0 && exteotz == 1 && reteotz == 1) {
Startz = 2;
mcp2.digitalWrite(takeastepz, HIGH);
}
if (Stopz == 0) {
Startz = 0;
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
}
if (exteotz == 0) {
Startz = 0;
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
}
if (reteotz == 0) {
Startz = 0;
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
}
if (JogDwnz == 0) {
Startz = 30;
}
if (JogUpz == 0) {
Startz = 40;
}
if (exteotz == 1 && reteotz == 1) {
if (Startz == 2 && Refz == 0) {
positionZ();
}
if (Startz == 35 || Startz == 45 || Startz == 30 || Startz == 40) {
jogZ();
}
}
}
void Timer() {
cm = millis(); // cm = current Millis, pm = previous Millis, iv = intervalTime
if ((cm - pm) > iv) {
pm = cm;
if (initEncoders < 4000) {
++initEncoders;
}
if (RampTimez < 5000) {
++RampTimez;
}
if (Tmrx < 10000) {
++Tmrx;
}
if (Tmry < 10000) {
++Tmry;
}
if (Tmrz < 10000) {
++Tmrz;
}
if (seq < 100) {
++seq;
}
}
}
void recUdp() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Udp.read(packetBuffer, 12);
disc = packetBuffer;
disc.trim();
LL = disc.length();
Serial.println(disc);
if (disc.endsWith("R1X")) {
R1x = disc.toInt();
disc = "";
diss = "R1X";
TransUdp();
}
if (disc.endsWith("R2X")) {
R2x = disc.toInt();
disc = "";
diss = "R2X";
TransUdp();
}
if (disc.endsWith("R3X")) {
R3x = disc.toInt();
disc = "";
diss = "R3X";
TransUdp();
}
if (disc.endsWith("R4X")) {
R4x = disc.toInt();
disc = "";
diss = "R4X";
TransUdp();
}
if (disc.endsWith("D1X")) {
D1x = disc.toInt();
disc = "";
diss = "D1X";
TransUdp();
}
if (disc.endsWith("D2X")) {
D2x = disc.toInt();
disc = "";
diss = "D2X";
TransUdp();
}
if (disc.endsWith("W1X")) {
W1x = disc.toInt();
disc = "";
diss = "W1X";
TransUdp();
}
if (disc.endsWith("W2X")) {
W2x = disc.toInt();
disc = "";
diss = "W2X";
TransUdp();
}
if (disc.endsWith("20X")) {
disc = "";
diss = String(cvx) + "EX";
TransUdp();
} // Get Encoder Position
if (disc.endsWith("22X")) {
diss = disc;
disc = "";
Startx = 2;
TransUdp();
}//Run
if (disc.endsWith("21X")) {
diss = disc;
disc = "";
Startx = 1;
TransUdp();
}//Auto
if (disc.endsWith("32X")) {
Startx = 35;
mcp1.digitalWrite(directionx, LOW);
diss = "32X";
disc = "";
TransUdp();
}//Jog fwd
if (disc.endsWith("42X")) {
Startx = 45;
mcp1.digitalWrite(directionx, HIGH);
diss = "42X";
disc = "";
TransUdp();
}//Jog rev
if (disc.endsWith("52X")) {
Startx = 0;
Refx = 5;
mcp1.digitalWrite(directionx, LOW);
diss = "52X";
disc = "";
TransUdp();
}//Reference
if (disc.endsWith("11X")) {
diss = "11X";
disc = "";
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
TransUdp();
}//Stop
if (disc.endsWith("A")) {
cpx = disc.toFloat();
disc.remove(0, LL);
diss = String(cpx) + "A";
TransUdp();
diss.remove(0, LL);
}
if (disc.endsWith("T")) {
cpx = disc.toFloat();
initEncoders = 0;
R = 0;
//Tester();
disc.remove(0, LL);
diss = String(cpx);
TransUdp();
diss.remove(0, 6);
}
if (disc.endsWith("R1Y")) {
R1y = disc.toInt();
disc = "";
diss = "R1Y";
TransUdp();
}
if (disc.endsWith("R2Y")) {
R2y = disc.toInt();
disc = "";
diss = "R2Y";
TransUdp();
}
if (disc.endsWith("R3Y")) {
R3y = disc.toInt();
disc = "";
diss = "R3Y";
TransUdp();
}
if (disc.endsWith("R4Y")) {
R4y = disc.toInt();
disc = "";
diss = "R4Y";
TransUdp();
}
if (disc.endsWith("D1Y")) {
D1y = disc.toInt();
disc = "";
diss = "D1Y";
TransUdp();
}
if (disc.endsWith("D2Y")) {
D2y = disc.toInt();
disc = "";
diss = "D2Y";
TransUdp();
}
if (disc.endsWith("W1Y")) {
W1y = disc.toInt();
disc = "";
diss = "W1Y";
TransUdp();
}
if (disc.endsWith("W2Y")) {
W2y = disc.toInt();
disc = "";
diss = "W2Y";
TransUdp();
}
if (disc.endsWith("20Y")) {
diss = String(cvy) + "EY"; // Get Encoder Position
disc = "";
TransUdp();
}
if (disc.endsWith("22Y")) {
diss = disc; //Run
disc = "";
Starty = 2;
TransUdp();
}
if (disc.endsWith("21Y")) {
diss = disc; //Auto
disc = "";
Starty = 1;
TransUdp();
}
if (disc.endsWith("32Y")) {
Starty = 35; //Jog fwd
mcp2.digitalWrite(directiony, LOW);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("42Y")) {
Starty = 45; //Jog rev
mcp2.digitalWrite(directiony, HIGH);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("52Y")) {
Starty = 0; //Reference
Refy = 5;
mcp2.digitalWrite(directiony, LOW);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("11Y")) {
diss = disc; //Stop
disc = "";
Starty = 0;
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
TransUdp();
}
if (disc.endsWith("B")) {
cpy = disc.toFloat();
disc = "";
diss = String(cpy) + "B";
TransUdp();
diss = "";
}
if (disc.endsWith("R1Z")) {
R1z = disc.toInt();
disc = "";
diss = "R1Z";
TransUdp();
}
if (disc.endsWith("R2Z")) {
R2z = disc.toInt();
disc = "";
diss = "R2Z";
TransUdp();
}
if (disc.endsWith("R3Z")) {
R3z = disc.toInt();
disc = "";
diss = "R3Z";
TransUdp();
}
if (disc.endsWith("R4Z")) {
R4z = disc.toInt();
disc = "";
diss = "R4Z";
TransUdp();
}
if (disc.endsWith("D1Z")) {
D1z = disc.toInt();
disc = "";
diss = "D1Z";
TransUdp();
}
if (disc.endsWith("D2Z")) {
D2z = disc.toInt();
disc = "";
diss = "D2Z";
TransUdp();
}
if (disc.endsWith("D3Z")) {
D3z = disc.toInt();
disc = "";
diss = "D3Z";
TransUdp();
}
if (disc.endsWith("D4Z")) {
D4z = disc.toInt();
disc = "";
diss = "D4Z";
TransUdp();
}
if (disc.endsWith("W1Z")) {
W1z = disc.toInt();
disc = "";
diss = "W1Z";
TransUdp();
}
if (disc.endsWith("W2Z")) {
W2z = disc.toInt();
disc = "";
diss = "W2Z";
TransUdp();
}
if (disc.endsWith("N1Z")) {
disc = "";
Nopz = 1;
diss = "N1Z";
TransUdp();
}
if (disc.endsWith("N2Z")) {
disc = "";
Nopz = 0;
diss = "N2Z";
TransUdp();
}
if (disc.endsWith("20Z")) {
diss = String(cvz) + "EZ"; // Get Encoder Position
disc = "";
TransUdp();
}
if (disc.endsWith("22Z")) {
diss = disc; //Run
disc = "";
Startz = 2;
TransUdp();
}
if (disc.endsWith("21Z")) {
diss = disc; //Auto
disc = "";
Startz = 1;
TransUdp();
}
if (disc.endsWith("32Z")) {
Startz = 35; //Jog fwd
mcp2.digitalWrite(directionz, LOW);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("42Z")) {
Startz = 45; //Jog rev
mcp2.digitalWrite(directionz, HIGH);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("52Z")) {
Startz = 0; //Reference
Refz = 5;
mcp2.digitalWrite(directionz, LOW);
diss = disc;
disc = "";
TransUdp();
}
if (disc.endsWith("11Z")) {
diss = disc; //Stop
disc = "";
Starty = 0;
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
TransUdp();
}
if (disc.endsWith("C")) {
cpz = disc.toFloat();
disc = "";
diss = String(cpz) + "C";
TransUdp();
diss = "";
}
if (disc.length() > 12) {
disc = "";
}
}
}
void TransUdp() {
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.println(diss);
Udp.endPacket();
diss = " ";
}
void positionX () {
if (cpx < cvx && Startx < 20) {
mcp1.digitalWrite(directionx, LOW); // Reverse was High, changed encoder wiring
}
if (cpx > cvx && Startx < 20) {
mcp1.digitalWrite(directionx, HIGH); // Forward was Low
}
// One Inch = 1024 pulses
if (Startx == 2) {
mcp1.digitalWrite(axisXenable, LOW);// Stepper A4988 enabled
mcp1.digitalWrite(takeastepx, HIGH);
}//start stepping
if (pvx >= R1x) {
mcp1.digitalWrite(step_ms1x, LOW);
mcp1.digitalWrite(step_ms2x, LOW);
mcp1.digitalWrite(step_ms3x, LOW);
} // full step
if (pvx < R1x && pvx >= R2x) {
mcp1.digitalWrite(step_ms1x, HIGH);
} // half Step
if (pvx < R2x && pvx >= R3x) {
mcp1.digitalWrite(step_ms2x, HIGH);
mcp1.digitalWrite(step_ms1x, LOW);
} // 1/4 step
if (pvx < R3x && pvx >= R4x) {
mcp1.digitalWrite(step_ms2x, HIGH);
mcp1.digitalWrite(step_ms1x, HIGH);
}// 1/8 step
if (pvx <= W1x && Startx == 2) {
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(step_ms1x, LOW);
mcp1.digitalWrite(step_ms2x, LOW);
mcp1.digitalWrite(step_ms3x, LOW);
diss = String(cvx) + "IX"; TransUdp(); // transmitt X position to computer
Startx = 1; // standby - was running but now enabled and standing by for next command
}
Tmrx = 0;
}
void positionY () {
if (cpy < cvy && Starty < 20) {
mcp2.digitalWrite(directiony, LOW); // Reverse was High, changed encoder wiring
}
if (cpy > cvy && Starty < 20) {
mcp2.digitalWrite(directiony, HIGH); // Forward was Low
}
// One Inch = 1524 pulses
if (Starty == 2) {
mcp1.digitalWrite(axisYenable, LOW);
mcp2.digitalWrite(takeastepy, HIGH);
}// Stepper A4988 enabled
if (pvy >= R1y) {
mcp1.digitalWrite(step_ms1y, LOW);
mcp1.digitalWrite(step_ms2y, LOW);
mcp1.digitalWrite(step_ms3y, LOW);
} // full step
if (pvy < R1y && pvy >= R2y) {
mcp1.digitalWrite(step_ms1y, HIGH);
} // half Step
if (pvy < R2y && pvy >= R3y) {
mcp1.digitalWrite(step_ms2y, HIGH);
mcp1.digitalWrite(step_ms3y, LOW);
} // 1/4 step
if (pvy < R3y && pvy >= R4y) {
mcp1.digitalWrite(step_ms2y, HIGH);
mcp1.digitalWrite(step_ms3y, HIGH);
}// 1/8 step
if (pvy <= W1y && Starty == 2) {
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(step_ms1y, LOW);
mcp1.digitalWrite(step_ms2y, LOW);
mcp1.digitalWrite(step_ms3y, LOW);
diss = String(cvy) + "IY"; TransUdp(); // transmitt Y position to computer
Starty = 1; // standby - was running but now enabled and standing by for next command
}
Tmry = 0;
}
void positionZ () {
if (cpz < cvz && Startz < 20) {
mcp2.digitalWrite(directionz, LOW); // Reverse was High, changed encoder wiring
}
if (cpz > cvz && Startz < 20) {
mcp2.digitalWrite(directionz, HIGH); // Forward was Low
}
// One Inch = 2512 pulses
if (Startz == 2) {
mcp2.digitalWrite(axisZenable, LOW);
mcp2.digitalWrite(takeastepz, HIGH);
}// Stepper A4988 enabled
if (pvz >= R1z) {
mcp2.digitalWrite(step_ms1z, LOW);
mcp2.digitalWrite(step_ms2z, LOW);
mcp2.digitalWrite(step_ms3z, LOW);
} // full step
if (pvz < R1z && pvz >= R2z) {
mcp2.digitalWrite(step_ms1z, HIGH);
} // half Step
if (pvz < R2z && pvz >= R3z) {
mcp2.digitalWrite(step_ms2z, HIGH);
mcp2.digitalWrite(step_ms1z, LOW);
} // 1/4 step
if (pvz < R3z && pvz >= R4z) {
mcp2.digitalWrite(step_ms2z, HIGH);
mcp2.digitalWrite(step_ms1z, HIGH);
}// 1/8 step
if (pvz <= W1z && Startz == 2) {
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(step_ms1z, LOW);
mcp2.digitalWrite(step_ms2z, LOW);
mcp2.digitalWrite(step_ms3z, LOW);
diss = String(cvz) + "IZ"; TransUdp(); // transmitt Z position to computer
Startz = 1; // standby - was running but now enabled and standing by for next command
}
Tmrz = 0;;
}
void readEncoderX() {
// Initialize temporary variables for SPI read
int countx_1, countx_2, countx_3, countx_4;
// Read encoder
digitalWrite(CS, HIGH);
mcp1.digitalWrite(selEncodery, HIGH);
mcp1.digitalWrite(selEncoderz, HIGH);
mcp1.digitalWrite(selEncoderx, LOW); // Begin SPI conversation
delayMicroseconds(100);
SPI.transfer(0x60); //was 60 // Request count
countx_1 = SPI.transfer(0x00); // Read highest order byte
countx_2 = SPI.transfer(0x00);
countx_3 = SPI.transfer(0x00);
countx_4 = SPI.transfer(0x00); // Read lowest order byte
delayMicroseconds(100);
mcp1.digitalWrite(selEncoderx, HIGH); // Terminate SPI conversation
delayMicroseconds(100);
// Calculate encoder count
cvx = (countx_1 << 8) + countx_2;
cvx = (cvx << 8) + countx_3;
cvx = (cvx << 8) + countx_4;
//Stepper Direction, cp = Command Position;cv = realtime encoder counts
}
void readEncoderY() {
// Initialize temporary variables for SPI read
int county_1, county_2, county_3, county_4;
// Read encoder
digitalWrite(CS, HIGH);
mcp1.digitalWrite(selEncoderz, HIGH);
mcp1.digitalWrite(selEncoderx, HIGH);
mcp1.digitalWrite(selEncodery, LOW); // Begin SPI conversation
SPI.transfer(0x60); //was 60 // Request count
county_1 = SPI.transfer(0x00); // Read highest order byte
county_2 = SPI.transfer(0x00);
county_3 = SPI.transfer(0x00);
county_4 = SPI.transfer(0x00); // Read lowest order byte
mcp1.digitalWrite(selEncodery, HIGH); // Terminate SPI conversation
// Calculate encoder count
cvy = (county_1 << 8) + county_2;
cvy = (cvy << 8) + county_3;
cvy = (cvy << 8) + county_4;
//Stepper Direction, CP = Command Position;CV = realtime encoder counts}
}
void readEncoderZ() {
// Initialize temporary variables for SPI read
int countz_1, countz_2, countz_3, countz_4;
// Read encoder
digitalWrite(CS, HIGH);
mcp1.digitalWrite(selEncodery, HIGH);
mcp1.digitalWrite(selEncoderx, HIGH);
mcp1.digitalWrite(selEncoderz, LOW); // Begin SPI conversation
SPI.transfer(0x60); //was 60 // Request count
countz_1 = SPI.transfer(0x00); // Read highest order byte
countz_2 = SPI.transfer(0x00);
countz_3 = SPI.transfer(0x00);
countz_4 = SPI.transfer(0x00); // Read lowest order byte
mcp1.digitalWrite(selEncoderz, HIGH); // Terminate SPI conversation
// Calculate encoder count
cvz = (countz_1 << 8) + countz_2;
cvz = (cvz << 8) + countz_3;
cvz = (cvz << 8) + countz_4;
//Stepper Direction, CP = Command Position;CV = realtime encoder counts}
}
void Reffx() {
if (Startx == 20) {
mcp1.digitalWrite(directionx, LOW);
mcp1.digitalWrite(axisXenable, LOW);
mcp1.digitalWrite(takeastepx, HIGH);
mcp1.digitalWrite(step_ms1x, LOW);
mcp1.digitalWrite(step_ms2x, LOW);
mcp1.digitalWrite(step_ms3x, LOW);
} // full step
if (Startx == 20 && xhome == 0) {
Startx = 21;
Tmrx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (Startx == 21 && xhome == 0 && Tmrx > 20) {
mcp1.digitalWrite(axisXenable, LOW);
mcp1.digitalWrite(directionx, HIGH);
mcp1.digitalWrite(step_ms2x, HIGH);
mcp1.digitalWrite(step_ms1x, HIGH); // 1/8 step
mcp1.digitalWrite(takeastepx, HIGH);
}
if (Startx == 21 && xhome == 1) {
mcp1.digitalWrite(takeastepx, LOW);
}
if (Startx == 21 && xhome == 1 && Tmrx > 20) {
Startx = 0;
//initEncoders();
if (seqr == 0) {
seqr = 4;
}
diss = "RX"; TransUdp();
mcp1.digitalWrite(step_ms1x, LOW);
mcp1.digitalWrite(step_ms2x, LOW);
mcp1.digitalWrite(step_ms3x, LOW);
mcp1.digitalWrite(directionx, HIGH);
}
}
void Reffy() {
if (Starty == 20) {
mcp2.digitalWrite(directiony, LOW);
mcp1.digitalWrite(axisYenable, LOW);
mcp2.digitalWrite(takeastepy, HIGH);
mcp1.digitalWrite(step_ms1y, LOW);
mcp1.digitalWrite(step_ms2y, LOW);
mcp1.digitalWrite(step_ms3y, LOW);
} // full step
if (Starty == 20 && yhome == 0) {
Starty = 21;
Tmry = 0;
mcp2.digitalWrite(takeastepy, LOW);
mcp1.digitalWrite(axisYenable, HIGH);
}
if (Starty == 21 && yhome == 0 && Tmry > 20) {
mcp1.digitalWrite(axisYenable, LOW);
mcp2.digitalWrite(directiony, HIGH);
mcp1.digitalWrite(step_ms2y, HIGH);
mcp1.digitalWrite(step_ms1y, HIGH); // 1/8 step
mcp2.digitalWrite(takeastepy, HIGH);
}
if (Starty == 21 && yhome == 1) {
mcp2.digitalWrite(takeastepy, LOW);
}
if (Starty == 21 && yhome == 1 && Tmry > 20) {
Starty = 0;
//initEncoders();
if (seqr == 0) {
seqr = 5;
}
diss = "RY"; TransUdp();
mcp1.digitalWrite(step_ms1y, LOW);
mcp1.digitalWrite(step_ms2y, LOW);
mcp1.digitalWrite(step_ms3y, LOW);
mcp2.digitalWrite(directiony, HIGH);
}
}
void Reffz() {
if (Startz == 20) {
mcp2.digitalWrite(directionz, LOW);
mcp2.digitalWrite(axisZenable, LOW);
mcp2.digitalWrite(takeastepz, HIGH);
mcp2.digitalWrite(step_ms1z, LOW);
mcp2.digitalWrite(step_ms2z, LOW);
mcp2.digitalWrite(step_ms3z, LOW);
} // full step
if (Startz == 20 && zhome == 0) {
Startz = 21;
Tmrz = 0;
mcp2.digitalWrite(takeastepz, LOW);
mcp2.digitalWrite(axisZenable, HIGH);
}
if (Startz == 21 && zhome == 0 && Tmrz > 20) {
mcp2.digitalWrite(axisZenable, LOW);
mcp2.digitalWrite(directionz, HIGH);
mcp2.digitalWrite(step_ms2z, HIGH);
mcp2.digitalWrite(step_ms1z, HIGH); // 1/8 step
mcp2.digitalWrite(takeastepz, HIGH);
}
if (Startz == 21 && zhome == 1) {
mcp2.digitalWrite(takeastepz, LOW);
}
if (Startz == 21 && zhome == 1 && Tmrz > 20) {
Startz = 0;
//initEncoders();
if (seqr == 0) {
seqr = 6;
}
diss = "RZ"; TransUdp();
mcp2.digitalWrite(step_ms1z, LOW);
mcp2.digitalWrite(step_ms2z, LOW);
mcp2.digitalWrite(step_ms3z, LOW);
mcp2.digitalWrite(directionz, HIGH);
}
}
void jogX() {
// remote computer
if (Startx > 29) {
mcp1.digitalWrite(step_ms1x, LOW);
mcp1.digitalWrite(step_ms2x, LOW);
mcp1.digitalWrite(step_ms3x, LOW);
} // full step
if (Startx == 35 && JogFwdx == 1) {
mcp1.digitalWrite(directionx, LOW);
mcp1.digitalWrite(takeastepx, HIGH);
mcp1.digitalWrite(axisXenable, LOW);
}
if (Startx == 45 && JogRevx == 1) {
mcp1.digitalWrite(directionx, HIGH);
mcp1.digitalWrite(takeastepx, HIGH);
mcp1.digitalWrite(axisXenable, LOW);
}
//Local
if (JogFwdx == 0 && Startx == 30) {
mcp1.digitalWrite(directionx, LOW);
mcp1.digitalWrite(takeastepx, HIGH);
mcp1.digitalWrite(axisXenable, LOW);
}
if (JogFwdx == 1 && Startx == 30) {
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (JogRevx == 0 && Startx == 40) {
digitalWrite(directionx, HIGH);
mcp1.digitalWrite(takeastepx, HIGH);
mcp1.digitalWrite(axisXenable, LOW);
}
if (JogRevx == 1 && Startx == 40) {
Startx = 0;
mcp1.digitalWrite(takeastepx, LOW);
mcp1.digitalWrite(axisXenable, HIGH);
}
if (JogRevx == 1 && Startx == 40 || JogFwdx == 1 && Startx == 30) {
diss = "11X";
TransUdp();
}
}
be careful what you wish for huh?.. lol