Go Down

Topic: Stepper motor controller and arduino (Read 12021 times) previous topic - next topic

ACrespin124

Hahaha my code was kind of a test for the motor and the driver, but your code easily separates variables for adjustment in a way that I've been wondering how to do myself.  Just learned a little something new in the process :)

I've come across an even worse problem now, I tried Verifying my old code that worked from before and now it's giving me this error which I know others have had before but I can't find the answer to it.

at java.util.regex.Pattern$GroupTail.match(Pattern.java:4717)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4568)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3777)
at java.util.regex.Pattern$Branch.match(Pattern.java:4604)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
at java.util.regex.Pattern$Loop.match(Pattern.java:4785)

The previous "working" code is actually below (next post, won't let me post in this one) but I removed the second motor block since it was giving me trouble with the move back and forth.

Robin2

That Java problem is because whoever wrote the Arduino IDE did not trap all possible errors correctly. There is some mistake in your code that Java is choking on. You will need to read through your code very carefully. Perhaps make a copy of your code and then delete chunks to isolate where the choke point is.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ACrespin124

WOOT! I found the error after reading all lines of code that I had modified...

So now I'm back to square one: my code worked and yours didn't on the board, but I'll make a diagram to show my wiring exactly how I have it.

 Here's my code:

Code: [Select]

#include <Keypad.h>
#include <Adafruit_NeoPixel.h>


// Define the states for the lock state machine
#define LOCKED 2
#define UNLOCKED 0

// State Variables:   Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;
int Distance = 0; //Record steps

// Define your password key sequence here
char* secretCode = "2301000";

// Keypad key matrix:
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// Keypad pin definitions
byte rowPins[rows] = {5, 6, 7, 8};
byte colPins[cols] = {2, 3, 4};

// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

int transistorPin = 9; //LED Transistor Pin Definition

int StepperDistance = 10; //Stepper Distance Pin Definiton
int StepperDirection = 11; //Stepper Direction Pin Definition
int StepperStart = 12; //Stepper Engage Pin Definition

int ColorLED = 13; //Color LED Definition

Adafruit_NeoPixel IndicatorLED = Adafruit_NeoPixel(2, ColorLED, NEO_RGB + NEO_KHZ800);
//400 IS FOR WS2811 and 800 IS FOR WS2812


void setup()
{
  //Set stepper and Light Parts as Outputs)
  pinMode(transistorPin, OUTPUT);
  pinMode(StepperDistance, OUTPUT);
  pinMode(StepperDirection, OUTPUT);
  pinMode(StepperStart, OUTPUT);



  //Set stepper settings off and Clockwise
  digitalWrite(StepperStart, LOW);
  digitalWrite(StepperDistance, LOW);
  digitalWrite(StepperDirection, LOW);


  // Initialize state of lock and communications.
  setLockState(LOCKED);
  IndicatorLED.begin();
  IndicatorLED.show(); //Initialize LEDs to 'off'
  Serial.begin(9600);
}


void loop()
{
  // Run the state machine:

  // Locked State - Monitor keypad for valid Password code entry
  if (LockState == LOCKED)
  {
    char key = keypad.getKey();

    if (key == '*' || key == '#')
    {
      position = 0;
    }
    if (key != 0)
    {
      if (key == secretCode[position])  // Valid key in Password sequence
      {
        Serial.print("Matched ");
        Serial.print(key);
        Serial.print("-at-");
        Serial.println(position);
        position ++;

        IndicatorLED.setPixelColor(0, 255, 0, 0);
        IndicatorLED.setPixelColor(1, 255, 0, 0);
        IndicatorLED.show();
        delay(10);
        IndicatorLED.setPixelColor(0, 0, 0, 0);
        IndicatorLED.setPixelColor(1, 0, 0, 0);
        IndicatorLED.show();
        //Green LED flash
      }
      else  // Invalid key - start all over again
      {
        Serial.println("Invalid Code!");
        position = 0;

        IndicatorLED.setPixelColor(0, 0, 255, 0);
        IndicatorLED.setPixelColor(1, 0, 255, 0);
        IndicatorLED.show();
        delay(100);
        IndicatorLED.setPixelColor(0, 0, 0, 0);
        IndicatorLED.setPixelColor(1, 0, 0, 0);
        IndicatorLED.show();
        //Red LED on for 2 seconds
      }
    }


    if (position == 7)  // Password successfully entered - advance state
    {
      setLockState(UNLOCKED);

      position = 0;
    }
    delay(100);
  }

}

// Set the state and the time of the state change
void setLockState(int state)
{
  LockState = state;
  StartTime = millis ();
  if (state == LOCKED)
  {
    Serial.println("Locked!");
  }

  else if (state == UNLOCKED)
  {
    Serial.println("Unlocked!");


    //MyStepper.step(STEPS);
    digitalWrite(StepperStart, HIGH);
    delay(25);
    digitalWrite(StepperDirection, LOW);
    digitalWrite(StepperDistance, HIGH);
    delay(100);
    digitalWrite(StepperDistance, LOW);
    digitalWrite(StepperStart, LOW);
    
    //Code for 90 degree clockwise turn below... 1:1 stepping ratio, no chopping
    if(Distance < 90);
    {
      digitalWrite(StepperDirection, LOW);
      digitalWrite(StepperDistance, HIGH);
      delayMicroseconds(100);
      digitalWrite(StepperDistance, LOW);
      delayMicroseconds(100);
      Distance = Distance + 1; //Record this Step
      Serial.println("Stepped!");
    }
    
      
      
      
    for (int brightness = 0; brightness < 255; brightness++) {
      analogWrite(transistorPin, brightness);
      delay(12);
    }

    rainbow(12); //Start flashing Rainbow LEDs
    

    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);  //0, Begin Rainbow Countdown to Close ROYGBP
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 255, 0);
    IndicatorLED.setPixelColor(1, 0, 255, 0);  //RED
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(100);
    IndicatorLED.setPixelColor(0, 100, 255, 0);
    IndicatorLED.setPixelColor(1, 100, 255, 0);  //ORANGE
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(100);
    IndicatorLED.setPixelColor(0, 255, 255, 0);
    IndicatorLED.setPixelColor(1, 255, 255, 0);  //YELLOW
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(100);
    IndicatorLED.setPixelColor(0, 255, 0, 0);
    IndicatorLED.setPixelColor(1, 255, 0, 0);  //GREEN
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(100);
    IndicatorLED.setPixelColor(0, 0, 0, 255);
    IndicatorLED.setPixelColor(1, 0, 0, 255);  //BLUE
    IndicatorLED.show();
    delay(1000);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(100);
    IndicatorLED.setPixelColor(0, 0, 255, 255);
    IndicatorLED.setPixelColor(1, 0, 255, 255);  //PURPLE
    IndicatorLED.show();
    
    

    //Fade off the Light
    for (int brightness = 255; brightness >= 0; brightness--) {
      analogWrite(transistorPin, brightness);
      delay(20);
    }
    //Close Cabinet
    
    //Code for 90 degree clockwise turn below...
    
    
    //Indicate the code will return to locked state
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(500);
    IndicatorLED.setPixelColor(0, 0, 255, 0);
    IndicatorLED.setPixelColor(1, 0, 255, 0);  //RED
    IndicatorLED.show();
    delay(500);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    delay(500);
    IndicatorLED.setPixelColor(0, 0, 255, 0);
    IndicatorLED.setPixelColor(1, 0, 255, 0);  //RED
    IndicatorLED.show();
    delay(500);
    IndicatorLED.setPixelColor(0, 0, 0, 0);
    IndicatorLED.setPixelColor(1, 0, 0, 0);
    IndicatorLED.show();
    setLockState(LOCKED);


  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < IndicatorLED.numPixels(); i++) {
      IndicatorLED.setPixelColor(i, Wheel((i + j) & 255));
    }
    IndicatorLED.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return IndicatorLED.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return IndicatorLED.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return IndicatorLED.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}


Robin2

Here's my code:
That is very long compared to mine :)

If this code makes your stepper move more than one step then your driver must be very different from the type my code is intended for
Code: [Select]
    digitalWrite(StepperStart, HIGH);
    delay(25);
    digitalWrite(StepperDirection, LOW);
    digitalWrite(StepperDistance, HIGH);
    delay(100);
    digitalWrite(StepperDistance, LOW);
    digitalWrite(StepperStart, LOW);


If this code does make you stepper move multiple steps please post a link to the datasheet for your stepper driver.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up