Servo and DC Motor Toggle

Hi, I have started a new project which invloves a servo, a DC motor, one potentiometer and 2 pushbuttons. Initially only the servo should start, whose speed is controlled by the potentiometer. When I press the pushbutton, the servo should stop and the DC motor should start, with it's speed controlled by the same potentiometer and it's direction by the second pushbutton. Instead of this on start only the DC motor powers up and rest of them don't work. I am not understanding what is wrong with my code. Kindly help.

#include <Servo.h>
#define potPin A0
int pos = 0;
int val = 0;
#define button 7
#define BUTTON 3
#define pot A0
#define pwm1 10
#define pwm2 9
Servo servo_9;
boolean motor_dir = 0;
int motor_speed;
int buttonNew;
int buttonOld=1;
int buttonPushCounter = 0;
int lastButtonState = 0;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  servo_9.attach(9, 500, 2500);
  Serial.begin(9600);
}

void loop()
{
  buttonNew=digitalRead(BUTTON);
  if (buttonNew != lastButtonState) {
    if (buttonNew == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    
    delay(50);
  }
 
  lastButtonState = buttonNew;
if(buttonOld==0 && buttonNew==1){
  motor_speed = analogRead(pot);
  if(motor_dir)
    analogWrite(pwm1, motor_speed);
  else
    analogWrite(pwm2, motor_speed);
  if(!digitalRead(button)){                
    while(!digitalRead(button));           
    motor_dir = !motor_dir;                
    if(motor_dir)
      digitalWrite(pwm2, 0);
    else
      digitalWrite(pwm1, 0);
  }
  else{
  
  int val = analogRead(0);
  for (pos = 0; pos <= 180; pos += 1) {
    
    servo_9.write(pos);
    
    delay(val);
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    
    servo_9.write(pos);
   
    delay(val); 
  }
  }
  buttonOld=buttonNew;
}
}

I am finding it very difficult to follow the logic of your sketch

Why, for instance, do you need both the buttonOld and lastButtonState variables ?

That was a mistake on my part.

Why are you writing motor_speed direct from an analogRead()? analogWrite doesn't take values from 0-1023. Why sometimes analogRead(pot) and sometimes analogRead(0)? Do you really want the servo to take over 3 minutes to complete a move, the result of 180 x 1023ms?

Which button is "the push button" and which "the second pushbutton". I can only see button and BUTTON. More sensible names might make their purpose clear. And how are they connected? You have them set to INPUT_PULLUP but seem to be expecting HIGH to mean pushed. But I may be mistaken there as I got lost trying to follow all your ifs and elses, sorry.

Steve

this condition will never execute because buttonOld is initialized to 1 and only set to buttonNew within that condition

but like Bob, it's hard to understand the code because it relies on the state of the buttons rather than using the button to modify an explicit "state" variable that determines what to do (e.g. control motor or servo)

this is an awkward way of checking for a button being pressed and then released.

a more conventional approach would set both PWM outputs by conditionally checking the motor direction, not doing them under separate tests

of course there's no way to change the speed during each cycle. it would make sense to update the servo position using a servo direction variable in each iteration of loop, while updating the servo delay as well

using "button" and "BUTTON" as two separate variable names is also a poor idea

Thank you, I will definitely try to implement what you have suggested.

Isn't there a problem with 'pwm2' and 'servo_9' both using Pin 9?

Also, from the Servo library documentation:

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10,

That means you shouldn't be using Pin 9 or Pin 10 for PWM when you use the Servo library. I recommend you move the motor control pins to two other PWM pins.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.