Programming Stepper with microswitch

Question:

I am trying to program a stepper to reverse direction after a micro switch is activated. I believe I'm very close, but not close enough. I'm now stumped. See each code attached.

change_mod is simply a modified code for latching a micros witch. It works perfectly.

Stepper_with_micro_TEST is a debounce, stepper code spliced with the change_mod code. It should work as well. BUT, it doesn't

I isolated the problem to the if statement. ..........if ( debouncer.fell() )

If this line is omitted, the program runs without any micro switch function. No errors in code.

Can anyone figure this out? I'm stumped!!

change_mod.ino (867 Bytes)

Stepper_with_micro_TEST.ino (1014 Bytes)

paloian:
Can anyone figure this out? I'm stumped!!

your code doesn't compile because this

digitalWrite (SWITCH_PIN2,HIGH);

is outside a function.

Post your code that worked

You are correct. Unfortunately, I uploaded the wrong rev. That line was omitted because code never ran.

Assume that line has been removed and motor still doesn't run. I attached the most recent code that doesn't work.

Stepper_with_micro_TEST.ino (981 Bytes)

BTW, thank you for your quick response. Some more info. I think the state of pin 2 should be low according to the code. After code is uploaded to the board, I measured the voltage between pin 2 and ground to be 5V. If I press the micro switch, pin 2 goes low, but the motor still doesn't run and pin 2 doesn't latch.

Note: change_mod does latch and work for an LED. Both codes are identical except for one thing........
Stepper micro TEST code has three pins and code for a stepper motor vs. LED.

Also note I've verified the stepper motor code is good and works by itself.

Thanks again

paloian:
BTW, thank you for your quick response. Some more info. I think the state of pin 2 should be low according to the code. After code is uploaded to the board, I measured the voltage between pin 2 and ground to be 5V. If I press the micro switch, pin 2 goes low, but the motor still doesn't run and pin 2 doesn't latch.

Note: change_mod does latch and work for an LED. Both codes are identical except for one thing........
Stepper micro TEST code has three pins and code for a stepper motor vs. LED.

Also note I've verified the stepper motor code is good and works by itself.

Thanks again

you aren't trying to power your servo with the output from the Arduino pins, right?

No, I have a stepper driver TB6600. All it requires is a signal from Arduino.

Looks like your code will only make the motor step 1 time in the opposite direction with each button press, back and forth 1 step.

WOW!! Excellent. yes, I confirmed you are correct. I'm pressed.

Now, How do I correct this problem?

Thanks

Mike

Why does the change_mod code (written for LED) latch the MS(micro swtich) and it won't latch the MS when slightly modified for a stepper? both code are virtually identical.

You are sending the LED an alternating on/off state, you're sending the stepper a 50 uS pulse, on then off.

I partially understand. However, based on your reply, this will never work. I've read about counters, boolean commands and a few other techniques to latch the MS state. I haven't found one that works properly.

Any recommendations?

Again, many thanks, you've been very helpful.

I found a latching code program which I think will work with some modification. I want to thank all of you. If I have a problem, I'll post it again.

Try this:

void loop() {

 // Update the Bounce instance :
   debouncer.update();
   
   // Call code if Bounce fell (transition from HIGH to LOW):
   if ( debouncer.fell() ){

 // Toggle stepper state :
     stepperState = !stepperState;
     digitalWrite(DIR,stepperState);
     delay(500);
   } 
      digitalWrite(ENA,HIGH);
      digitalWrite(PUL,HIGH);
      delayMicroseconds(10); // pulse length, leave fixed
      digitalWrite(PUL,LOW);
      delayMicroseconds(2000); // interval between pulses, controls speed
   
   
  }

I just saw this. Yes, I will try it and let you know how it work out.

I am very grateful for your help.

Mike

Mr. Outsider.............You are a genius!!

Many, many thanks............It works.

BTW, after your last comment I was just beginning to make the simple change that you made. But you saved me lots of time. I knew I was close.

Thanks

Mike

if anyone wants this code, let me know and I'll upload it.