#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:
- 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.
- 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.