0

momentary button switch code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

momentary button switch code

by billbishop on Sun Jul 14, 2019 6:43 pm

Hi, I have an Arduino Uno with the Adafruit motor shield v2.3.

I have two steppers attached to the motor shield along with two button switchs'. The code I've written runs the steppers correctly, but the buttons are not in control of the motors.

Here is what I have and thanks to whoever knows!

Code: Select all | TOGGLE FULL SIZE

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

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

int resetButtonValue = HIGH;
int blackButtonValue = HIGH;
int redButtonValue = HIGH;
int redDirection = HIGH;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

void setup() {
  myMotor1->setSpeed(15);  // 10 rpm
  myMotor2->setSpeed(15);

  pinMode(RESET_BUTTON_PIN, INPUT);
  pinMode(BLACK_BUTTON_PIN, INPUT);
  pinMode(RED_BUTTON_PIN, INPUT);
 
  AFMS.begin();
 
  analogReference(DEFAULT);
  delay(15);

}

void loop() {

  blackButtonValue = analogRead(BLACK_BUTTON_PIN);

  if (blackButtonValue = LOW);
  {
    myMotor1->step(56, FORWARD, SINGLE);
    delay(2);
    myMotor2->step(1036, FORWARD, SINGLE);
    delay(2);

  }
}

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

Re: momentary button switch code

by kcl1s on Sun Jul 14, 2019 7:10 pm

You have several things that need to be addressed here.

Buttons are considered digital inputs so should be read with a digital read. blackButtonValue = digitalRead(BLACK_BUTTON_PIN);

Even though you are using the analog pins they work as digital inputs too. When you do the analog read blackButtonValue will have a value 0 - 1024. Only a value of 0 will be interpreted as LOW.

In this line if (blackButtonValue = LOW); you are using the single = . This assigns a value. You want to use the double == to do a comparison operation.

You do not show your wiring but make sure you use pull down resistors on your button circuits to keep the pins from floating when the buttons are open.

Hope this helps
Fellow hobbyist
Keith

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

Re: momentary button switch code

by billbishop on Sun Jul 14, 2019 7:30 pm

Thanks, I made those changes but am unsure of how to add a pull down resistor for the pins...and it looks like I'll need them -

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

Re: momentary button switch code

by adafruit_support_bill on Sun Jul 14, 2019 8:18 pm

The Arduino UNO has built-in pullup resistors. If you are using an UNO, the simplest thing is to wire your switches between the GPIO pin and GND, then enable the internal pull-up resistors via PinMode: https://www.arduino.cc/reference/en/lan ... o/pinmode/

Otherwise, you will need to add external resistors: https://www.instructables.com/id/Unders ... h-Arduino/

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 3:58 pm

Here is my set up, the buttons operate via key fob wireless....I put a 5.1k resistor between each button pin and ground...still no results -
Attachments
this.jpg
this
this.jpg (529.82 KiB) Viewed 217 times

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

Re: momentary button switch code

by adafruit_support_bill on Mon Jul 15, 2019 4:19 pm

Your buttons are not even connected to the Arduino. Your interface to the Arduino appears to be an RF receiver board. That has logic level outputs, so pullups/pulldowns are not needed.

We can't see much detail in the photos and it is not clear what you are using for a transmitter. But you need to debug that interface first. Make sure that pressing a button on the transmitter causes the associated pin on the receiver to go high.

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 6:15 pm

Thanks, the pins do show HIGH when a button is pressed, so I'm also wondering about the code I wrote, which I've already posted.

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

Re: momentary button switch code

by adafruit_support_bill on Mon Jul 15, 2019 6:23 pm

See Keith's response above. You need to do a digitalRead() on those pins.

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 6:54 pm

Yes, I had already made those changes last night.

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

Re: momentary button switch code

by adafruit_support_bill on Mon Jul 15, 2019 7:04 pm

Please post your modified code.

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 7:09 pm

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);

