element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Programing issue
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 37 replies
  • Answers 3 answers
  • Subscribers 405 subscribers
  • Views 3481 views
  • Users 0 members are here
  • programing
  • arduino
Related

Programing issue

dirtdiver
dirtdiver over 14 years ago

Hi,

so I have a dc motor and a servo each controlled with a different potentiometer.First I made two separate codes and all works fine, but when I combine the two codes the servo starts getting readings from both pots.Here are the working codes

 

int motorPin = 9;                                          

int potpin = 0;

int val;   

 

void setup()

{

  pinMode(potpin,INPUT);

pinMode(motorPin, OUTPUT);

}

 

 

void loop()

{

  val = analogRead(potpin);        

  val = map(val, 0, 1023, 0, 255);   

 

analogWrite(motorPin, val); 

  delay(15);                         

}

 

 

AND

 

 

 

#include <Servo.h>

 

Servo myservo; 

 

int potpin = 5; 

int val;   

 

void setup()

{

  myservo.attach(3); 

}

 

void loop()

{

  val = analogRead(potpin);           

  if (val<300){

  myservo.write(67);

  }else if (val<600){

  myservo.write(89);

  } else {

    myservo.write(109);

  }

 

delay(15);                          

}

 

Heres the combined code that doesnt work.If you see whats wrong please reply, couse I cant find the mistake here.

 

 

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int motorPin = 9;

int potpin1 = 5;  // GEARS

int potpin = 0;  // SPEED

int val;    // variable to read the value from the analog pin

 

 

void setup()

{

  pinMode(potpin1,INPUT);                //GEARS

  pinMode(potpin,INPUT);                 //SPEED

pinMode(motorPin, OUTPUT);

myservo.attach(3);  // attaches the servo on pin 9 to the servo object

}

 

 

void loop()

{

 

  val = analogRead(potpin);           //SPEED

  val= map(val, 0, 1023, 0, 255); 

 

 

analogWrite(motorPin, val); 

  delay(15);                         

 

  val = analogRead(potpin1);            //   GEARS

  if (val<300){

  myservo.write(130);

  }else if (val<600){

  myservo.write(110);

  } else {

    myservo.write(90);

  }

 

delay(15);

}

 

int motorPin = 9;
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
void setup()
{
  pinMode(potpin,INPUT);
pinMode(motorPin, OUTPUT);
}
void loop()
{
  val = analogRead(potpin);        
  val = map(val, 0, 1023, 0, 255);   
analogWrite(motorPin, val); 
  delay(15);                         
}

ALSO HERES A VIDEO SHOWING THE APLICATION OF THE FIRST TWO CODES  

http://www.youtube.com/watch?v=MrHqeFmT6DU

  • Sign in to reply
  • Cancel

Top Replies

  • dmaruska
    dmaruska over 14 years ago in reply to dirtdiver +1
    Most welcome...
