DC motor 775 and code

Hi, I have this problem, I think it is so easy but I can't understand where is the difference between the working code and the not working code.
The first work for everything but not for DC motor, Code 2 does not working instead, why?
Codes are the same, I wrote the Code 2, when I saw it workes I do copy and paste on Code 1.

Code 1 :

[code]
#include <MobaTools.h>
#include <Servo.h>

// Define servo motor
Servo Servo1;
Servo Servo2;
Servo Servo3;

const byte enablePin = 8;  // for my CNC shield stepper enable
const int stepPin = 12; // a-stp // stepper 4
const int dirPin = 13; // a-dir

int ang1 = 40;
int ang2 = 100;
int ang3 = 90;

// Motore DC
int dir1PinA = 11;
int dir2PinA = 10;
int speedPinA = 9; // Deve essere un pino PWM per essere
//in grado di controllare la velocità del motore

int speed = 100;
int speed2 = 1;

int val;
int ka = 5;

long stepPos = 200;

//Define stepper motor
MoToStepper StepperX(stepPos,  STEPDIR);
//MoToStepper StepperY(stepPos,  STEPDIR);
MoToStepper StepperZ(stepPos,  STEPDIR);
MoToStepper StepperA(stepPos,  STEPDIR);


void setup() {

  //Define Pin DC motor
  pinMode(dir1PinA, OUTPUT);
  pinMode(dir2PinA, OUTPUT);
  pinMode(speedPinA, OUTPUT);
  Serial.begin(9600);

  //Define Pin Stepper
  pinMode(stepPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);


  //Define servo initial angle
  Servo1.write(ang1);
  Servo2.write(ang2);
  Servo3.write(ang3);


  //Define stepper motor
  StepperX.attach(2, 5);
  //StepperY.attach(3, 6);
  StepperZ.attach(4, 7);
  StepperA.attach(12, 13);

  StepperX.setSpeed(5500);
  // StepperY.setSpeed(5500);
  StepperZ.setSpeed(5500);
  StepperA.setSpeed(5500);

  StepperX.setRampLen(1);
  //StepperY.setRampLen(1);
  StepperZ.setRampLen(1);
  StepperA.setRampLen(1);
}

void loop() {
  if (Serial.available())
  {
    val = Serial.read();

    if (val == 'y') {
      ang1 += ka;
      Servo1.write(ang1);
      Serial.println('y');

    }

    if (val == 'h') {
      ang1 -= ka;
      Servo1.write(ang1);
      Serial.println('h');

    }


    if (val == 't') {
      ang2 += ka;
      Servo2.write(ang2);
      Serial.println('t');

    }

    if (val == 'g') {
      ang2 -= ka;
      Servo2.write(ang2);
      Serial.println('g');

    }
    if (val == 'u') {
      ang3 += ka;
      Servo3.write(ang3);
      Serial.println('u');

    }

    if (val == 'j') {
      ang3 -= ka;
      Servo3.write(ang3);
      Serial.println('j');

    }


    //Adding stepper motor comad

    //Stepper 4 (arm3)         A
    if (val == 'f') {
      StepperA.doSteps(stepPos);
      Serial.println('f');
    }

    if (val == 'r') {
      StepperA.doSteps(-stepPos);
      Serial.println('r');
    }

    //Stepper 3 (arm2)         Z

    if (val == 'd') {
      StepperZ.doSteps(stepPos);
      Serial.println('d');
    }

    if (val == 'e') {
      StepperZ.doSteps(-stepPos);
      Serial.println('e');
    }
    //DC Motor

    if (val == 'w') {
      Serial.println('w');
      analogWrite(speedPinA, speed);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, LOW);
      digitalWrite(dir2PinA, HIGH);
    }

    if (val == 's') {
      Serial.println('s');
      analogWrite(speedPinA, speed);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, HIGH);
      digitalWrite(dir2PinA, LOW);
    }

    if (val == 'x') {
      Serial.println('x');
      analogWrite(speedPinA, speed2);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, HIGH);
      digitalWrite(dir2PinA, LOW);
    }

    //Stepper 1 (base)         X

    if (val == 'q') {
      StepperX.doSteps(stepPos);
      Serial.println('q');

    }

    if (val == 'a') {
      StepperX.doSteps(-stepPos);
      Serial.println('a');

    }
  }
}


[/code]

Code 2:

[code]

// Motore DC
int dir1PinA = 11;
int dir2PinA = 10;
int speedPinA = 9; // Deve essere un pino PWM per essere
//in grado di controllare la velocità del motore

int speed = 100;
int speed2 = 1;

int val;
void setup() {

  //Define Pin DC motor
  pinMode(dir1PinA, OUTPUT);
  pinMode(dir2PinA, OUTPUT);
  pinMode(speedPinA, OUTPUT);
  Serial.begin(9600);
}
void loop() {

  if (Serial.available()) {
    val = Serial.read();

    if (val == 'w') {
      Serial.println('w');
      analogWrite(speedPinA, speed);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, LOW);
      digitalWrite(dir2PinA, HIGH);
    }

    if (val == 's') {
      Serial.println('s');
      analogWrite(speedPinA, speed);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, HIGH);
      digitalWrite(dir2PinA, LOW);
    }

    if (val == 'x') {
      Serial.println('x');
      analogWrite(speedPinA, speed2);//Imposta la velocità via PWM
      digitalWrite(dir1PinA, HIGH);
      digitalWrite(dir2PinA, LOW);
    }
  }
}
[/code]

You do not say what board that you are using nor do you sat what "not working" means. The phrase "not working conveys no useful information.

How to get the best from this forum.

Here is my guess.

From the Servo library reference:

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

HI,
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

What are you using as a power supply?
Can you please post link to specs/data on the motor?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The DC motor doesn't work with "the first". Why would you expect the DC motor to work with "Code 2"?

DC motor work with the code 2.

I use Arduino Uno with CNC shield V3.0 and i have a 24v power supply for DC motor.
But I think the problem is in the code not in the components.

Not working means that the motor is stopped while with the code 2 it work perfectly. I think You are right, if I use MobaTools library for servo it's the same? How can I solve that?

The MobaTools servo library uses Timer 1, just like the standard Servo library so analogWrite will not work on pins 9 and 10 with MobaTools either.

Use a different PWM pin for the motor. You could use pins 9 and 10 for dir1PinA and dir2PinA and pin 11 for speedPinA. Then PWM pin 11 to control speed.

Ok thank you so much for your reply, I'll do that.

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