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 Tue Jul 16, 2019 10:52 am

Ok, that helps...note, I checked voltage with my tester at the pins I use on the wireless receiver and also the motorshield analog pins I'm using. Although I got a reading of 4.8v - 4.95v, it took a while for the tester to show it, (2 - 3 seconds of holding the button down) and before and after I got the right reading my tester was all over the place. I'll change the code to focus the serial monitor on the black button...

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

Re: momentary button switch code

by billbishop on Tue Jul 16, 2019 11:02 am

Here is my code and a jpg of the readout:

Code: Select all | TOGGLE FULL SIZE

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

const int RESET_BUTTON_PIN = A1;
const int RED_BUTTON_PIN = A2;
const int BLACK_BUTTON_PIN = A3;

int resetButtonValue = LOW;
int blackButtonValue = LOW;
int redButtonValue = LOW;
int redDirection = -1;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

int analogValue = 0;

void setup() {

  Serial.begin(9600);

  AFMS.begin();

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

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

}

void loop() {
  analogValue = analogRead(3);

  Serial.println(analogValue);

  delay(10);

  blackButtonValue = digitalRead(BLACK_BUTTON_PIN);
  Serial.println(blackButtonValue);
  if (blackButtonValue == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(2);
    myMotor2->step(1024, FORWARD, SINGLE);
    delay(2);

  }
}
Attachments
serialreadoutpinA3.jpg
serialreadoutpinA3.jpg (263.8 KiB) Viewed 189 times

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

Re: momentary button switch code

by adafruit_support_bill on Tue Jul 16, 2019 11:20 am

Code: Select all | TOGGLE FULL SIZE
  analogValue = analogRead(3);

  Serial.println(analogValue);

  delay(10);

  blackButtonValue = digitalRead(BLACK_BUTTON_PIN);
  Serial.println(blackButtonValue);


You still have the analog values from pin A0 printing out. These will be interleaved with the digital values from A3.

Since the digital values can only be 0 or 1, It looks like you are getting all zeros from your button pin.

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

Re: momentary button switch code

by billbishop on Tue Jul 16, 2019 11:33 am

Agreed

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

Re: momentary button switch code

by billbishop on Tue Jul 16, 2019 11:35 am

the buttons seem fine, but the motor shield has seen a lot of rough action of various kinds, I have a new one here and am about to swap it in. Thanks for your input! I'll send results after shield replacement.

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

Re: momentary button switch code

by billbishop on Wed Jul 17, 2019 10:45 am

So yes, I swapped in the new motor shield and everything works as intended...my thanks for all the tidbits of help on the way!

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

Re: momentary button switch code

by adafruit_support_bill on Wed Jul 17, 2019 11:16 am

Good to hear that worked for you. Thanks for the follow-up.

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

Re: momentary button switch code

by billbishop on Wed Jul 17, 2019 9:19 pm

And now to program button #2, hardware includes the arduino uno stacked with adafruit motor shield, 2 - 28BYJ-48 steppers; one 5v and the other 12v, two buttons connected to the motor shield via a wireless key fob and 2 12v inputs; one to the motor shield and the other to the uno. My comment is at the bottom.

Code: Select all | TOGGLE FULL SIZE

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

const int RESET_BUTTON_PIN = A1;
const int RED_BUTTON_PIN = A2;
const int BLACK_BUTTON_PIN = A3;


int resetButtonValue = LOW;
int redButtonValue = LOW;
int blackButtonValue = LOW;
int blackDirection = 0;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

void setup() {
  AFMS.begin();

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

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

}

void loop() {
  redButtonValue = digitalRead(RED_BUTTON_PIN);
  blackButtonValue = digitalRead(BLACK_BUTTON_PIN);
 
  if (redButtonValue == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(10);
    myMotor2->step(1024, FORWARD, SINGLE);
    delay(2);

  }

  if (blackButtonValue == HIGH)

  //Here is where I want to press the button once and move 1/2 rotation backwards,
  //but then, on the next button press move 1/2 rotation forward...and so on back and forth
  //with a button press in between
  {
    myMotor2->step(1024, BACKWARD, SINGLE);
  }
  else
  {
    myMotor2->step(1024, FORWARD, SINGLE);
  }
}
Attachments
IMG_20190717_203539.jpg
IMG_20190717_203539.jpg (797.6 KiB) Viewed 129 times

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

Re: momentary button switch code

by kcl1s on Wed Jul 17, 2019 10:35 pm

//Here is where I want to press the button once and move 1/2 rotation backwards,
//but then, on the next button press move 1/2 rotation forward...and so on back and forth
//with a button press in between


To do that you have to account for button releases also using button state code. This way your code only runs once for each button press. Here is some example code you can incorporate into your sketch.

Code: Select all | TOGGLE FULL SIZE
const int btn = 2; //button on pin 2

bool btnState = LOW;
bool oldState = LOW;