Parents
  • Former Member
    0 Former Member over 14 years ago

    // Try this code

     

    #include <Servo.h>

     

    Servo gear;
    int pot1= A0;                  //for motor
    int pot2= A1;                  //for servo

     

    int motor = 9;
    int s,g,i;                     // variable to store analog value. s=speed, g=gear and i=intermediate;

     

    void setup()
    {
      pinMode(pot1, INPUT);
      pinMode(pot2, INPUT);
      pinMode(motor, OUTPUT);
      gear.attach(3);               
    }

     

    void loop()
    {
      g=analogRead(pot2);
      i=map(i,0,1023,0,179);              // mapping according to your 180* servo
     
      s=analogRead(pot1);
      s=map(s,0,1023,0,255);             //mapping for motor speed
     
      analogWrite(motor,s);
      delay(5);
     
          if(i<53)
          {
           gear.write(130);
          }
          else if(i<106)
          {
           gear.write(110);
          }
          else
          {
            gear.write(90);
          }
          delay(10);     
    }
     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dirtdiver
    0 dirtdiver over 14 years ago in reply to Former Member

    here's something weird!!!
    I changed the code so that it uses only one pot for controling the servo and the motor is constantly on,
    now heres the thing- when the motor is set to 255 -its starts rotating , the servo is working like its supposed to (getting readings from the pot)
    BUT when i set the motor to say 50 or 100 the motor doesnt spin, the servo goes crazy (random positions)
    heres the code
    #include <Servo.h>
    Servo myservo;
    int motorPin = 9;
    int potpin = 1; 
    int val;   
    void setup()
    {
      myservo.attach(3);
    pinMode(motorPin, OUTPUT);
    }
    void loop()
    {
      val = analogRead(potpin);           
      if (val<300){
      myservo.write(67);
      }else if (val<600){
      myservo.write(89);
      } else {
        myservo.write(109);
      }
                     
    delay(15); 
      analogWrite(motorPin, 255);    // WHEN I SET THIS TO 50 OR 100 INSTEAD OF 255 - NOTHING SEEMS TO WORK
      delay(15);  
    }
    that is the base of my problem couse when i have a pot controling the motor and whne its sending it a value not close to 255 the servo goes crazy and the motor is unable to start (it starts at the pots max ..at full speed )
    anyone any ideas image

    here's something weird!!!

    I changed the code so that it uses only one pot for controling the servo and the motor is constantly on,

    now heres the thing- when the motor is set to 255 -its starts rotating , the servo is working like its supposed to (getting readings from the pot)

    BUT when i set the motor to say 50 or 100 the motor doesnt spin, the servo goes crazy (random positions)

    heres the code

     

    #include <Servo.h>

     

    Servo myservo;

     

    int motorPin = 9;

    int potpin = 1; 

    int val;   

     

    void setup()

    {

      myservo.attach(3);

    pinMode(motorPin, OUTPUT);

    }

     

    void loop()

    {

      val = analogRead(potpin);           

      if (val<300){

      myservo.write(67);

      }else if (val<600){

      myservo.write(89);

      } else {

        myservo.write(109);

      }

     

    delay(15); 

     

      analogWrite(motorPin, 255);    // WHEN I SET THIS TO 50 OR 100 INSTEAD OF 255 - NOTHING SEEMS TO WORK

      delay(15);  

    }

     

    that is the base of my problem couse when i have a pot controling the motor and whne its sending it a value not close to 255 the servo goes crazy and the motor is unable to start (it starts at the pots max ..at full speed )

     

    anyone any ideas image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 14 years ago in reply to dirtdiver

    U missed one thing in your programme and that is pinMode(potpin,INPUT);

    Now another thing, if you put any analog pin as input mode and if you do not connect any wire/pot, by mistake, to Analog pin then it will take value 'Randomly', rectified type wave would be there...so this problem might be there.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 14 years ago in reply to dirtdiver

    Here is the another way to do it... using L293 Motor driver IC. Now you dont need any kind of transistor. So hardware problem reduced here....

    Just try it...i have tried this at home. make sure you have connected grounds both of the devices together.

     

    as you can see in the picture,

    1 pin of IC is PWM. Its is enable, connect arduino pwm pin here. So you can control speed also.

    2 and 7 shoud be connected to arduino digital pin. So, by putting high and low to pins, you can set the motor direction easily.

     

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dirtdiver
    0 dirtdiver over 14 years ago in reply to Former Member

    yeah i missed that ,  but even with it the same thing is happening, what could be causing this?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 14 years ago in reply to dirtdiver

    h..it might be this one,

    if you put any analog pin as input mode and if you do not connect any  wire/pot, by mistake, to Analog pin then it will take value 'Randomly',  rectified type wave would be there...so this problem might be there.

     

    and secondly if your motor is not reliable in torque then it will absolutely stop at 50 and 100!! coz of low torque motor... so try high torqe motor. most of the torque motor will run at 50 speed in No-load condition, but during load they stops..so use spedd100 or more..and use high torque motor.

    In your case torqe should be 35kg.cm.! It would work better...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 14 years ago in reply to dirtdiver

    and try another circuit as i shown you in that figure...click it and zoom it out... it would work better and more efficiently..coz L293 is Motor Driver Ic so it will provide your circuit better current.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dirtdiver
    0 dirtdiver over 14 years ago in reply to Former Member

    ok I'll go get one L293 ,try that thing and report back

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dirtdiver
    0 dirtdiver over 14 years ago in reply to Former Member

    does it metter if its L293D or  L293C/D

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • dirtdiver
    0 dirtdiver over 14 years ago in reply to Former Member

    does it metter if its L293D or  L293C/D

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • Former Member
    0 Former Member over 14 years ago in reply to dirtdiver

    No... its does not have vast difference... Just difference in current ratings!!! L293 have 1 A, while L293D have 0.5 A. Nothing else...

    You are using only one motor, so either L293 or D you might use.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube