[solved] problem running a servo and a motor on the same project


i new circuitry , arduino.  attaching picture of set up.  bought basic starter kit , trying combine servo control "mood cue" project , variable motor control "zeotrope" project.

so can run motor if remove servo code program , can run servo if remove motor control code.  when try write servo in following code neither servo or motor runs correctly.  appreciated.


code:
code: [select]
#include <servo.h>

servo myservo;  // create servo object
//constants motor via h-bridge
const int controlpin1 = 2; // connected pin 7 on h-bridge
const int controlpin2 = 3; // connected pin 2 on h-bridge
const int enablepin = 10;   // connected pin 1 on h-bridge


//variable servo
//int angle;   // variable hold angle servo motor

void setup() {
  myservo.attach(9); // attaches servo on pin 9 servo object
 
  pinmode(controlpin1, output);
  pinmode(controlpin2, output);
  pinmode(enablepin, output);

  // put enable pin low start
  digitalwrite(enablepin, low);

}

void loop() {

    //myservo.write(90);  //if line commented motor works, otherwise motor not run.
    delay(15);

    digitalwrite(controlpin1, high);    //motor forward
    digitalwrite(controlpin2, low);
    analogwrite(enablepin, 125);      //motor half power


   
    delay(3000);                      //motor runs 3 seconds
    analogwrite(enablepin, 0);        //stop motor
    delay(3000);                      //pause 3 seconds

    //myservo.write(60);
    delay(15);
   
    digitalwrite(controlpin1, low);   //reverse motor
    digitalwrite(controlpin2, high);
    analogwrite(9, 125);              //motor half power

    delay(3000);                      //motor runs 3 seconds
    analogwrite(enablepin, 0);        //stop motor
    delay(3000);                      //pause 3 seconds
   
}



the servo library reference tells wrong.

quote
on boards other mega, use of library disables analogwrite() (pwm) functionality on pins 9 , 10, whether or not there servo on pins.
code: [select]
const int enablepin = 10;   // connected pin 1 on h-bridge
analogwrite(enablepin, 125);      //motor half power


karma using code tags on first post.


Arduino Forum > Using Arduino > Programming Questions > [solved] problem running a servo and a motor on the same project


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

RFID-RC522 with Arduino Pro Micro project. Error on the code?