Servo and motor problem

This program is running cw and servo but ccw is not running.


//L293D motor driver
#include <Servo.h>
// Motor A connections
#define enA 9
#define in1 8
#define in2 7
Servo Myservo;
int pos;
void setup() {
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  Myservo.attach(3);
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  delay(100);
  digitalWrite(in2, LOW);
  delay(100); 
}
void loop(){
     CW();
     servo();
     CCW();
     exit(0);
}
// This function lets you control spinning direction of motors
void CW() {
  // Set motors to maximum speed
  analogWrite(enA, 255);
// Turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(5000);
  // Turn off motors
 digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
    delay(1000);
  
}
void servo(){
  for(pos=0;pos=180;pos++){
  
  Myservo.write(pos);
  delay(15);    
}  
}
void CCW(){
   // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(5000);
    digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
    delay(1000); 
  
  }

Put a serial print with some kind of "here I am" message in the ccw function to see if it's being entered.

What's this for exit(0);?

Welcome to the forum

void loop()
{
  CW();
  servo();
  CCW();
  exit(0);
}

No call to servo() after the call to CCW() ?

yes sir

  1. CW
  2. Servo
  3. CWW

CWW is not working

See post #2

I want to run the code only once

What code in the CCW() function would make the servo move ? Come to that, what code in the entire sketch would move the servo counter clockwise ?

Where do you think the code is going to exit to as there is no Operating System ? If you want it to run only once then put it in setup()

I don't think cw() and ccw() are about the servo, seems there's a dc motor in there on a 293.

Humour (or as the spell check seems to prefer, humor) me: put a serial print message in ccw() to see if the function is being entered.

This is my circuit diagram, first, I have to drive 5 sec CW dc motor, then servo 180, then 5 sec CCW dc motor.

servo doesn't work after.

Help me..............................................................

@arukali
Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section. Therefore I have moved your post here. Please be more careful where you post in future.

You may want to read this before you proceed:-
how to get the best out of this forum

I added those debug lines to your code from earlier in the thread, got same problem as you.

Then saw that the for loop is coded wrong, added the < and it works now:

for (pos = 0; pos <= 180; pos++) {

image

edit: Full disclosure, I ran it on an Arduino with no servo or 293 and motor actually within 100m.

Thank you Sir .....
I didn't check carefully
:sweat_smile::sweat_smile::sweat_smile::sweat_smile:

:+1:

Side note: you shouldn't be powering the motor and / or servo from the Arduino 5V pin. You should use an external supply; you may be overloading the Arduino's voltage regulator.

If you try to control speed with the enA pin you will find that it will not work.

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

.

1 Like

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