How can I set motor speeds to equal eachother?

Hey folks, I found a program recently that I followed to have a Stepper Motor follow a Servo motor to make it look like a skull is looking at you as you pass by. It kinda works but I need some tips, I didn't have luck on the discord but maybe I can get help here!

#include <Stepper.h> //Stepper

#include <Servo.h> // Servo



int stepperAngle = 90;
int pos = 90 ;
int posf;
int multiplier = 10;
const int trig = 5;
const int echo = 4;
const int esp = 12; //Eye Servo Pin
Stepper stepper(posf,8,9,10,11);
Servo eyeServo;
long dur;
int dist;
long lastSeen;


void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(6, OUTPUT);
  pinMode(7,OUTPUT);
  
  eyeServo.attach(esp);
  eyeServo.write(90);
  
 Serial.begin(9600); // Starts serial Communication. (Serial Communication is something that 
                     //                               lets your device & board talk) 
}

void loop() {
  int posf = pos * multiplier;
  int lastAngle = pos;

  // Ultrasonic Tracking
  getDistance();

 if (dist > 10 && dist < 30){ //30 for testing
   
    if (pos < 60){
      pos = 60;
    }else{
      pos-=6;
    }
  }else{
    if (pos > 120){
      pos = 120;
    }else{
      pos++;
    }
  }
  if (pos > 50 && pos < 108){
    lastSeen = millis();
  }
  if (lastSeen != pos){
    eyeServo.write(pos);
  }
  Serial.println(pos);

  delay(1);
  
  digitalWrite(6, HIGH); 
  digitalWrite(7, HIGH);

    //STEPPER ------------------------------------------
  stepToDeg(pos);
  delay(1);
}

void stepToDeg(int stepAngle){
  if (pos < 121){
    int difference = stepperAngle - pos;
    int absDifference = abs(difference);
    int negDif = -difference*7;
    stepper.step(negDif);
    stepperAngle = pos;
  }
}

void getDistance(){ //TAKES AN ULTRASONIC MEASUREMENT AND RETURNS THE COMPUTED DISTANCE IN CM
  // Clears the trigPin
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  
  //Sends pulse
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  dur = pulseIn(echo, HIGH);
    
  dist = dur*0.034/2;
} 

Maybe, if we knew what the problem is.

Explain what the program should do, and what it does instead.

  • don't you need to include Stepper.h?
  • posf, lastAngle, lastSeen are unused.
  • what is the purpose of
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
  • what is the intention of this logic comparing a position to a time in msec?
  • what is the intention of this logic?

i get the following when i run through some case

iteration dist pos
 0   5   1
 1   5   2
 2   5   3
 3   5   4
 4   5   5

 0  15  60
 1  15  54
 2  15  60
 3  15  54
 4  15  60

 0  25  54
 1  25  60
 2  25  54
 3  25  60
 4  25  54

 0  35  55
 1  35  56
 2  35  57
 3  35  58
 4  35  59

 0  45  60
 1  45  61
 2  45  62
 3  45  63
 4  45  64

 0  55  65
 1  55  66
 2  55  67
 3  55  68
 4  55  69

 0  65  70
 1  65  71
 2  65  72
 3  65  73
 4  65  74

 0  75  75
 1  75  76
 2  75  77
 3  75  78
 4  75  79

 0  85  80
 1  85  81
 2  85  82
 3  85  83
 4  85  84

 0  95  85
 1  95  86
 2  95  87
 3  95  88
 4  95  89

 0 105  90
 1 105  91
 2 105  92
 3 105  93
 4 105  94

 0 115  95
 1 115  96
 2 115  97
 3 115  98
 4 115  99

 0 125 100
 1 125 101
 2 125 102
 3 125 103
 4 125 104

 0 135 105
 1 135 106
 2 135 107
 3 135 108
 4 135 109

It's supposed to use the ultra-sonic to track someone walking by, when it spots someone, the servo then moves with it and keeps contact with the person (unless they walk past the it returns to starting position) & the stepper maintains speed with it.

However, what it's doing is the servo continually moves even if the person is standing still. Because of that it almost enters a loop of moving to the right and trying to return to original position. Meanwhile the stepper moves at a snail's pace, moving minimally.

how does a single sensor determine the direction of the person or it the person stops?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.