0

momentary button switch code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: momentary button switch code

by billbishop on Fri Jul 19, 2019 11:36 am

maybe, all of the buttons need to be bool...? or all of the buttons not...?

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by kcl1s on Fri Jul 19, 2019 1:42 pm

Thanks. Ok, now the btn is working but the redButton is doing nothing - hmmmm
and I have to keep the btn pressed for the whole move.

The guide says on this page https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/using-stepper-motors The stepping commands are 'blocking' and will return once the steps have finished. To me this means the code should take the 1024 steps before returning to the main (loop). If so you should not have to hold the button the whole time. I may be not understanding this and do not have a motor shield to test it out. Maybe the Adafruit techs will chime in.

Keith

kcl1s
 
Posts: 1248
Joined: Tue Aug 30, 2016 12:06 pm

Re: momentary button switch code

by adafruit_support_bill on Fri Jul 19, 2019 2:29 pm

Keith is correct. The motor commands are blocking and will not return until the steps are finished. While the motor is stepping, it will not be reading the buttons.

If you want to have more than one motor moving at a time, while remaining responsive to button input, you should use the AccelStepper library. See the Accel_MultiStepper example in the motor shield library:

https://github.com/adafruit/Adafruit_Mo ... tepper.ino

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: momentary button switch code

by billbishop on Fri Jul 19, 2019 2:55 pm

Thanks, but, not the problem...which is either the non boolean button works or the boolean button works; but never both. Incidently the motors are only supposed to run one at a time. The other snag is that where the boolean button works, I have to hold the button down for the entirety of its' travel - soon as I let go the motor stops

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by kcl1s on Fri Jul 19, 2019 4:25 pm

One thing to try. Your little geared stepper motors use a lot of steps per revolution. Your speed of 25 may be too much for the communication speed of the shield. Lower the speed setting to 5 and see if you have better luck.

Keith

kcl1s
 
Posts: 1248
Joined: Tue Aug 30, 2016 12:06 pm

Re: momentary button switch code

by billbishop on Fri Jul 19, 2019 5:04 pm

Interesting thing about the steppers; one is the usual 5v and the other is 12v. - but they are both 28BYJ-48. In running through the stepper examples and tutorials, the first discovery was that only 1024 steps would give me 1/2 revolution, no matter what number I put in setup for steps/revolution, 32, 64, 2048 or whatever.

I have 12 volts going into both the uno and the motor shield. Everything I'm doing is within all the hardware's specs, motors, boards etc...

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by adafruit_support_bill on Sat Jul 20, 2019 7:09 am

Thanks, but, not the problem...which is either the non boolean button works or the boolean button works;

Not sure what you mean by 'boolean button'. If you swap the motors, does the problem follow the motor or stay with the button?

Interesting thing about the steppers; one is the usual 5v and the other is 12v. - but they are both 28BYJ-48

Yes. There are lots of variations on that model number produced by different manufacturers.

I have 12 volts going into both the uno and the motor shield. Everything I'm doing is within all the hardware's specs, motors, boards etc...

Sounds like you are powering the 5v motor with 12v. That's not within spec. It will probably run for a while - but it will run quite hot and probably not last long.

In running through the stepper examples and tutorials, the first discovery was that only 1024 steps would give me 1/2 revolution, no matter what number I put in setup for steps/revolution, 32, 64, 2048 or whatever.

The steps/revolution is only for calculating speed. The number of steps always determines how far it turns.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: momentary button switch code

by billbishop on Sat Jul 20, 2019 11:56 am

adafruit_support_bill wrote:
Thanks, but, not the problem...which is either the non boolean button works or the boolean button works;

Not sure what you mean by 'boolean button'. If you swap the motors, does the problem follow the motor or stay with the button?

The "red" button has no Bool code and "btn" button does


I have 12 volts going into both the uno and the motor shield. Everything I'm doing is within all the hardware's specs, motors, boards etc...


Sounds like you are powering the 5v motor with 12v. That's not within spec. It will probably run for a while - but it will run quite hot and probably not last long.

The 5v motor only turns 12 - 13 degrees on button press. I have been testing this setup for the past week with the motors remaining cool always. The power in to the shield is 12v for the 12v motor while the power in to the uno is split between board and motor. I've had no problems with the motors at all. I have been thinking about lowering the input voltage to the uno to 9v..but there is no reason yet for the change. I suspect that the 5v motor is drawing from the uno power because of the 12v motor.

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by adafruit_support_bill on Sat Jul 20, 2019 11:59 am

I suspect that the 5v motor is drawing from the uno power because of the 12v motor.