void setup() {
  // put your setup code here, to run once:
  pinMode(btn, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  btnState = digitalRead(btn);
  if(btnState == HIGH && oldState == LOW) {
    //you have a new press
    //put new press code here
    //
  }
  oldState = btnState;
}

To toggle between directions you can make use of the ! (not) operator to change the value of a bool variable between true and false. Then your if statement to control direction. Something like this in the new press code area should work. Don't forget to declare the bool variable at the top of your code bool dirState = LOW;

Code: Select all | TOGGLE FULL SIZE
    dirState = !dirState; //toggle direction each press
    if (dirState)      //direction state = true
      {
      myMotor2->step(1024, BACKWARD, SINGLE);
      }
      else
      {
      myMotor2->step(1024, FORWARD, SINGLE);
      }


Keith

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

Re: momentary button switch code

by billbishop on Thu Jul 18, 2019 12:34 pm

Thanks for that, will be test driving it this afternoon, will get back to you.

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

Re: momentary button switch code

by billbishop on Thu Jul 18, 2019 4:15 pm

here is what I did...what happened is myMotor2 just kept spinning while myMotor1 never moved:

Code: Select all | TOGGLE FULL SIZE

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


bool dirState = LOW;

const int RESET_BUTTON_PIN = A1;
const int RED_BUTTON_PIN = A2;
const int btn = A3; //button on pin 2

bool btnState = LOW;
bool oldState = LOW;


int resetButtonValue = LOW;
int redButtonValue = LOW;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

void setup() {

  AFMS.begin();

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

  pinMode(RESET_BUTTON_PIN, INPUT);
  pinMode(RED_BUTTON_PIN, INPUT);
  pinMode(btn, INPUT);

}

void loop() {
  redButtonValue = digitalRead(RED_BUTTON_PIN);
  if (redButtonValue == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(10);
    myMotor2->step(1024, FORWARD, SINGLE);
    delay(2);

  }

  btnState = digitalRead(btn);
  if (btnState == HIGH && oldState == LOW)

    dirState = !dirState; //toggle direction each press
  if (dirState)      //direction state = true
  {
    myMotor2->step(1024, BACKWARD, SINGLE);
  }
  else
  {
    myMotor2->step(1024, FORWARD, SINGLE);
  }
  oldState = btnState;


}

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

Re: momentary button switch code

by billbishop on Thu Jul 18, 2019 4:45 pm

Aha! Broken wire...will retry in a bit

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

Re: momentary button switch code

by billbishop on Thu Jul 18, 2019 5:20 pm

well fixing that made no difference - still staring at the hardware

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

Re: momentary button switch code

by kcl1s on Fri Jul 19, 2019 7:49 am

The keep moving part is caused because of missing curly brackets. The brackets insure your motor code only runs on a new press. I edited your code below.

Code: Select all | TOGGLE FULL SIZE

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


bool dirState = LOW;

const int RESET_BUTTON_PIN = A1;
const int RED_BUTTON_PIN = A2;
const int btn = A3; //button on pin 2

bool btnState = LOW;
bool oldState = LOW;


int resetButtonValue = LOW;
int redButtonValue = LOW;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

void setup() {

  AFMS.begin();

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

  pinMode(RESET_BUTTON_PIN, INPUT);
  pinMode(RED_BUTTON_PIN, INPUT);
  pinMode(btn, INPUT);

}

void loop() {
  redButtonValue = digitalRead(RED_BUTTON_PIN);
  if (redButtonValue == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(10);
    myMotor2->step(1024, FORWARD, SINGLE);
    delay(2);

  }

  btnState = digitalRead(btn);
  if (btnState == HIGH && oldState == LOW)
  {
    dirState = !dirState; //toggle direction each press
    if (dirState)      //direction state = true
    {
      myMotor2->step(1024, BACKWARD, SINGLE);
    }
    else
    {
      myMotor2->step(1024, FORWARD, SINGLE);
    }
  }
  oldState = btnState;
}

Let us know if that helps
Keith

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

Re: momentary button switch code

by billbishop on Fri Jul 19, 2019 10:32 am

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.

Code: Select all | TOGGLE FULL SIZE

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




const int RESET_BUTTON_PIN = A1;
const int RED_BUTTON_PIN = A2;
const int btn = A3; //button on pin 2

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


int resetButtonValue = LOW;
int redButtonValue = LOW;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

void setup() {

  AFMS.begin();

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

  pinMode(RESET_BUTTON_PIN, INPUT);
  pinMode(RED_BUTTON_PIN, INPUT);
  pinMode(btn, INPUT);

}

void loop() {
  redButtonValue = digitalRead(RED_BUTTON_PIN);
  if (redButtonValue == HIGH)
  {
    myMotor1->step(100, FORWARD, SINGLE);
    delay(10);
    myMotor2->step(1024, FORWARD, SINGLE);
    delay(2);

  }

  btnState = digitalRead(btn);
  if (btnState == HIGH && oldState == LOW)
  {
    dirState = !dirState; //toggle direction each press
    if (dirState)      //direction state = true
    {
      myMotor2->step(1024, BACKWARD, SINGLE);
    }
    else
    {
      myMotor2->step(1024, FORWARD, SINGLE);
    }
  }
  oldState = btnState;
}

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

Please be positive and constructive with your questions and comments.