2

am a complete beginner at coding with Arduino, although I have used them for a few years by copying other peoples code. I only understand a tiny fraction of the layout of a sketch and would really appreciate some help to solve, what I believe, is a simple problem.

I have a sketch to control the speed and direction of a stepper motor:-

/*
  Stepper Motor Test
  stepper-test01.ino
  Uses MA860H or similar Stepper Driver Unit
  Has speed control & reverse switch

DroneBot Workshop 2019 https://dronebotworkshop.com */

// Defin pins

int reverseSwitch = 2; // Push button for reverse int driverPUL = 7; // PUL- pin int driverDIR = 6; // DIR- pin int spd = A0; // Potentiometer

// Variables

int pd = 500; // Pulse Delay period boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

setdir = !setdir;

}

void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode (driverPUL, OUTPUT); pinMode (driverDIR, OUTPUT); attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

}

void loop() {

pd = map((analogRead(spd)),0,1023,1000,150);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
digitalWrite(LED_BUILTIN, HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);

}

This works perfectly with my stepper driver and stepper motor with good speed control. However, the author did warn that debounce might be a problem and this has proved to be the case. I understand the principal of debounce and I have studied a number of debounce sketches and can also understand the method of reduction using a few milliseconds delay. I believe that the following code is as simple as it can get but I have no idea how to incorporate this in the above sketch.

bool debounce() {
  static uint16_t state = 0;
  state = (state<<1) | digitalRead(btn) | 0xfe00;
  return (state == 0xff00);
}

(The author appears to have split the code into two parts (I think)

#define btn 2  //assuming we use D2 on Arduino

void setup() { pinMode(btn, INPUT_PULLUP); pinMode(LED_BUILTIN, OUTPUT); }

void loop() { if (debounce()) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } }

Can anyone look at these sketches and advise me if a) they are compatible and b) how do incorporate the debounce code into the working sketch?

Thanks in advance from a complete coding numptee (Scottish expression!)

chrisl
  • 16,622
  • 2
  • 18
  • 27
Solmod
  • 23
  • 3

2 Answers2

0

You can try replacing the function revmotor() with this code. That is instead of using the separate debounce code that you have found. It works by locking out presses of the switch for 100ms following the last press.

void revmotor (){
  static uint32_t lastSwitchAtMs = 0 ;
  if ( millis() - lastSwitchAtMs > 100UL ) {  // debounce 100ms
    setdir = !setdir;  
  }
  lastSwitchAtMs = millis() ; 
}

Note that the variable setdir should really be declared so volatile boolean setdir = LOW; // Set Direction

EDIT:

Incidentally, found the article that you followed here: https://dronebotworkshop.com/big-stepper-motors/ which includes the code and a schematic diagram.

6v6gt
  • 1,192
  • 6
  • 8
0

I believe that the debounce() function and example was from me that I described in my blog post The simplest button debounce solution, in fact I was not the one who first write it, I adapted it from Jack Gansslle's A Guide to Debouncing - Part 2 that was first published way back to 2004.

In your case, all you need to do is when revmotor() event is triggered, called the debounce function and alter the state of setdir only if debounce return true.


int reverseSwitch = 2;  // Push button for reverse

void revmotor (){

if (debounce()) setdir = !setdir;

}

bool debounce() { static uint16_t state = 0; state = (state<<1) | digitalRead(reverseSwitch) | 0xfe00; return (state == 0xff00); }

void setup() { // your pin setup code here pinMode(reverseSwitch, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING); }

I must say that I did not test this particular code before post this answer, but I've been using it in similar use case in the past many times.

hcheung
  • 1,933
  • 8
  • 15