I am trying to control a stepper over the ethernet. I am new at this.
The stepper code works on its own.
The ethernet code works on its own. I am just using the DHCP chat relay example code.
When I combine the code there must be something I am doing wrong because only the ethernet functionality works.
What have I tried:
I have uploaded each of the programs many times and am sure that the hardware is fine. I have check the GPIO pins with a DMM and am sure they are not functioning with the combined code is used but they are working when I use the stepper code, also the stepper moves.
I have inserted a serial communication debug flag near where the stepper should be moving and I am sure the program is reaching the correct section of code.
I have tried a bunch of resets.
I have tried moving around the stepper class instantiation before or after calling the other libraries.
I have tried declaring the GPIO as input or output in several places.
I have tried using the analog pins, thinking perhaps SPI is hogging all the digital pins?
It seems like one of the other libraries is disabling the GPIO.
Stepper only code: This is working just fine.
#include <AccelStepper.h>
// Define some steppers and the pins the will use
//AccelStepper stepper(1, pinSTEP, pinDIR);
AccelStepper stepperX(AccelStepper::FULL2WIRE,6, 7);
//AccelStepper stepper(1, pinSTEP, pinDIR);
AccelStepper stepperY(AccelStepper::FULL2WIRE, 2, 3);
void setup()
{
Serial.begin(115200); // make sure it's the same on both sides
stepperX.setMaxSpeed(8000.0);
stepperX.setAcceleration(5000.0);
stepperX.moveTo(10);
stepperY.setMaxSpeed(8000.0);
stepperY.setAcceleration(5000.0);
stepperY.moveTo(10);
}
void loop()
{
// ADD this code back in to reverse the move to setpoint for the stepper motor
// Change direction at the limits
// if (stepperX.distanceToGo() == 0)
// stepperX.moveTo(-stepperX.currentPosition());
// if (stepperY.distanceToGo() == 0)
// stepperY.moveTo(-stepperY.currentPosition());
//ADD this code if you want to use a serial line "keypad" to enter move commands
char incomingChar = 0; // for incoming serial data
if (Serial.available() > 0) {
incomingChar = Serial.read();
switch (incomingChar) {
case 'l':
//Move Code motor 1
stepperX.moveTo(stepperX.currentPosition() - 500);
break;
case 'r':
//Move Code motor 1
stepperX.moveTo(stepperX.currentPosition() + 500);
break;
case 'u':
//Move Code motor 2
stepperY.moveTo(stepperY.currentPosition() + 500);
break;
case 'd':
//Move Code motor 2
stepperY.moveTo(stepperY.currentPosition() - 500);
break;
default:
break;
}
} else incomingChar = 0; // not really needed but in case you want to do things in the loop upon receiving new chars. if (incomingChar == 0) then no new char was received, otherwise later in the loop you still have the char received
stepperX.run();
stepperY.run();
}
**Ethernet code: The ethernet communication portion is working but the stepper will not move **
#include <AccelStepper.h>
// Define some steppers and the pins the will use
//AccelStepper stepper(1, pinSTEP, pinDIR);
AccelStepper stepperX(AccelStepper::FULL2WIRE, 6, 7); //9,10
//AccelStepper stepper(1, pinSTEP, pinDIR);
AccelStepper stepperY(AccelStepper::FULL2WIRE, 2, 3);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x07
};
IPAddress ip(192, 168, 0, 15);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
//pinMode(6,OUTPUT);
//pinMode(7,OUTPUT);
//pinMode(2,INPUT);
//pinMode(3,INPUT);
//Setup Steppers
stepperX.setMaxSpeed(8000.0);
stepperX.setAcceleration(5000.0);
stepperX.moveTo(100);
stepperY.setMaxSpeed(8000.0);
stepperY.setAcceleration(5000.0);
stepperY.moveTo(100);
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// initialize the Ethernet device not using DHCP:
Ethernet.begin(mac, ip, myDns, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
// start listening for clients
server.begin();
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println("We have a new client");
client.println("Hello, client!");
gotAMessage = true;
}
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.print(thisChar);
switch (thisChar) {
case 'l':
//Move Code motor 1
stepperX.moveTo(stepperX.currentPosition() - 500);
client.println("Moved Left");
break;
case 'r':
//Move Code motor 1
stepperX.moveTo(stepperX.currentPosition() + 500);
break;
case 'u':
//Move Code motor 2
stepperY.moveTo(stepperY.currentPosition() + 500);
break;
case 'd':
//Move Code motor 2
stepperY.moveTo(stepperY.currentPosition() - 500);
break;
default:
break;
}
Ethernet.maintain();
}
}