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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum Help with interrupt problem
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 6 replies
  • Subscribers 304 subscribers
  • Views 786 views
  • Users 0 members are here
  • frontpage
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

Help with interrupt problem

Former Member
Former Member over 11 years ago

Hello,

I got this stepper motor running code and it works fine. However I am trying to write a code to enable me to pause it and turn on led on pin 13, when I press a button and then have it continue running where it left off when I release the button.

I tried by attaching an interrupt but it didn’t work. The modifications I made are in bold. Anyone know what I did wrong?

I’m using an Arduino mega 2560.

Here is the code below. Any help will be appreciated.

 

//////////////////////////////////////////////////////////////////

//Copyright2011 bildr

//Released under the MIT License - Please reuse change and share

//Using the easy stepper with your arduino

//use rotate and/or rotateDeg to controll stepper motor

//speed is any number from .01 -> 1 with 1 being fastest -

//Slower Speed == Stronger movement

/////////////////////////////////////////////////////////////////

#include <avr/interrupt.h>

 

# define LED_PIN 13

# define SWITCH_PIN 5      // pin 18 for interrupt on Arduino mega 2560

 

#define DIR_PIN 2

#define STEP_PIN 3

 

void setup() {

pinMode(DIR_PIN, OUTPUT);

pinMode(STEP_PIN, OUTPUT);

  pinMode(LED_PIN, OUTPUT);

  // switch input

  pinMode(18, INPUT);

  // attach the interrupt on pin 18

  attachInterrupt(SWITCH_PIN, light, LOW);

}

 

void light() {

  digitalWrite(LED_PIN, HIGH);

}

 

void loop(){

 

//rotate a specific number of degrees

rotateDeg(360, 1);

delay(1000);

 

rotateDeg(-360, .1);  //reverse

delay(1000);

 

 

//rotate a specific number of microsteps (8 microsteps per step)

//a 200 step stepper would take 1600 micro steps for one full revolution

rotate(1600, .5);

delay(1000);

 

rotate(-1600, .25); //reverse

delay(1000);

}

 

 

 

void rotate(int steps, float speed){

//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)

//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger

int dir = (steps > 0)? HIGH:LOW;

steps = abs(steps);

 

digitalWrite(DIR_PIN,dir);

 

float usDelay = (1/speed) * 70;

 

for(int i=0; i < steps; i++){

digitalWrite(STEP_PIN, HIGH);

delayMicroseconds(usDelay);

 

digitalWrite(STEP_PIN, LOW);

delayMicroseconds(usDelay);

}

}

 

void rotateDeg(float deg, float speed){

//rotate a specific number of degrees (negitive for reverse movement)

//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger

int dir = (deg > 0)? HIGH:LOW;

digitalWrite(DIR_PIN,dir);

 

int steps = abs(deg)*(1/0.225);

float usDelay = (1/speed) * 70;

 

for(int i=0; i < steps; i++){

digitalWrite(STEP_PIN, HIGH);

delayMicroseconds(usDelay);

 

digitalWrite(STEP_PIN, LOW);

delayMicroseconds(usDelay);

}

}

  • Sign in to reply
  • Cancel
Parents
  • booran
    booran over 11 years ago

    You'll need to adjust your light() method to handle both turning on and off, for example, you can read the current state of the pin (or make a variable to toggle the state)

     

     

    void light() {

         // Check if pin is high

         if(digitalRead(LED_PIN))

         {

              digitalWrite(LED_PIN, LOW);

         }

         // Pin is low, so turn high

         else

         {

              digitalWrite(LED_PIN, HIGH);

         }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • booran
    booran over 11 years ago

    You'll need to adjust your light() method to handle both turning on and off, for example, you can read the current state of the pin (or make a variable to toggle the state)

     

     

    void light() {

         // Check if pin is high

         if(digitalRead(LED_PIN))

         {

              digitalWrite(LED_PIN, LOW);

         }

         // Pin is low, so turn high

         else

         {

              digitalWrite(LED_PIN, HIGH);

         }

    }

    • 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