element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 Seting up motor on limit switches
  • 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 4 replies
  • Subscribers 379 subscribers
  • Views 1000 views
  • Users 0 members are here
Related

Seting up motor on limit switches

Vallka
Vallka over 1 year ago

Hello, I am new to all this but am learning and really enjoying it.

I have been working on a project to turn a machine with a motor for a time period then stop and wait for a tiime period then repeat.

This is not working out so I want to change the plan and make it work on a button/motion sensor and limit switches.

This is the code I have and I had alot of help from others on this.

Ideally you would push a button, the motor would run for a time period or hit limit swich then reverse for a time period and stop until button pushed again.

This is the code I have.

// run forwards for 10sec
// stop for 2sec
// reverse for 10sec,
// stop for 3min
// repeat the loop endlessly.

int r_en = 2;
int l_en = 3;
int r_pwm = 5;
int l_pwm = 6;



void setup() {
pinMode(r_en, OUTPUT);
pinMode(l_en, OUTPUT);
pinMode(r_pwm, OUTPUT);
pinMode(l_pwm, OUTPUT);
}
void loop() {
forward();
delay(10000);
stop();
delay(2000); 
reverse();
delay (10000);
stop();
delay (3 * 60 * 1000); 
}

void stop() {
analogWrite(r_pwm, 0);
analogWrite(l_pwm, 0);
digitalWrite(r_en, LOW); // disable r motor
digitalWrite(l_en, LOW); // disable l motor
}

void forward() {
analogWrite(r_pwm, 150);
analogWrite(l_pwm, 0);
digitalWrite(r_en, HIGH); // enable motor r
digitalWrite(l_en, HIGH); // enable motor l
}

void reverse() {
analogWrite(r_pwm, 0);
analogWrite(l_pwm, 150);
digitalWrite(r_en, HIGH); // enable motor r
digitalWrite(l_en, HIGH); // enable motor l
}

  • Sign in to reply
  • Cancel

Top Replies

  • aspork42
    aspork42 over 1 year ago +1
    Hello, In your subroutines like forward() , the code just runs through the four lines one time, then returns to the main loop. I’m not in front of an arduino to check, but I think that they delay() function…
  • balajivan1995
    balajivan1995 over 1 year ago +1
    I'm assuming you want to add two more input to control the flow of motor instead of coding the sequence to run one event after another. Below is the rough idea of how to implement it. void process_motor…
Parents
  • balajivan1995
    balajivan1995 over 1 year ago

    I'm assuming you want to add two more input to control the flow of motor instead of coding the sequence to run one event after another.

    Below is the rough idea of how to implement it.

    void process_motor()
    {
    	static int time_passed = 0;
    	// Set limit switch and button as external trigger input
    	if (button_press_action)
    	{
    		time_passed = millis();
    		forward();
    		while (millis() - time_passed < 10000 && !limit_switch_action)
    		{
    			// Do nothing here
    		}
    		stop();
    	}
    	if (limit_switch_action)
    	{
    		time_passed = millis();
    		reverse();
    		while (millis() - time_passed < 10000 && !button_press_action)
    		{
    			// Do nothing here
    		}
    		stop();
    	}
    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Vallka
    Vallka over 1 year ago in reply to balajivan1995

    Ok I kinda get this, would I need switches that are NO or NC?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • aspork42
    aspork42 over 1 year ago in reply to Vallka

    They can be either one. Your code is what decides how it reacts. Like I had noted before, you can start with a blank sketch just to test switch input. Once you’ve got that figured out, roll it into your main code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • aspork42
    aspork42 over 1 year ago in reply to Vallka

    They can be either one. Your code is what decides how it reacts. Like I had noted before, you can start with a blank sketch just to test switch input. Once you’ve got that figured out, roll it into your main code.

    • Cancel
    • Vote Up 0 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