Motor power will be the same for both motors. If you are supplying 12v via VIN or the external power terminals, both motors will be seeing 12v.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: momentary button switch code

by billbishop on Sat Jul 20, 2019 12:45 pm

The purpose of my post is to discuss code. Whatever insight you have would be appreciated -

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by kcl1s on Sat Jul 20, 2019 3:22 pm

billbishop wrote:The purpose of my post is to discuss code. Whatever insight you have would be appreciated -

I find the best way to troubleshoot code is to reduce it to simple pieces and get them working then combine them together. Another thing to do is to make good use of serial prints to nail down what your code is actually doing.

You said you had the red button code working before you added the forward / reverse code so lets concentrate on just the second code. Below is code for that. I reduced the speed to 5, I took out all the code that the second button did not need, added a 100 ms delay and I added serial prints to see how the code is executing. Like I said before the code compiles but I do not have the hardware to test it.

Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_MotorShield.h>
//#include <Adafruit_MS_PWMServoDriver.h>

const int btn = A3; //button on pin 2

bool dirState = LOW;
bool btnState = LOW;
bool oldState = LOW;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();


Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(2048, 2);

void setup() {
  Serial.begin(9600);
  AFMS.begin();

  myMotor2->setSpeed(5);


  pinMode(btn, INPUT);

}

void loop() {
  btnState = digitalRead(btn);
  Serial.print(btnState);
  Serial.print("   ");
  Serial.println(millis());
  if (btnState == HIGH && oldState == LOW)
  {
    Serial.println("New Press");
    dirState = !dirState; //toggle direction each press
    if (dirState)      //if direction state = true
    {
      myMotor2->step(1024, BACKWARD, SINGLE);
    }
    else               //direction state = false
    {
      myMotor2->step(1024, FORWARD, SINGLE);
    }
  }
  oldState = btnState;
  delay(100);
}


Let us know how it works and what the serial monitor shows
Keith

kcl1s
 
Posts: 1248
Joined: Tue Aug 30, 2016 12:06 pm

Re: momentary button switch code

by billbishop on Sun Jul 21, 2019 10:58 am

I have already separated out first the red button then the btn button and as I said, both worked fine when only one at a time was tested. Both together create problems as I reported. That is why I suspect there is incompatibility between the two approachs - Boolean and not Boolean.

I'm sure there is another way to do a toggle without going Boolean, I've used it before and am trying to find it so I can use it again because it worked for a very similar situation.

I will say this again...the hardware, including the motors is ok. I keep testing to make sure of this.

The voltage I have coming in to each board was researched and tested..there is no problem there.

The code is the problem and only in reference to the button operations.

Thanks!

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Re: momentary button switch code

by kcl1s on Sun Jul 21, 2019 2:26 pm

I'm sure there is another way to do a toggle without going Boolean, I've used it before and am trying to find it so I can use it again because it worked for a very similar situation.


OK sounds like you got a plan to figure it out.

Good luck
Keith

kcl1s
 
Posts: 1248
Joined: Tue Aug 30, 2016 12:06 pm

Re: momentary button switch code

by billbishop on Wed Aug 21, 2019 5:03 pm

Here is a sketch that works with my set up -

Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_MotorShield.h>
#include <Adafruit_MS_PWMServoDriver.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

const int BLACK_BUTTON_PIN = A0;
const int RED_BUTTON_PIN = A2;

int blackButtonState = LOW;
int redButtonState = LOW;
int redButtonDirection = 0;

Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(2048, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(2048, 2);

void setup() {
  AFMS.begin();

  pinMode(RED_BUTTON_PIN, INPUT);
  pinMode(BLACK_BUTTON_PIN, INPUT);

  myMotor1->setSpeed(15);
  myMotor2->setSpeed(15);
}

void loop() {
  blackButtonState = digitalRead(BLACK_BUTTON_PIN);

  if (blackButtonState == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(2);

    myMotor2->step(1024, FORWARD, DOUBLE);
    delay(2);
  }
  redButtonState = digitalRead(RED_BUTTON_PIN);
  if (redButtonState == HIGH)
  {
    myMotor2->step(1024, BACKWARD, DOUBLE);
    delay(2);
  }
  redButtonState = digitalRead(RED_BUTTON_PIN);
  if (redButtonState == HIGH)
  {
    myMotor2->step(1024, FORWARD, DOUBLE);
    delay(2);
  }

}

billbishop
 
Posts: 44
Joined: Sun Jun 16, 2019 12:55 pm

Please be positive and constructive with your questions and comments.