Button Motor Control Issues

Hello,

I am currently having problems with school project I am working on. The project consists mainly of a motor and 2 momentary pushbuttons, plus other electrical components to handle the load of the motor. The push buttons are suppose to perform a specific motor function when pressed. However, when I plug my arduino Uno in, all I get is the first motor function (i.e. first button press function) to continuously cycle, even if no button is pressed. If anyone could help me get these buttons to work right it would be greatly appreciated. Thanks

(BTW I'm sort of new with the Arduino coding environment)

Tyler

MechanoReceptorStimulationDevice.ino (1.98 KB)

The first thing people in here will tell you is you need to post your code or nobody can help.
I thought it was just people being arrogant but I understand it now.

I have my code posted as an attachment, I'm not quite sure how to post my code like everyone else does haha, sorry its my first time posting to the Forum.

On the header of the reply input you will see a hashtag #
Click it

OOps, nevermind this post

Thank you for pointing that out. Now attached properly is my code :slight_smile:

const int motorPin = 9;
const int delaytime = 100;
const int buttonPin1 = 2;
const int buttonPin2 = 3;
int buttonState1 = 0;
int buttonState2 = 0;

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  
  Serial.println(buttonState1);
  
  if (buttonState1 == HIGH) {
    
     analogWrite(motorPin,  220);
       delay(delaytime/5);
     analogWrite(motorPin,  30);
       delay(delaytime);
     analogWrite(motorPin,  50);
       delay(delaytime);
     analogWrite(motorPin,  70);
       delay(delaytime);
     analogWrite(motorPin,  80);
       delay(delaytime);
     analogWrite(motorPin, 90);
       delay(delaytime);
     analogWrite(motorPin,  100);
       delay(delaytime);
     analogWrite(motorPin,  120);
       delay(delaytime);
     analogWrite(motorPin,  150);
       delay(delaytime);
     analogWrite(motorPin,  175);
       delay(delaytime);
     analogWrite(motorPin,  200);
       delay(delaytime);
     analogWrite(motorPin,  175);
       delay(delaytime);
     analogWrite(motorPin,  150);
       delay(delaytime);
     analogWrite(motorPin,  120);
       delay(delaytime);
     analogWrite(motorPin,  100);
       delay(delaytime);
     analogWrite(motorPin,  90);
       delay(delaytime);
     analogWrite(motorPin,  80);
       delay(delaytime);
     analogWrite(motorPin, 50);
       delay(delaytime);
     analogWrite(motorPin,  30);
       delay(delaytime);
     analogWrite(motorPin, 0);
       delay(delaytime*10);
  }
  
    else if (buttonState2 == HIGH) {
      
     analogWrite(motorPin,  220);
       delay(delaytime/5);
     analogWrite(motorPin,  100);
       delay(delaytime*5);
     analogWrite(motorPin, 80);
       delay(delaytime);
     analogWrite(motorPin,  50);
       delay(delaytime);
     analogWrite(motorPin, 0);
       delay(delaytime*10);
  }
  
    else {
    analogWrite(motorPin, 0);
  }
  
}

You are aware, aren't you, that a "delay" stops the entire board from doing anything while the delay is in effect, correct?

So pressing the other button, while the first button code is running will produce no results.

That's my noob perspective

How are your switches wired?

If you don't have external pull-up resistors change these 2 lines.
pinMode(mechrep1Pin, INPUT_PULLUP);
pinMode(mechrep2Pin, INPUT_PULLUP);

Bittsen~

So are you saying that I should delete the lines that say delay(delay time*10)?

LarryD~

I have the switches wired with external pull-up resistors. One terminal of the switch is wired to 5V, while the other is wired to the digital pin on the arduino and a resistor to ground. So when the button is pressed, 5V should be read.

TyNo:
Bittsen~

So are you saying that I should delete the lines that say delay(delay time*10)?

LarryD~

I have the switches wired with external pull-up resistors. One terminal of the switch is wired to 5V, while the other is wired to the digital pin on the arduino and a resistor to ground. So when the button is pressed, 5V should be read.

I'm not saying that at all. I'm just letting you know that delays aren't benign. They have a global effect.

Yeah as Larry just posted, we's need to see the wiring.

Ok you posted in the meantime that you have pullUPs?- if so the pin will be high until you press the button... Post your wiring...

I see you have a Serial.print for button 1.... what does it say? Because if you have the wiring such that button1 is high even when it's not pressed, then that would explain why it's working thru the code like it is.

Suggestion: put Serial.prints inside each leg of the "if", that way you'll see where the flow is going.

if (buttonState1 == HIGH) {
    Serial.println("I'm in the button 1 high leg.....");     //<<<<<<<<<<<<<<<<<<, new
     analogWrite(motorPin,  220);
       delay(delaytime/5);

//

else if (buttonState2 == HIGH) {
      Serial.println("I'm in the button 2 high leg.....");     //<<<<<<<<<<<<<<<<<<, new
     analogWrite(motorPin,  220);
       delay(delaytime/5);
     analogWrite(motorPin,  100

I have the switches wired with external pull-up resistors. One terminal of the switch is wired to 5V, while the other is wired to the digital pin on the arduino and a resistor to ground. So when the button is pressed, 5V should be read.

Not following what you a saying.

Is it ?
+5V----/////----Switch contact ------Switch contact, arduino pin, ------//////------ground

Ok so I attached a rough sketch of how I got the switches wired.

Can you try a 10K instead of 100K?
Does the switch close or open when you press it?

The switch closes when pressed. And I will try swapping the 100ks for some 10ks and see whats happens.

TyNo:
The switch closes when pressed. And I will try swapping the 100ks for some 10ks and see whats happens.

In which case those are pulldowns, not ups, and both switches should read low, so yep let's see what the change in resistor does.

But while you're at it, put debug prints in the if legs....

Servo test code with a button setup that does not use resistors. You can try the code without servos and see if you get output to the serial monitor when the button pins are connected to ground. Connect a wire to ground and then touch wire to pins 5 and 6 to test.

//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 6; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  servo1.write(pos); //starting position
  digitalWrite(6, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
  Serial.println("servo button sweep test 12-23-2013");
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    pos=(pos+1);
    if(pos>180) pos=180; //limit upper value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement 
  }    

  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    pos=(pos-1);
    if(pos<0) pos=0; //limit lower value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement
  }
}

I changed the 100k resistors to 10k but still nothing. The value of the button state is not displaying in the serial monitor either.... This is frustrating because I had it working perfectly two nights before and now it doesn't work....

Hahahahha figured out the problem, most rookie mistake ever. I first upload the code onto an Arduino Micro and never switch the board to the Uno before uploading the sketch so the proper code was never even running! Man i feel dumb haha. Thanks to everyone in this posting who answered and helped out. I greatly appreciate everything from everyone!!

Tyler