void setup() {

  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() {

  blackButtonValue = digitalRead(BLACK_BUTTON_PIN);

  if (blackButtonValue == HIGH);

  myMotor1->step(100, FORWARD, SINGLE);
  delay(2);
  myMotor2->step(1024, FORWARD, SINGLE);
  delay(2);

}

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 7:10 pm

the code is truncated at the end to just include the black button operations

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

Re: momentary button switch code

by adafruit_support_bill on Mon Jul 15, 2019 8:16 pm

This statement does not do anything. It ends at the semicolon:

Code: Select all | TOGGLE FULL SIZE
  if (blackButtonValue == HIGH);


If you want to do something based on the state of the button, you need to do in in the scope of the 'if' statement. You can define the scope of the 'if' statement with curly brackets as below:

Code: Select all | TOGGLE FULL SIZE
  if (blackButtonValue == HIGH)
  {
       // do something here
  }


https://www.tutorialspoint.com/cplusplu ... tement.htm

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

Re: momentary button switch code

by billbishop on Mon Jul 15, 2019 8:32 pm

Arduino: 1.8.9 (Windows XP), Board: "Arduino/Genuino Uno"

C:\Program Files\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files\Arduino\hardware -tools C:\Program Files\Arduino\tools-builder -tools C:\Program Files\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files\Arduino\libraries -libraries C:\Documents and Settings\b\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=0X2341_0X0043 -ide-version=10809 -build-path C:\DOCUME~1\b\LOCALS~1\Temp\arduino_build_999425 -warnings=all -build-cache C:\DOCUME~1\b\LOCALS~1\Temp\arduino_cache_710868 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=C:\Program Files\Arduino\hardware\tools\avr -verbose C:\Documents and Settings\b\Desktop\July14\July14.ino
C:\Program Files\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files\Arduino\hardware -tools C:\Program Files\Arduino\tools-builder -tools C:\Program Files\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files\Arduino\libraries -libraries C:\Documents and Settings\b\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=0X2341_0X0043 -ide-version=10809 -build-path C:\DOCUME~1\b\LOCALS~1\Temp\arduino_build_999425 -warnings=all -build-cache C:\DOCUME~1\b\LOCALS~1\Temp\arduino_cache_710868 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=C:\Program Files\Arduino\hardware\tools\avr -verbose C:\Documents and Settings\b\Desktop\July14\July14.ino
Using board 'uno' from platform in folder: C:\Program Files\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp" -o nul
"C:\\Program Files\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files\\Arduino\\libraries\\Adafruit_Motor_Shield_V2_Library-1.0.5" "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp" -o nul
"C:\\Program Files\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files\\Arduino\\libraries\\Adafruit_Motor_Shield_V2_Library-1.0.5" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp" -o nul
Using cached library dependencies for file: C:\Program Files\Arduino\libraries\Adafruit_Motor_Shield_V2_Library-1.0.5\Adafruit_MotorShield.cpp
Using cached library dependencies for file: C:\Program Files\Arduino\libraries\Adafruit_Motor_Shield_V2_Library-1.0.5\utility\Adafruit_MS_PWMServoDriver.cpp
Using cached library dependencies for file: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.cpp
Using cached library dependencies for file: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire\src\utility\twi.c
Generating function prototypes...
"C:\\Program Files\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files\\Arduino\\libraries\\Adafruit_Motor_Shield_V2_Library-1.0.5" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp" -o "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Program Files\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Program Files\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files\\Arduino\\libraries\\Adafruit_Motor_Shield_V2_Library-1.0.5" "-IC:\\Program Files\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp" -o "C:\\DOCUME~1\\b\\LOCALS~1\\Temp\\arduino_build_999425\\sketch\\July14.ino.cpp.o"
C:\Documents and Settings\b\Desktop\July14\July14.ino: In function 'void loop()':

July14:36:5: error: expected '(' before '{' token

if {blackButtonValue == HIGH}

^

Using library Adafruit_Motor_Shield_V2_Library-1.0.5 at version 1.0.5 in folder: C:\Program Files\Arduino\libraries\Adafruit_Motor_Shield_V2_Library-1.0.5
Using library Wire at version 1.0 in folder: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire
exit status 1
expected '(' before '{' token

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

Re: momentary button switch code

by adafruit_support_bill on Mon Jul 15, 2019 8:42 pm

Code: Select all | TOGGLE FULL SIZE
if {blackButtonValue == HIGH}

That is not at all like the code example I posted.

The curly braces don't go around the if condition. They appear after the if condition to define the block of code executed by the if.

Please read the tutorial link posted above:
https://www.tutorialspoint.com/cplusplu ... tement.htm

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

Please be positive and constructive with your questions and comments.