Hello, I need to count some pieces with limit switches on three places(mega2560).
What is recomendation for library that will fix debouncing in best way? I would like to have something call function in which I will count up or down depanding of input pin. I would use interupt pins. For example I found ezButton but as I saw it is not using interupt pins but any pin.
Thanks
Why do you want to use interrupt pins for mechanical switches? Usually that's not a good idea. How fast will your switches count pieces?
The MoToButtons class of my MobaTools library is optimized for many ( up to 32 ) switches/buttons per instance.
Maybe one, two per second. I thought to be sure to use inerupt pins that counting and other program run good.
In this case interrupts will create more problems than they solve. Polling the switches at a debouncing intervall ( maybe 20..50ms ) will do the job.
And writing non blocking code in loop() should be always done.
Do not use interrupts for slow mechanical switches.
Scan your switches every ~50ms and look for switch change in state.
Always write your sketches in a non blocking fashion.
consider
// check multiple buttons and toggle LEDs
enum { Off = HIGH, On = LOW };
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
byte butState [N_BUT];
// -----------------------------------------------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
byte but = digitalRead (pinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (On == but)
return n;
}
}
return -1;
}
// -----------------------------------------------------------------------------
void
loop ()
{
switch (chkButtons ()) {
case 2:
digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
break;
case 1:
digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
break;
case 0:
digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
break;
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
pinMode (pinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (pinsBut [n]);
}
for (unsigned n = 0; n < sizeof(pinsLed); n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
}
}
But would be much more easy with a lib - and really nonblocking
// check multiple buttons and toggle LEDs
#include <MobaTools.h>
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
MoToButtons myButtons( pinsBut, N_BUT, 20, 500 );
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
for (unsigned n = 0; n < N_BUT; n++) {
digitalWrite (pinsLed [n], LOW);
pinMode (pinsLed [n], OUTPUT);
}
}
// -----------------------------------------------------------------------------
void loop () {
myButtons.processButtons();
for (unsigned n = 0; n < N_BUT; n++) {
if ( myButtons.pressed(n) ) {
digitalWrite (pinsLed [n], ! digitalRead (pinsLed [n]));
Serial.print(n+1); Serial.println(". button pressed");
}
}
}
I am prepared for the flaming. There is also hardware debouncing. A 0.1uF ceramic cap across the switch is often all the debounce needed.
`Yes this works. Thnks. gcjr I didnt test yours soulution this is shorter In your library how do you choose which button is pressed? Like if I want in down code: if input 2 count1 update for example.
#include <MobaTools.h>
byte counter = 0;
byte pinsBut [] = { 2,3,4 };
#define N_BUT sizeof(pinsBut)
MoToButtons myButtons( pinsBut, N_BUT, 30, 500 );
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (250000);
}
// -----------------------------------------------------------------------------
void loop () {
myButtons.processButtons();
for (unsigned n = 0; n < N_BUT; n++) {
if ( myButtons.pressed(n) ) {
counter++;
Serial.print(n+1); Serial.println(". button pressed");
Serial.println(counter);
}
}
}
Ok, found, used part from example...
void loop() {
myButtons.processButtons();
// for (unsigned n = 0; n < N_BUT; n++) {
if (PressedEvent(0)) {
counter++;
Serial.print(1); Serial.println(". button pressed");
Serial.println(counter);
}
if (PressedEvent(1)) {
counter--;
Serial.print(2); Serial.println(". button pressed");
Serial.println(counter);
}
if (PressedEvent(2)) {
counter++;
Serial.print(3); Serial.println(". button pressed");
Serial.println("Hello");
}
}
bool PressedEvent( uint8_t buttonNbr ) {
bool state = false;
if ( myButtons.pressed(buttonNbr) ) {
state = true;
}
return state;
}
Nice.
But press event 2 should reset the counter to 0.
a7
just a test...
Hi,
You don't need that function in your case. In the end it only returns the return value uf myButtons.pressed(n). So you could also use it directly:
if (myButtons.pressed(0)) {
counter++;
Serial.print(1); Serial.println(". button pressed");
Serial.println(counter);
}
if (myButtons.pressed(1)) {
counter--;
Serial.print(2); Serial.println(". button pressed");
Serial.println(counter);
}
if (myButtons.pressed(2)) {
counter++;
Serial.print(3); Serial.println(". button pressed");
Serial.println("Hello");
}
When I use AccelStepper for stepp motor and MobaTools for buttons it doesnt compile. When I use separate it works both.
That should not interfere with each other... Can you show the sketch? As an alternative, you could control your stepper with MobaTools too ( class MoToStepper ).
I switched to MobaTools and all is ok. Code is much more same like we discus MobaTools buttons and simple AccelSteper for stepp moving. Just for info when I compile this is the error:
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
In file included from C:\Users\Milos Kovacevic\Documents\Arduino\libraries\MobaTools\src/MobaTools.h:163:0,
from C:\Users\Milos Kovacevic\Desktop\test\test.ino:7:
C:\Users\Milos Kovacevic\Desktop\test\test.ino: In function 'void setup()':
C:\Users\Milos Kovacevic\Documents\Arduino\libraries\MobaTools\src/utilities/MoToStepper.h:53:29: error: 'class AccelStepper' has no member named 'setSpeedSteps'; did you mean 'setSpeed'?
#define setMaxSpeed( speed) setSpeedSteps( speed*10 )
^
C:\Users\Milos Kovacevic\Desktop\test\test.ino:51:8: note: in expansion of macro 'setMaxSpeed'
m_1.setMaxSpeed(6000);
^~~~~~~~~~~
C:\Users\Milos Kovacevic\Documents\Arduino\libraries\MobaTools\src/utilities/MoToStepper.h:53:49: warning: integer overflow in expression [-Woverflow]
#define setMaxSpeed( speed) setSpeedSteps( speed*10 )
C:\Users\Milos Kovacevic\Desktop\test\test.ino:51:8: note: in expansion of macro 'setMaxSpeed'
m_1.setMaxSpeed(6000);
^~~~~~~~~~~
C:\Users\Milos Kovacevic\Documents\Arduino\libraries\MobaTools\src/utilities/MoToStepper.h:51:29: error: 'class AccelStepper' has no member named 'writeSteps'
#define moveTo writeSteps
^
C:\Users\Milos Kovacevic\Desktop\test\test.ino:53:8: note: in expansion of macro 'moveTo'
m_1.moveTo(2000000);
^~~~~~
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Code is:
#include <Arduino.h>
#include <AccelStepper.h>
#include <MobaTools.h>
byte counter = 0;
byte pinsBut [] = { 2,3,4 };
#define N_BUT sizeof(pinsBut)
MoToButtons myButtons( pinsBut, N_BUT, 30, 500 );
//Pinovi Motor_1 Traka 1
#define dirP_1 31
#define runP_1 33
#define stepP_1 13
#define motorInterfaceType 1 // Define motor interface type
AccelStepper m_1(motorInterfaceType, stepP_1, dirP_1); // Creates an instance
void setup() {
// set the maximum speed, acceleration factor, initial speed and the target position
m_1.setMaxSpeed(6000);
m_1.setAcceleration(100);
m_1.moveTo(2000000);
}
void loop() {
myButtons.processButtons();
if (myButtons.pressed(0)) {
counter++;
Serial.print(1); Serial.println(". button pressed");
Serial.println(counter);
}
if (myButtons.pressed(1)) {
counter--;
Serial.print(2); Serial.println(". button pressed");
Serial.println(counter);
}
if (myButtons.pressed(3)) {
Serial.print(3); Serial.println(". button pressed");
Serial.println("Hello");
}
m_1.run();
}
Ups - Indeed seems you found an error with an incompatibility between AccelStepper and MobaTools if both are included. I have to have a closer look at this.
Thanks for improving MobaTools
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.