2 Replies Latest reply on May 25, 2021 9:22 AM by amgalbu

    Stepper Motor With FRS

    jerrychok

      Hello Everyone,

      I'm  here asking for help for my project, what I'm facing now is, is don't know how to make my stepper motor stop in a certain direction, Here is what I want my project to work,

      When there is no force detected stepper motor should not move,

      when there is force detected the stepper motor should spin in a clockwise direction and stop at a certain step until when the force is removed then the stepper motor will spin in a counter-clockwise direction and stop at a certain step.

       

      From now what I manage is, manage to have the force sensor when there is force it will move in a clockwise direction and when the force is removed it will turn counter clock-wise, but it did not stop in a certain direction.

       

      Below is my code.

       

      #define dirPin 5
      #define stepPin 2
      #define stepsPerRevolution 1600
      
      
      int fsrAnalogPin = 0;
      int fsrReading;
      
      
      void setup() {
         // Declare pins as output:
        pinMode(fsrReading, INPUT);
      
        pinMode(stepPin, OUTPUT);
        pinMode(dirPin, OUTPUT);
      
      
      }
      
      void loop() {
      Serial.begin(9600);
      fsrReading = analogRead(fsrAnalogPin); 
      
        Serial.print("Analog reading = ");
        Serial.print(fsrReading);
      
      if(fsrReading > 200){
        digitalWrite(dirPin, LOW);
        // Spin the stepper motor 1 revolution slowly:
        for (int i = 0; i < 500; i++) {
          // These four lines result in 1 step:
          digitalWrite(stepPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPin, LOW);
          delayMicroseconds(1000);
        }
        delay(2000);
      }
      else if(fsrReading < 200){
        digitalWrite(dirPin, HIGH);
        // Spin the stepper motor 1 revolution quickly:
        for (int i = 0; i < 500; i++) {
          // These four lines result in 1 step:
          digitalWrite(stepPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPin, LOW);
          delayMicroseconds(1000);
        }
        delay(2000);
      }
      }
      

       

      Message was edited by: Christopher Stanton - code formatting

        • Re: Stepper Motor With FRS
          shabaz

          Hi Jerry,

           

          There's a few points:

           

          (1) It is hard reading code unless it is formatted correctly using a monospaced font. You can embed code using that row of icons/tools in the online editor, when you created your post. If you click on the ">>" icon, you can select "Syntax highlighting", it makes it much easier for people to follow.

          (2) The more lines of code you write, the more chance of errors creeping in. If you find you are repeating things, then make a separate function for them, so that you can reduce the lines of code down to the function and the function call. For example, you could have had a function called (say) stepOnce() and in that you could have had the lines containing digitalWrite(stepPin, HIGH) and delayMicroSeconds.

          (3) It's bad form to have the same constant 1000 everywhere. Create a #define or a variable, in one place called (say) STEPDELAY. That way, you can easily change it across the board without making edits in four places.

          (4) A flow-chart or pseudo-code will help you until you're more familiar with coding and (importantly) troubleshooting

          (5) In the real world, analog signals have noise, they vary. They can also drift, say with temperature changes. In your code, you are checking to see if the analog signal, converted to a digital value, is above 200, or below 200, and in each case, your code spins the motor, and then the code loops back around and re-executes (because your code is in an 'Arduino function' called loop). It follows that the only way the motor will be stationary indefinitely is if the integer digital value is exactly 200, no more, no less. Either that was accidental and you've coded and missed that condition, or you need to ask yourself, what are the chances that the analog signal will have no noise, and will not drift, and will end up in that condition that the digital value is precisely 200?

          3 of 3 people found this helpful
          • Re: Stepper Motor With FRS
            amgalbu

            Hello Jerry

            loop() is called repeatedly

            This means that your code will do these steps

             

            1. read analog input

            2. make 500 steps

            3. wait 2 seconds

            4. return to point n.1

             

            I expect the motor will never stop unless you add a flag that says you already performed you 500 steps

             

            cheers