Marija 0 Posted March 8, 2016 Share Posted March 8, 2016 Hi all, I'm relatively new to microcontroller programming and I'm having a few problems with a current project that I would appreciate help with. I'm using MSP430G2553 and Energia to control the speed of two vibromotors. To this I use three pushbuttons, one to change from one motor to another and two more(up and down) to increase/decrease the speed. I have 3 interrupts to do all this and I copied the code below. The program works mostly but sometimes it acts randomly, for example, increasing the speed when I press the decrease button, or not responding at all. My questions are these, they are quite simple but I am struggling quite a lot and I want to understand the basics good: 1). Am I using the interrupts correctly? Do I need to put all of them in the setup routine? Also, I have too much code in the interrupt functions, but not sure how to fix this. Should I just poll the flags in the functions and do the motor speed updating in the loop routine? Basically, and help and comments regarding the interrupts would be very helpful. 2). How do I use switch debouncing for more then one switch? I used the Energia example previously to debounce one switch but it seems complicated using that for 3 switches. int reading = digitalRead(buttonPin);if (reading != lastButtonState) {lastDebounceTime = millis();}if ((millis( ) - lastDebounceTime) > debounceDelay) {buttonState = reading;} My current code: up_down_test.ino Thank you a lot in advance! Quote Link to post Share on other sites
Shiv 0 Posted March 8, 2016 Share Posted March 8, 2016 Have you looked at/tried de-bouncing using an RC network? If you're looking to implement this only in software, try, increasing the de bounce period (debounceDelay) to around 30-50 milliseconds. Quote Link to post Share on other sites
Fmilburn 446 Posted March 8, 2016 Share Posted March 8, 2016 Hi @Marija A couple of thoughts and questions about what you are trying to do: There is the following comment in your code , but then you fail to use INPUT_PULLUP in the call to pinMode. This looks like it might be the problem - try fixing it first. /* Enable internal pullup. * Without the pin will float and the example will not work */ What is the purpose of the variable called flag? I can't see that you are doing anything useful with it attachInterrupts is in the right place (i.e. setup) since you are setting it once and aren't changing it. I don't think the length of the ISR is the problem here although you are correct it should be minimized. Have you tried using serial print to the monitor to debug / see what state you are in and how it is changing? It might capture bouncing of the switches that your LED might miss You aren't using the variables lastDebounceTime and debounceDelay. This is confusing to someone trying to help debug your code. It is not that much more complicated to debounce 3 switches. Try adding it next if you still have a problem after enabling the pullup. Marija 1 Quote Link to post Share on other sites
Marija 0 Posted March 8, 2016 Author Share Posted March 8, 2016 Hi @Marija A couple of thoughts and questions about what you are trying to do: There is the following comment in your code , but then you fail to use INPUT_PULLUP in the call to pinMode. This looks like it might be the problem - try fixing it first. /* Enable internal pullup. * Without the pin will float and the example will not work */ What is the purpose of the variable called flag? I can't see that you are doing anything useful with it attachInterrupts is in the right place (i.e. setup) since you are setting it once and aren't changing it. I don't think the length of the ISR is the problem here although you are correct it should be minimized. Have you tried using serial print to the monitor to debug / see what state you are in and how it is changing? It might capture bouncing of the switches that your LED might miss You aren't using the variables lastDebounceTime and debounceDelay. This is confusing to someone trying to help debug your code. It is not that much more complicated to debounce 3 switches. Try adding it next if you still have a problem after enabling the pullup. Thank you for the help @Mention ! I didn't use INPUT_PULLUP but instead just pinMode(UP, INPUT), becaus ethe Energia documentation says I should use just INPUT if it is for external push button, not the on board one. Yes true, I don't use the flag to do anything useful, I just realized that. So basically if I leave my ISRs as they are, I won't really have code in the loop routine? Apart from printing on the serial monitor. I will try that next. I'm not completely sure how to do the debouncing. Because I don't read switch state anywhere else in the code, just in attachInterrupt. Should I read the state of each switch in the loop function and debounce each there? Or should I put it in the setup function? I know this seems basic, but I'm still a bit puzzled with the interrupts. Quote Link to post Share on other sites
abecedarian 330 Posted March 8, 2016 Share Posted March 8, 2016 @@Marija With an analog input, you likely wouldn't want to set a pull-up or pull-down state. However, with digital inputs, and without setting the input as INPUT_PULLUP or INPUT_PULLDOWN in the sketch or using an external pull-up or pull-down resistor, the input pin will float, as in won't have a default state, and this can cause issues like false or missed triggers. So, if the switch connects to ground when pushed, either set internal pull-up in the sketch or provide an external resistor (10K should work) pull-up to VCC. If the switch connects to VCC when pushed, either set internal pull-down in the sketch or provide an external weak pull-down resistor to ground. Marija 1 Quote Link to post Share on other sites
Marija 0 Posted March 9, 2016 Author Share Posted March 9, 2016 @@Marija With an analog input, you likely wouldn't want to set a pull-up or pull-down state. However, with digital inputs, and without setting the input as INPUT_PULLUP or INPUT_PULLDOWN in the sketch or using an external pull-up or pull-down resistor, the input pin will float, as in won't have a default state, and this can cause issues like false or missed triggers. So, if the switch connects to ground when pushed, either set internal pull-up in the sketch or provide an external resistor (10K should work) pull-up to VCC. If the switch connects to VCC when pushed, either set internal pull-down in the sketch or provide an external weak pull-down resistor to ground. Sorry I forgot to mention, but I do use external 10k pull-up resistor and it seems to work fine with it. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.