Go Down

Topic: Combining project 5 & 10: DC motor no longer variable speed (Read 944 times) previous topic - next topic

Rusty1975

Dear,

in an attempt to have the set-up for a motorized vehicle I combined the circuit and code of
* project 5 in which a servo motor is used which could be used for steering and
* project 10 in which a DC motor is turned on/off and with the possibility to switch the direction. I added a little variation to the code in the book to map the potentiometer (0-1023) to the speed of the DC motor (0-255), which works fine.

However, in this combination, the DC motor only works (at full speed, 255) when the potentiometer reads 1023.
Any other potentiometer value is translated/mapped to a certain value of the motorspeed (seen in the serial monitor), however the DC motor doesn't spin at all.

If I upload the code of only project 10 to the combined circuit, I can vary the speed of the DC motor by means of the potentiometer, i.e. the circuit and the code work fine. Only in combination with the code of the servo motor (that works fine, even in the combined circuit/code) the DC motor's speed is no longer variable.

The code is underneath.

I can't find any incompatibility in the code. Is there any other reason why it doesn't do what I expect it to do?
Thanks for the help.!

/*
  Arduino Starter Kit example
  Project 10 - Zoetrope
  Parts required:
  - two 10 kilohm resistors
  - two momentary pushbuttons
  - one 10 kilohm potentiometer
  - motor
  - 9V battery
  - H-Bridge

  Project 5 - Servo Mood Indicator
  Parts required:
  - servo motor
  - 10 kilohm potentiometer
  - two 100 uF electrolytic capacitors
*/

// include the Servo library
#include <Servo.h>

Servo myServo;  // create a servo object

const int potPinS = A1; // analog pin used to connect the potentiometer of the servo motor
int potValS;  // variable to read the value from the analog pin of the servo motor
int angleS;   // variable to hold the angle for the servo motor

const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9;   // connected to pin 1 on the H-bridge
const int directionSwitchPin = 4;  // connected to the switch for direction
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the DC motor on and off
const int potPin = A0;  // connected to the potentiometer's output of the DC motor

// create some variables to hold values from your inputs of the DC motor
int onOffSwitchState = 0;  // current state of the on/off switch
int previousOnOffSwitchState = 0; // previous position of the on/off switch
int directionSwitchState = 0;  // current state of the direction switch
int previousDirectionSwitchState = 0;  // previous state of the direction switch

int potVal = 0; //of the DC motor
int motorEnabled = 0; // Turns the DC motor on/off
int motorSpeed = 0; // speed of the DC motor
int motorDirection = 1; // current direction of the DC motor

void setup() {
  myServo.attach(10); // attaches the servo on pin 10 to the servo object

// initialize the inputs and outputs  of the DC motor
  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // pull the enable pin LOW to start
  digitalWrite(enablePin, LOW);
  // open a serial connection to your computer
  Serial.begin(9600);
}

void loop() {
  potValS = analogRead(potPinS); // read the value of the potentiometer of the servo motor
  Serial.print("potValS: "); // print out the value to the Serial Monitor
  Serial.print(potValS);

  angleS = map(potValS, 0, 1023, 0, 155); // scale the numbers from the pot of the servo motor
  Serial.print(", angleS: "); // print out the angle for the servo motor
  Serial.print(angleS);

  myServo.write(angleS); // set the servo position
  delay(15); // wait for the servo to get there

  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin); // read the value of the on/off switch
  delay(1);

  directionSwitchState = digitalRead(directionSwitchPin); // read the value of the direction switch

  // read the value of the pot of the DC motor used for PWM
  potVal = analogRead(potPin);
  motorSpeed = map(potVal, 0, 1023, 0, 255);
  //motorSpeed = analogRead(potPin)/4; original code of project 10
  Serial.print(", PotValue = " );
  Serial.print(potVal);
  Serial.print(", Motorspeed = ");
  Serial.print(motorSpeed);
  Serial.print(", On/Off = ");
  Serial.print(onOffSwitchState);
  Serial.print(", Direction = ");
  Serial.println(directionSwitchState);
 
  // if the on/off button changed state since the last loop()
  if (onOffSwitchState != previousOnOffSwitchState) {
    // change the value of motorEnabled if pressed
    if (onOffSwitchState == HIGH) {
      motorEnabled = !motorEnabled;
    }
  }

  // if the direction button changed state since the last loop()
  if (directionSwitchState != previousDirectionSwitchState) {
    // change the value of motorDirection if pressed
    if (directionSwitchState == HIGH) {
      motorDirection = !motorDirection;
    }
  }

  // change the direction the motor spins by talking to the control pins
  // on the H-Bridge
  if (motorDirection == 1) {
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
  } else {
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
  }

  // if the DC motor is supposed to be on
  if (motorEnabled == 1) {
    // PWM the enable pin to vary the speed
    analogWrite(enablePin, motorSpeed);
  } else { // if the DC motor is not supposed to be on
    //turn the DC motor off
    analogWrite(enablePin, 0);
  }
  // save the current on/off switch state as the previous
  previousDirectionSwitchState = directionSwitchState;
  // save the current switch state as the previous
  previousOnOffSwitchState = onOffSwitchState;
}

rogertee

without looking at the two projects are you using two different analog pins for the dc motor and a different one for servo angle try using another analog pin for dc motor speed like a2 and adjust the code and circuit to use a2 instead of a0 and separate pot for each also include a circuit diagram in future.

Go Up