Servo problems with motorshield + arduino

#include <Servo.h>
//Motor
int M1 = 4; // left motor M1 forward
int E1 = 5;// left motor M1 backward speed
int E2 = 6; // right motor M2 forward speed
int M2 = 7; // right motor M2 backward

//Flame
int analogL = A3; // define the flame sensor interface
int RsensorValue = 0;
int analogR = A4; // define the flame sensor interface
int LsensorValue = 0; // variable to store the value coming from the sensor 
int val ;// define numeric variables val
float sensorL; //read analog value
float sensorR; //read analog value

//Line Sensor
int LS = 8;
int CS = 9;
int RS = 10;

//Servo
Servo myservo;
int pos = 180;//180 because we have attached our servo in the other direction
 
void setup ()
{
  //motor
  pinMode(M1, OUTPUT); // make Left motor as output front
  pinMode(M2, OUTPUT); // make right motor as output back
  pinMode(E1, OUTPUT); // make left motor as output front speed
  pinMode(E2, OUTPUT); // make right motor as output back speed
  //Flame
  pinMode(analogL, INPUT) ;// output interface defines the flame sensor
  pinMode(analogR, INPUT) ;// output interface defines the flame sensor
  //Line Sensor
  pinMode(LS, INPUT);
  pinMode(CS, INPUT);
  pinMode(RS, INPUT);
  //Servo
  //myservo.attach(2); // problem when attaching it at first.
  Serial.begin(115200);
}

 
void loop ()
{
  //Print flame value
  sensorL = analogRead(analogL);
  sensorR = analogRead(analogR);
  Serial.println(sensorL);  // display tempature
  Serial.println(sensorR);  // display tempature
 
  if (digitalRead(LS) && digitalRead(CS) && digitalRead(RS)) // absolute left turn
  {
    digitalWrite(M1,LOW);
    digitalWrite(M2,HIGH);
    analogWrite(E1,115);
    analogWrite(E2,150);
    delay(50);
  }
  else if (digitalRead(LS) && digitalRead(CS) && !digitalRead(RS)) //Small left turn
  {
    digitalWrite(M1,LOW);
    digitalWrite(M2,HIGH);
    analogWrite(E1,20);
    analogWrite(E2,55);
  }
  else if (!digitalRead(LS) && digitalRead(CS) && digitalRead(RS)) //Small Right
  {
    digitalWrite(M1,HIGH);
    digitalWrite(M2,LOW);
    analogWrite(E1,55);
    analogWrite(E2,20);
  } 
 
  if (!digitalRead(LS) && !digitalRead(CS) && !digitalRead(RS)) //Preparing for flame sensing
  {
    digitalWrite(M1,HIGH);
    digitalWrite(M2,HIGH);
    analogWrite(E1,60);
    analogWrite(E2,40);
  }  
  if ((sensorL <300) && (sensorR <300)) // When the flame sensor detects a signal, LED flashes
  {
    Serial.println("Straight");
    digitalWrite(M1,HIGH);
    digitalWrite(M2,LOW);
    analogWrite(E1,50);
    analogWrite(E2,50);
  }
   
  if ((sensorL >300) && (sensorR <300)) // no fire, fire, turn right
  {
    Serial.println("Right");
    digitalWrite(M1,HIGH); //left motor turns
    digitalWrite(M2,LOW); // right motor stops
    analogWrite(E1,50);
    analogWrite(E2,50);
  }
  
  if ((sensorL <300) && (sensorR >300)) //  fire, no fire, turn left
  { 
    Serial.println("Left");
    digitalWrite(M1,LOW); //left motor stops
    digitalWrite(M2,HIGH); // right motor turns
    analogWrite(E1,50);
    analogWrite(E2,50);
  }
  
  if ((sensorL <100) && (sensorR <100)) // When robot is close to the flame, robot stops
  {
    Serial.println("Stop");
    myservo.attach(2);
    digitalWrite(M1,LOW); // left motor stops
    digitalWrite(M2,LOW); // right motor stops
    analogWrite(E1,0);
    analogWrite(E2,0);
  
      for (pos = 145; pos >= 10; pos -= 1) 
      { 
        // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
      for (pos = 10; pos <= 145; pos += 1) 
      {
        // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                         // waits 15ms for the servo to reach the position
      }
    myservo.detach();
    delay(5000);
    robotstop();
  }
}


void robotstop()
{
  digitalWrite(M1,LOW);
  digitalWrite(M2,LOW);
  analogWrite(E1,0);
  analogWrite(E2,0);
  delay(5000);
}

Hi, so my project is about a fire fighting robot and it requires the servo to mix in the container when it reaches the fire. But for some reason. The servo is acting up. Here are the issues that we have faced and have find ways to solve:

  1. The servo moves together with the left motor.

Now this is weird. Because when left motor moves forward, the servo moves as well. And when the motor moves backwards, it moves too. It is also noted that when we remove the digital pin from the arduino, it still gets signal to move. WHAT! The way we solve this is to isolate the power source which then brings a second problem.

  1. After isolating the power source, the servo doesn’t move with the motor anymore but, we run into another problem. When we attach the digital pin into the motor shield, it goes and locks in the most min position.

We initially was able to make it work by only attaching it to a pin number when we need it and detach it when we don’t in the loop area [Note: we had to bend the pin of the motor shield to be able to attach the pin directly into the arduino]. But that has proven to be futile when the motor shield is powered again once the digitalpin is connected regardless of code.

now the robot has 3 power sources.
A power bank powering the arduino,
a 12v lipo powering the motorshield and
4 AA batteries going through a VRM to the servo.

I really don’t want to bother you guys because I should learn too but all my seniors have also hit a deadend for this one too so I have resorted to ask help from you guys. I am open for discussion and pics if needed but my time zone is ZULU +8 so responds may take a while.

As for servo wiring its is noted that red is live, brown is ground and orange is signal. Seems right but feels suspicious since the servo moved, albeit, only in one direction and doesn’t go the other direction when powering the motor when connected to the motor shield.

if there are also other ways to improve the code i’ll be happy to learn more.

Do the 3 power sources have a common GND connection ? What voltage does the "power bank" provide and how is it connected to the Arduino ?

So my configuration is this: I have two sides of the breadboard.

1) one side has a 12v battery which is connected to the motor shield. (and previously the arduino through the Vin port and ground but recently we found that the arduino heats up so much to the point that it stop signal until it is cooled enough I guess that we moved to power bank)

the other side has a 5v line that comes from the arduino ( which passes through the motor shield) and goes to every component on the board. which means 5 of the input signals, excluding the servo in this case.

2) Power bank now connects the arduino with the given USB cable. We don't know the voltage and amp of the battery bank but common battery banks are assumed to have 5v and also 1A power.

3) 4 AA batteries are connected to a VRM that grounds onto the AA battery case itself and is connected also to the servo and grounded with the same VRM ground pin.

Hi, Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? Please show all your batteries and how they are connected.

NOT a fritzy picture.

Thanks.. Tom.. :)

Sorry that I don’t have my robot with me at the moment (it’s in my locker) But this is the sketch that I drew on a paper. Hope this is not hard to look at.

Before this, the servo is connected to the same line as the other components.

Hi; OPs pic. |500x375 Do you have the gnd of the servo power and the "PowerBank" connected to gnd of the Arduino?

Tom... :)