Go Down

Topic: difficulty in controlling both brushless and dc motors (Read 777 times) previous topic - next topic

robyc

I am trying to program my arduino 2009 to control both a brushless motor and 2 Dc motors.

For the brushless motor I am using the Servo.h library, using the command "writeMicroseconds()" to control it.
For the Dc motors I am using the Qik2s9v1 controller with Qik2s9v1.h and NewSoftSerial.h libraries (commands: motor.motor0Forward(),  motor.motor0Reverse() etc.).

The two functions if used in separate sketches work perfectly (start, stop, increase and decrease of speed all work well).
If however I try to put them together in the same program I can only get one of them to work (either the 2 dc motors or the brushless motor) even if they are working on different pins.
Any suggestions or places to look for a program that works and that I could copy?
Tks.

zoomkat

Without your code issues may be hard to trouble shoot. Check the servo library information and make note of which pins can/can't be used when running PWM and the servo library together. 
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Grumpy_Mike

Quote
Any suggestions or places to look for a program that works and that I could copy?

On this forum we will help you learn. Where is the learning in copying?

robyc

The project is to build a robot grass cutter: the brushless motor turns the blade that cuts the grass and the 2 dc motors are attached to the wheels.

THIS IS THE PROGRAM (THAT WORKS)TO CONTROL THE BRUSHLESS MOTOR:

#include <Servo.h>
Servo myservo;  // create servo object to control a servo (brushless motor)

int minspe = 800; // min blade speed
int maxspe = 1900; // max speed for blade
int spe;
void setup()
{Serial.begin(9600); // scriviamo qualcosa sul monitor?
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object
myservo.writeMicroseconds(minspe); //min pulse (otherwise the ESC stops working)
}

void loop()   {

  delay (10000); // ******* INITIAL DELAY TO GET THE ESC RUNNING********
  accelerazioneLamaDiTaglio();
  delay (5000);
  decelerazioneLamaDiTaglio();
delay (5000);
}


void accelerazioneLamaDiTaglio(){
  Serial.println("inizio for loop di accelerazione    ");
  for (spe=800; spe <maxspe; spe++)
  { myservo.writeMicroseconds(spe);
  delay(15);   
  }
}
 
void decelerazioneLamaDiTaglio(){
  for (spe=2000; spe > 800; spe--)
  { myservo.writeMicroseconds(spe);
  delay(15);   
}
}



THIS PROGRAM WORKS AND CONTROLS THE 2 DC MOTORS:

#include <CompactQik2s9v1.h>
#include <NewSoftSerial.h>

/*
   Important Note:
      The rxPin goes to the Qik's "TX" pin
      The txPin goes to the Qik's "RX" pin
*/
#define rxPin 3
#define txPin 4
#define rstPin 5

NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);
CompactQik2s9v1 motor = CompactQik2s9v1(&mySerial,rstPin);

byte motorSelection;
byte motorSpeed;
int maxspeed = 110;

void setup() 
{
  Serial.begin(9600);
  mySerial.begin(9600);
  motor.begin();
 
//  motor.stopBothMotors();
}


void loop()
{
  Serial.println("inizio accer");
motorsacceleration();
delay (10000);
Serial.println("inizio decel");
motorsdecceleration();
delay (3000);
Serial.println("inizio reverse and fwd dx");
motorsreversefwddx();
///delay (5000);
motorsacceleration();
Serial.println("inizio reverse and fwd sx");
delay (10000);
motorsreversefwdsx();
///delay (5000);

}
void motorsacceleration(){
  for (int i=10; i<maxspeed; i++){   
motor.motor0Forward(i);
motor.motor1Forward(i);
delay (10);}
}

void motorsdecceleration(){
  for (int i=maxspeed; i>1; i--){ 
motor.motor0Forward(i);
motor.motor1Forward(i);
delay (10);}
}

void motorsreversefwddx(){
//if (int i>0) { motorsdecceleration();}
for (int i=10; i<60; i++){   
motor.motor0Reverse(i*2);
motor.motor1Reverse(i);
delay (10);}
delay (3000);
}

void motorsreversefwdsx(){
   for (int i=10; i<60; i++){   
motor.motor0Reverse(i);
motor.motor1Reverse(i*2);
delay (10);}
delay (3000);
}


THIS IS THE PROGRAM THAT HAS SOME DIFFICULTY IN RUNNING CORRECTLY: I PUT A FEW LINES OF PROGRAM TO SEE IF I COULD CONTROL THE MOTORS MANUALLY (WITH 2 SWITCHES) ON PIN 9 AND 12.
I CAN MODIFY THE SPEED OF THE BLADE BUT NO RESPONSE FROM THE 2 DC MOTORS
// .h
#include <Servo.h> //per la lama
Servo myservo;  // create servo object to control a servo

#include <CompactQik2s9v1.h> // per i motori di trazione
#include <NewSoftSerial.h>  // per i motori di trazione


// variables - lama di taglio
int minspe = 800; // velocita' zero lama di taglio
int maxspe = 1900; // massima velocita' lama di taglio
int spe;


// variables - 2 motori di trazione
#define rxPin 3
#define txPin 4
#define rstPin 5

NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);
CompactQik2s9v1 motor = CompactQik2s9v1(&mySerial,rstPin);

///byte motorSelection;
///byte motorSpeed;
int maxspeed = 110;


void setup(){

  myservo.attach(2);  // attaches the servo on pin 2 to the servo object
myservo.writeMicroseconds(minspe); //fornire al motore l'impulso minimo (altrimenti si blocca)
delay (10000);
accelerazioneLamaDiTaglio();
}
void loop()   {
  Serial.println("loop    ");
int ledPin = 12;   // manual control of blade
pinMode (ledPin, INPUT); // manual control of blade
int ledPin9 = 9;
pinMode (ledPin9, INPUT);

if (digitalRead(ledPin)== HIGH)
decelerazioneLamaDiTaglio() ;
delay (15);
if (digitalRead (ledPin)== LOW && spe < 900)
accelerazioneLamaDiTaglio();

if (digitalRead(ledPin9)== HIGH)
motorsacceleration();
delay (10);
}


void accelerazioneLamaDiTaglio(){
  Serial.println("inizio for loop di accelerazione    ");
  for (spe=800; spe <maxspe; spe++)
  { myservo.writeMicroseconds(spe);
  delay(15);   
  }
}
void decelerazioneLamaDiTaglio(){
  for (spe=2000; spe > 800; spe--)
  { myservo.writeMicroseconds(spe);
  delay(10);   
}}

void motorsacceleration(){
  for (int i=10; i<maxspeed; i++){   
motor.motor0Forward(i);
motor.motor1Forward(i);
delay (10);}}


Grumpy_Mike, you are right:
my idea of coping was to learn by reading a program that works...

Tks.

Magician

I can't see Serial.begin(9600) in combine sketch setup.
And second, both sketches use "blocking" function  to control motors - delay.
Whenever one motor accelerating / decelerating another motor just waiting.
You have to re-work your code, get rid of DELAY. Look into example :
"Blinking w/o delay" for begining.

Go Up