Help! I can't find the error in my code!

I have just begun programming the Arduino and it's quite a blast. However, I have run into a problem somewhere in my code. I have created a little program which receives commands over a TCP/IP protocol, using the arduino ethernet shield, and then commands two stepper motor drivers.

The problem is that I cannot seem to get pin 11 (the direction on the linear stepper motor to change). It sits at 3.86V. Everything else works wonderfully.

I modified a basic blink program to check if the pin was working and I could get it to switch between high and low without any problems. So, that makes me think that it is somewhere in the code. Any suggestions?

/*
 * Ethernet-Stepper Server
 *
 * This program functions as a server which receives commands from a client and sends pulses to
 * a stepper motor based upon the received command. The program also sends back information reporting
 * the actions it has taken. A EasyDriver v3 is attached to pins 2 & 3.
 * The command set as received through the ethernet shield is as follows:
 * "SXXXX" = S plus four digits produces a step of XXXX degrees. Ex. S0360 is one rotation
 * "TXXXX" = T plus four digits sets the delay between each step in milliseconds. Default is 150. EX T0300 sets a 300 millisecond delay
 * "DX" = D plus either R or L sets the direction of the stepper motor.
 * "LXXXX" = L plus four digits produces a step of XXXX degrees in the linear motor.
 * "KX" = K plus F or B sets the direction of the linear motor. Forward or backward.
 * "JXXXX" = sets the linear motor delay in milliseconds
 */

#include <Ethernet.h>

// network configuration.  gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 254, 39 };

// telnet defaults to port 23
Server server = Server(23);
int dirpin = 7; //The stepper direction is set by this pin
int steppin = 6; //The stepper motion is controlled by pulses on this pin.
int linsteppin = 9;
int lindirpin = 11;
int angle = 0;
int linangle = 0;
int numofsteps = 0;
int linnumofsteps = 0;
int TimeDelay = 150; //This is the stepper motor's delay parameter
int linTimeDelay = 1000; //This is the linear motor's delay parameter.

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip);
  // start listening for clients
  server.begin();
  
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(linsteppin, OUTPUT);
  pinMode(lindirpin, OUTPUT);
  digitalWrite(dirpin, HIGH); //Set default direction
  digitalWrite(lindirpin, LOW);
  
}

void loop()
{
  Client client = server.available();
  if (client)
  {
    boolean messageComplete = false;
    long timeOut = millis();
    char lineBuffer[100]; // large buffer for storing data
    int charCount;
  
    while (!messageComplete || server.available())
    {
      if ((abs(millis() - timeOut)) > 2000)
      {
        server.println("Timeout On Response");
        break;
      }
      if (server.available())
      {
        lineBuffer[charCount] = client.read();
        if (lineBuffer[charCount] == 13) //Waits for a carriage return
        {
          messageComplete = true;
        }
        charCount++;
        delay(50);
      }
    }

    if (messageComplete)
    {
      switch (lineBuffer[0])
      {
        case 'D':
          if (lineBuffer[1] == 'R')
          {
            digitalWrite(dirpin, HIGH);
            server.println("Going CW");
          }
          else if (lineBuffer[1] == 'L')
          {
            digitalWrite(dirpin, LOW);
            server.println("Going CCW");
           }
          break;
        case 'K':
          if (lineBuffer[1] == 'F')
          {
            digitalWrite(lindirpin, LOW);
            server.println("Going Forward");
          }
          else if (lineBuffer[1] == 'B')
          {
            digitalWrite(lindirpin, HIGH);
            server.println("Going Backward");
           }
          break;

          case 'T':
            char tempDelay[5];
          for (int i = 1; i < 5; i++)
          {
            if (lineBuffer[i] >= '0' && lineBuffer[i] <= '9')
            {
              tempDelay[i - 1] = lineBuffer[i];
            }
            else
            {
              server.println("Invalid Time Delay");
              break;
            }
          }
          TimeDelay = atoi(tempDelay);
          server.print("Valid Rotary Time Delay Received = ");
          server.print(TimeDelay);
          server.println(" Milliseconds.");
          break;
            
            case 'J':
            char lintempDelay[5];
          for (int i = 1; i < 5; i++)
          {
            if (lineBuffer[i] >= '0' && lineBuffer[i] <= '9')
            {
              lintempDelay[i - 1] = lineBuffer[i];
            }
            else
            {
              server.println("Invalid Time Delay");
              break;
            }
          }
          linTimeDelay = atoi(lintempDelay);
          server.print("Valid Linear Time Delay Received = ");
          server.print(linTimeDelay);
          server.println(" Milliseconds.");
          break;
        case 'S':
          char tempSteps[5];
          for (int i = 1; i < 5; i++)
          {
            if (lineBuffer[i] >= '0' && lineBuffer[i] <= '9')
            {
              tempSteps[i - 1] = lineBuffer[i];
            }
            else
            {
              server.println("Invalid Angle");
              break;
            }
          }
          numofsteps = atoi(tempSteps);
          server.print("Valid Angle Received = ");
          server.print(numofsteps);
          server.println(" Degrees.");
          moveMotor();
          break;

            case 'L':
          char lintempSteps[5];
          for (int i = 1; i < 5; i++)
          {
            if (lineBuffer[i] >= '0' && lineBuffer[i] <= '9')
            {
              lintempSteps[i - 1] = lineBuffer[i];
            }
            else
            {
              server.println("Invalid Lin Angle");
              break;
            }
          }
          linnumofsteps = atoi(lintempSteps);
          server.print("Valid Lin Angle Received = ");
          server.print(linnumofsteps);
          server.println(" Degrees.");
          linmoveMotor();
          break;
        default:
          server.println("Default");
          break;
      }
    }
  }  
}

