Randomised Stepper Movement

I’m a complete newby to the Arduino world but do have a background in IT so I kind of get the main concepts, a bit … I thought it would be fun to play around with it in my spare time and have started out with a project that’s a bit over my head because the deep end is the best place to learn :slight_smile:

I’m after a pointer in the right direction as I’m a bit stumped on using ‘random()’ in conjunction with a pair of stepper motors. Basically I’m wanting to drive both motors at random speeds with random movement but confining one motor to 180 degrees of movement and the other to 45 degrees. At this point I’ve done quite a bit of googling and I’m drawing blank as I don’t really have enough basic information on what I’m after to put a proper search together.

The motors I’ve got are 28BYJ-48’s being run through ULN2003A’s from here:

http://www.dx.com/p/dmdg-uln2003-stepper-motor-driver-module-5v-28byj-48-stepper-motor-for-arduino-349659

Although I’m starting to think that maybe I need servo motors rather than steppers (In case you couldn’t tell, I did the shopping before the research!)

The code I’m using is pinched from the web and adjusted to run 2 motors:

#define IN1 4
#define IN2 5
#define IN3 6
#define IN4 7
#define IN5 8
#define IN6 9
#define IN7 10
#define IN8 11
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left = 4095;
long time;
void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(IN5, OUTPUT);
  pinMode(IN6, OUTPUT);
  pinMode(IN7, OUTPUT);
  pinMode(IN8, OUTPUT);
}


void loop() {

  If(PIR = HIGH) {
    while (steps_left > 0) {
      currentMillis = micros();
      if (currentMillis - last_time >= 1000) {
        stepper(1);
        time = time + micros() - last_time;
        last_time = micros();
        steps_left--;
      }
    }
    Serial.println(time);
    Serial.println("Wait...!");
    delay(2000);
    Direction = !Direction;
    steps_left = 4095;
  }
}

void stepper(int xw) {
  for (int x = 0; x < xw; x++) {
    switch (Steps) {
      case 0:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, HIGH);
        break;
      case 1:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, HIGH);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, HIGH);
        digitalWrite(IN8, HIGH);
        break;
      case 2:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, HIGH);
        digitalWrite(IN8, LOW);
        break;
      case 3:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, HIGH);
        digitalWrite(IN7, HIGH);
        digitalWrite(IN8, LOW);
        break;
      case 4:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, HIGH);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, LOW);
        break;
      case 5:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, HIGH);
        digitalWrite(IN6, HIGH);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, LOW);
        break;
      case 6:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, HIGH);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, LOW);
        break;
      case 7:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        digitalWrite(IN5, HIGH);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, HIGH);
        break;
      default:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        digitalWrite(IN5, LOW);
        digitalWrite(IN6, LOW);
        digitalWrite(IN7, LOW);
        digitalWrite(IN8, LOW);
        break;
    }
    SetDirection();
  }
}
void SetDirection() {
  if (Direction == 1) {
    Steps++;
  }
  if (Direction == 0) {
    Steps--;
  }
  if (Steps > 7) {
    Steps = 0;
  }
  if (Steps < 0) {
    Steps = 7;
  }
}

Could anyone point me in the right direction or give me a wee hint?

[edit to format code]

using CTRL-T in the IDE does auto indenting the code, more readable

the concept of keeping the angle between 0 and 180 degrees is to let it overflow when it happens but add a safety net that brings it within range. In code the idea looks like

position += random(180)
if position > 180) position -= 180;

motor.goto position

45 degrees could be done similar.

Thanks for the auto format hint, that's going to make life a lot easier! Have reformatted the code in the original post.

Looking at your code, it's calling a position variable, so it that relying on a library to work? I haven't got a library in my code as the stepper library caused this motor to judder a bit but not much else. Should I be looking to create a library for this motor as a starting point and including those functions from the stepper library?

robtillaart: using CTRL-T in the IDE does auto indenting the code, more readable

the concept of keeping the angle between 0 and 180 degrees is to let it overflow when it happens but add a safety net that brings it within range. In code the idea looks like

position += random(180)
if position > 180) position -= 180;

motor.goto position




45 degrees could be done similar.

But why not the much simpler:

  position = random (180) ;

@MarkT Of course you are right, but the OP should have seen this optimization ;)