So i wrote this code to use a DC motor and two servos. whenever i have the servos lines of codes in just to perform sweeps, the Dc motor wouldn't work but the servos works.
when i remove the Servos codes, the DC motor works.
Can someone please help detect what the conflict is.
also i am using a Seeed Motor Shield to drive the motor.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created
int power=0;
int pos = 90;
int post = 90;
#define ledPin 13
#define interruptNumber2 0
#define interruptNumber3 1
#define limitSwitch2 2
#define limitSwitch3 3
#define OnOffSwitch 4
volatile byte limitFlag2;
volatile byte limitFlag3;
int B=0;
int I=0;
int pinI1=8;//define I1 interface for motor sheild
int pinI2=11;//define I2 interface for motor sheild
int speedpinA=9;//enable motor A
int spead =250;//define the spead of motor
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
myservo1.attach(6); // attaches the servo on pin 9 to the servo object
myservo1.write(pos); //
myservo2.attach(5); // attaches the servo on pin 9 to the servo object
myservo2.write(post);//
attachInterrupt(interruptNumber2, LimitInterrupt2, RISING);
attachInterrupt(interruptNumber3, LimitInterrupt3, RISING);
Serial.begin(9600);
}
const int TIMEDELAY = 1000;
void loop()
{
int OnOff = digitalRead(4); //turn on devices
Serial.println(OnOff);
Serial.println("\n");
delay(TIMEDELAY);
if(OnOff == HIGH)
{
Serial.println("DEVICE IS TURNINIG ON ");
Serial.println("\n");
delay(TIMEDELAY);
B=1;
if(B==1)
{
I=1;
if(I==1)
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
digitalWrite(pinI1,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI2,LOW);
if (limitFlag2==1)
{
Serial.println("MOTOR IS STOPPING");
Serial.println("\n");
limitFlag2=0;//reset the flag for the next interrupt
}
delay(5000);
while(I==1)
{
digitalWrite(ledPin,HIGH);
Serial.println("LED ON");
Serial.println("\n");
delay(TIMEDELAY);
digitalWrite(ledPin,LOW);
Serial.println("LED OFF");
Serial.println("\n");
delay(TIMEDELAY);
for(pos = 3; pos < 176; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(75); // waits 15ms for the servo to reach the position
}
for(post = 3; post < 176; post += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo2.write(post); // tell servo to go to position in variable 'pos'
delay(75); // waits 15ms for the servo to reach the position
}
for(pos = 176; pos>=3; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(75); // waits 15ms for the servo to reach the position
}
for(post = 176; post>=3; post-=1) // goes from 180 degrees to 0 degrees
{
myservo2.write(post); // tell servo to go to position in variable 'pos'
delay(75); // waits 15ms for the servo to reach the position
}
OnOff = digitalRead(4);
Serial.println(OnOff);
if(OnOff == LOW)
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
digitalWrite(pinI1,LOW);//turn DC Motor B move clockwise
digitalWrite(pinI2,HIGH);
if (limitFlag3==1)
{
Serial.println("MOTOR IS STOPPING");
Serial.println("\n");
limitFlag3=0;//reset the flag for the next interrupt
}
Serial.print("DEVICE IS TURNINIG OFF ");
Serial.println("\n");
delay(3000);
B=0;
I=0;
}
}
}
}
}
}
void LimitInterrupt2 ()
{
if (digitalRead(limitSwitch2)==HIGH && digitalRead(pinI1)==HIGH) //
{
digitalWrite(pinI1,LOW);//turn DC Motor B move clockwise
digitalWrite(pinI2,LOW);
Serial.print("LIMIT SWITCH WAS PRESSED");
Serial.println("\n");
limitFlag2=1;
}
}
void LimitInterrupt3 ()
{
if (digitalRead(limitSwitch3)==HIGH && digitalRead(pinI2)==HIGH) //
{
digitalWrite(pinI1,LOW);//turn DC Motor B move clockwise
digitalWrite(pinI2,LOW);
Serial.print("LIMIT SWITCH WAS PRESSED");
Serial.println("\n");
limitFlag2=1;
}
}
Moderator edit: code tags, yet again.
Final_Code_2_1.ino (6.82 KB)