1

I connected my motor and a servo to my L293D motor shield and i used a 3.7 volt battery with 4800 mAh. I also connected a button to on and off the motor for a certain amount of time but only the servo is working properly while my motor doesn't run.

HERE IS THE CODE

int LEDPin=11;
int buttonPin=4;
int buttonNew;
int buttonOld=1;
int dt=100;
#include <Servo.h>
#include <AFMotor.h>
Servo myservo; 
int servopin(10);
AF_DCMotor motor1(1);



void setup(){
  Serial.begin(9600);
  pinMode(LEDPin,OUTPUT);
  pinMode(buttonPin,INPUT);
  myservo.attach(servopin);
  motor1.setSpeed(200);
}

void loop(){
  buttonNew=digitalRead(buttonPin);
  if(buttonOld==0 && buttonNew==1){
    if (LEDState==0){
      digitalWrite(LEDPin,HIGH);
      myservo.write(0);
      Serial.print(buttonOld);
     int motorpos='0';
     int motorpos2='1';
      if (motorpos=='0'){
        motor1.run(FORWARD);
        delay(2000);
        motorpos='1';
      }
      if(motorpos='1'){
        motor1.run(RELEASE);
      }
       LEDState=1;
    }
    else{
      digitalWrite(LEDPin,LOW);
      myservo.write(90);
      Serial.print(buttonNew);
      int motorpos='0';
      int motorpos2='1';
      if (motorpos=='0'){
        motor1.run(BACKWARD);
        delay(2000);
        motorpos='1';
      }
      if (motorpos='1'){
        motor1.run(RELEASE);
      }
        LEDState=0;
    }
  }
  buttonOld=buttonNew;
  delay(dt);
}
4
  • what seems to be the problem because my motor doesn't work with this code but with a different code it works – Thomas Raymond Santos May 12 at 7:54
  • just to be sure that the motor isn't broken or the problem – Thomas Raymond Santos May 12 at 8:04
  • Sorry for that @jsotola I will consider it next time, thank you! – Thomas Raymond Santos May 12 at 8:11
  • 1
    Posting a schematic, not a frizzy thing will help. You can lose a fair amount of voltage using the L293D shield. A MOSFET bridge would be much better. – Gil May 13 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.