void moveMotor()
{
  server.print("Taking ");
  angle = (4.4444 * numofsteps);
  server.print(angle);
  server.println(" Lines.");
    
     
  for (int i = 0; i<angle; i++)
  {
    digitalWrite(steppin, HIGH);
    delayMicroseconds(TimeDelay); //The delay helps prevent this motor from skipping steps. This variable must be set for each motor via testing.
    digitalWrite(steppin, LOW);
    delayMicroseconds(TimeDelay); //The delay helps prevent this motor from skipping steps. This variable must be set for each motor via testing.
  }
  server.println("Move Complete.");
} 

void linmoveMotor()
{
  server.print("Taking ");
  linangle = (4.4444 * linnumofsteps);
  server.print(linangle);
  server.println(" Lines.");
    
     
  for (int i = 0; i<linangle; i++)
  {
    digitalWrite(linsteppin, HIGH);
    delayMicroseconds(linTimeDelay); //The delay helps prevent this motor from skipping steps. This variable must be set for each motor via testing.
    digitalWrite(linsteppin, LOW);
    delayMicroseconds(linTimeDelay); //The delay helps prevent this motor from skipping steps. This variable must be set for each motor via testing.
  }
  server.println("Move Complete.");
}

Are you using pin 11? The code is saying to use pin 7 ?

"int dirpin = 7; //The stepper direction is set by this pin"

Lefty

Yes. I am using pin 11. There are actually two motors being ran through the Arduino (a regular stepper & a hybrid linear stepper). The linear stepper variables are prefixed with lin. Lindirpin (Lin = Linear, Dir = direction, pin = pin) is the one that is not working right.

The variable dirpin works fine though.

Sorry for not fully commenting the code. :slight_smile:

Also, another note on this. When I do command pin 11 to change, it briefly drops voltage but then returns right back to the 3.86V.

The ethernet shield uses pin 11 (and 12 and 13 and, IIRC, 10). This is the ATmega's SPI hardware.

You'll have to move your motor to a different pin (or move the ethernet shield, and rewrite a large chunk of the ethernet library to match).

-j

Good spot. I changed the code to use pin 8 for the linear direction pin. Oddly, I am still unable to switch the pin. It is always at the "low" setting even when I send the command "KB". It displays the "going backward" line but does not actually change.

any ideas?

What version of the IDE and Ethernet Library are you using? There was a bug that was causing interference on pins 8 and 9 in version 12 of the IDE.

  • Fixing bug in Ethernet library that interfered with use of pins 8 and 9.

Version 12... :slight_smile:

I'll try switching tomorrow and i'll see if it takes care of it.

Yup, switching to version 13 did it. Now it is working correctly.

Good to hear! :wink: