added limit switch interrupts to motor controller, I don't know if its correct

Hi everyone, with the help of this forum I finished the code for my motor controller, but to further my Arduino education, I would like to add interrupts for my limit switches. I found several explanations for them, but I am trying to use them as pullup to not run voltage to them. I couldn't find any two examples that are the same, and I think I have at least one line that doesnt belong, although the compiler took it. Can you look and see if it's wrong. Thanks, adown

//Lift Controller with Limit Switch Interrupts

const int upswitchPin = 4;      //pin number of up button input
const int downswitchPin = 5;  //pin number of down button input
                 
const int goupPin = 7;          //pin number of the up output pin
const int godownPin = 8;      //pin number of the down output pin

int uplimitInt = 0;               //Up limit interrupt on pin 2
int downlimitInt = 1;           //Down limit interrupt on pin 3

int upswitchState = 0;        // innitialize the up switch state to 0
int downswitchState = 0;    // innitialize the down switch state to 0

void setup() {
   pinMode(goupPin, OUTPUT);                // set motor relay pins as inputs and outputs
   pinMode(godownPin, OUTPUT);
   
   pinMode(upswitchPin, INPUT_PULLUP); //Set up and down switch pins to internal pullup
   pinMode(downswitchPin, INPUT_PULLUP);
   
   pinMode(2, INPUT);            //Make 2 a digital input
   digitalWrite(2, HIGH);         //Enable internal pullup
   pinMode(3, INPUT);            //Make 3 a digital input
   digitalWrite(3, HIGH);        //Enable internal pullup
   
   attachInterrupt(uplimitInt, upStop, FALLING);     // Pin 2 upStop
   interrupts();
   attachInterrupt(downlimitInt, downStop, FALLING); // Pin 3 downStop
   interrupts();
  }
  
void loop() {
   upswitchState = digitalRead(upswitchPin);          // read the up switch input
   downswitchState = digitalRead(downswitchPin);  // read the down switch input
     
   if (upswitchState == LOW) {            //check to see if the up button is pressed
       goup();
   }   
   else if (downswitchState == LOW) { //check to see if the down button is preesed
       godown();
   }
}
   void goup() {
      digitalWrite(goupPin, HIGH);      // power the up output pin
   }   
   void godown() {
     digitalWrite(godownPin, HIGH);  // power the down output pin
   }
   void upStop() {   //Pin 2 stop motor giong up
     digitalWrite(goupPin, LOW);      // write power up output pin low
   }  
   void downStop() { //Pin 3 stop motor going down
     digitalWrite(godownPin, LOW);  //Write power down output pin low
   }
const int goupPin = 7;          //pin number of the up output pin
const int godownPin = 8;      //pin number of the down output pin

int uplimitInt = 0;               //Up limit interrupt on pin 2
int downlimitInt = 1;           //Down limit interrupt on pin 3


Don't do this! All ways use the smallest data type that will hold you numbers. In this case you would be much better of using

[code]
#define grioupPin 7
etc

[/code]

Next

   attachInterrupt(uplimitInt, upStop, FALLING);     // Pin 2 upStop
   interrupts();
   attachInterrupt(downlimitInt, downStop, FALLING); // Pin 3 downStop
   interrupts();

There is no need to keep turning on interrupts with the interrupts() call.

   upswitchState = digitalRead(upswitchPin);          // read the up switch input
   downswitchState = digitalRead(downswitchPin);  // read the down switch input

If you are going to play with interrupts then why are you also polling the pins in question?

And

f***ing well auto format your code BEFORE posting it help us - (and if you want (but I don't care) it will help you).

Mak

Mak, thanks. It was the code that keeps turning on the interrupts I thought didnt belong.

The digital read is on the up or down motor button, not the limit switches.

I thought I did auto format. I will ensure I do it in the future.
Thanks, adown

It's a bit of a dodgy setup. If you press the button to go down, it will go down until it hits the limit switch. Press the down button again and the down motor will start again and the limit switch will do nothing to stop it (because the interrupt is only called as it goes from HIGH to LOW).

This is a gear mangling recipe waiting to happen. Personally I prefer to implement limit switches in hardware with some feedback to allow the controller to detect that they've been triggered. This way, even if your code does something unexpected, you're not going to damage anything.

KenF, thanks for your input. I hadnt thought of that. I think I will stick to my original code that checks the motore switch and the limit switch then goes to motor control code, so it wont move without the limit switch set for thd other direction. Thanks again, adown

adown: Hi everyone, with the help of this forum I finished the code for my motor controller, but to further my Arduino education, I would like to add interrupts for my limit switches.

I don't think it has been helpful to start a new Thread because I suspect some of the stuff in your other Thread is very relevant.

Changing from a polling system to a system using interrupts is not the same as deciding to change the colour on your bedroom walls. The interrupt concept has to be designed into the control system.

I know you rejected my suggestion to use a state variable in place of an equally suitable alternative. However the state variable system would be easily adapted to using interrupts. I had suggested using a variable called motorDirection which could take the values 'U' 'D or 'S'

If I was using interrupts the ISR could then be as simple as this

void downStop() {
   motorDirection = 'S';
   limitDirection = 'D' // to record what direction it was going when the limit switch was pressed
}

You need to use the variable limitDirection to allow upward movement after the down limit is reached and vice versa.

All in all, this seems to be a case where using interrupts (rather than polling) makes the logic of the code more complicated precisely because they act indepenedently.

...R

I don't think it has been helpful to start a new Thread because I suspect some of the stuff in your other Thread is very relevant.

I thought it was the right thing to as I was seeking code help not approach suggestions.

I know you rejected my suggestion to use a state variable in place of an equally suitable alternative. However the state variable system would be easily adapted to using interrupts. I had suggested using a variable called motorDirection which could take the values 'U' 'D or 'S'

I couldn't figure it out, even with searching and reading, and I was afraid to ask for further help. That was the best I could do.

All in all, this seems to be a case where using interrupts (rather than polling) makes the logic of the code more complicated precisely because they act indepenedently.

I agree, verrrry complicated. Thats why I took Mak's suggestion and stuck with my original code.

I do appreciate your, but I am new to this, and I learn by doing.

I sometimes can't figure out the cryptic messages in some of the responses on this forum although I DO search and read a lot. Us new comers come to this forum for help and guidance from some of the best programmers around the world. There are a few that help, there are a few that like to act like they are helping, and there are a few that are just down right rude and should keep their comments to themselves. The latter leave new comers looking elsewhere for help. I mean no disrespect, and I realize this may get me banned, or at least black balled to never be helped again, but all I'm trying to do is learn this exciting new(to me) language, and turning to the experts for help. Don't be so reluctant to give a couple lines of actual code, or exactly where things should be entered.

I thank you, and everyone who helped me again for your help and guidance from all of my threads, adown

1 Like

If the equipment, motor or motor power supply could be damaged in case of Arduino malfunction, then a much better (and very common) arrangement for the limit switches is for them to open the motor power supply lead. That way the motor is guaranteed to stop at the endpoints. You need diodes so that the motor can be reversed after running into a limit. For example these linear actuators http://www.gimsonrobotics.co.uk/GLA750_12V-linear-actuator.html have built in switches and diodes. The circuit diagram is here http://www.gimsonrobotics.co.uk/product_images/gla_limit-switch-circuit.jpg

To detect whether a limit switch has been opened, you can usually monitor the motor current.

adown: I couldn't figure it out, even with searching and reading, and I was afraid to ask for further help. That was the best I could do.

Never be afraid to ask questions.

The only stupid question is the one you don't ask.

Learning-by-doing is great. I suggest you write a short program just to learn how to use interrupts. You can use a switch or just touching a couple of wires to trigger the interrupt. An interrupt is essential if you want to detect things that repeat very quickly OR to detect very short pulses that may otherwise be missed while the Arduino is doing something else OR to detect precisely when something happens so you can time it accurately - for example to measure the width of a pulse.

...R