element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Stepper Motor With FRS
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 2 replies
  • Subscribers 388 subscribers
  • Views 1299 views
  • Users 0 members are here
  • frs
  • stepper motor with arduino
  • force sensor
  • arduino
Related

Stepper Motor With FRS

jerrychok
jerrychok over 4 years ago

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

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 4 years ago +3
    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…
Parents
  • shabaz
    shabaz over 4 years ago

    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?

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • shabaz
    shabaz over 4 years ago

    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?

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube