#include <Servo.h>
int LedPin1 = 12;
int LedPin = 13;
int MotorPin1 = 9;
int MotorPin2 = 10;
int servoPin = 6;
Servo VishalServo;
void setup()
{
digitalWrite(LedPin, HIGH);
Serial.begin(9600);
Serial.println("Robotic tank");
pinMode(LedPin, OUTPUT);
pinMode(LedPin1, OUTPUT);
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
VishalServo.attach(servoPin);
VishalServo.writeMicroseconds(1500);
Serial.println("Wait for 1 second");
delay(1000);
Serial.println("You can now give commands");
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = VishalServo.read();
if (val == 2)
{
Serial.println("Robot is On");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, HIGH);
delay (500);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, LOW);
delay(5);
}
if (val == 1)
{
Serial.println("Robot is turning left");
digitalWrite(MotorPin2, HIGH);
delay(500);
digitalWrite(MotorPin2, LOW);
delay(5);
}
if (val == 3)
{
Serial.println("Robot is turning right");
digitalWrite(MotorPin1, HIGH);
delay(500);
digitalWrite(MotorPin1, LOW);
delay(5);
}
if (val == 9)
{
Serial.println("Turning right");
VishalServo.write(i+5);
delay(5);
}
if (val == 7)
{
Serial.println("Turning left");
if (i >= 170)
{
delay(5);
}
if(i <= 170)
{
VishalServo.write(i-5);
delay(5);
}
}
if (val == 5)
{
Serial.println("Shoot");
digitalWrite(LedPin1, HIGH);
delay(50);
digitalWrite(LedPin1, LOW);
delay(5);
}
if (val == 8)
{
Serial.println("Centering");
VishalServo.writeMicroseconds(1500);
delay(5);
}
else
{
delay(5);
}
}
The motors and servo are powered via external batteries.
When I click 1,2 or 3 to run the motors the servo goes all the way to 180 degrees and comes back.
When the motors start rotating the servo goes to 180 degrees and when the motors stop rotating the servo goes back to 90 degrees.
How do I stop the servo from going to 180 degrees, as in the code it is not supposed to go to 180 degrees when I press 1,2 or 3...
I just found out that when I disconnect the servo's connection to the arduino PWM pin, then when I click 1,2 or 3 then the servo goes to 180 degrees but then it does not come back and each time 1 click 1,2 or 3 it twitches a bit.This whole thing happens when the servo is just powered via a 9 V battery ( not the Arduino's power, but a separate battery only for the servo) and the grounds of everything in my circuit is wired together but the data pin on the servo is not connected to any of the digital pins.
Does this mean it has something to do with the power supply.I used a voltage regulator to make the 9V into 5V for the servo.
Do you think that by posting the questions about the same sketch in multiple threads you get more answers?
People might be more inclined to answer if you found about about [ code ] tags, so we can read the sketch more easily. That would stop the forum turning parts of the code into smiley faces.
Posting same thread does not give same answers but they are of different topics so I posted them in different locations.
Like servo problem is in motors section and error in the code is in language section etc.
To avoid the forum software interpreting punctuation as smileys, you must learn to put your code into a CODE block. To do this, click on the button above the typing area that looks like a number sign or hash character. It will insert two 'tags' in your draft post. Insert the code between those two tags. Or, if you've already inserted your code as regular text, you can select it in the post editor and then press the CODE button. It will come out like this sentence.
Nothing...I just stays with the old Binary sketch uploaded statement.
I added a couple of servo.detach() in the program but that didn't help either, I also tried adding some VishalServo.writeMicroseconds(1500) to get it to center in between the two lines.But no luck.
Just for a test, add at the end of your loop() function a delay(2000) and see how your tank behaves in this case except that it'll react to command only every 2 seconds. If it's different than what you see normally, then you don't properly grasp how your hardware works, specially the servo.
Oh about that Sorry I thought you were asking about any error messages.
Yes It says Robotic tank
Then wait for 1 second
Then you can give commands now
And if I click 1 it says, And if I click 2 it says, And if I click 3 it says, And if I click 5 it says, And if I click 7 it says, And if I click 8 it says, And if I click 9 it says the things it is supposed to say. Nothing different is said, absolutely the same thing is said
vishalapr:
And if I click 1 it says, And if I click 2 it says, And if I click 3 it says, And if I click 5 it says, And if I click 7 it says, And if I click 8 it says, ... Nothing different is said, absolutely the same thing is said
vishalapr:
Does anyone of you think that there is a proble with pin 6 and maybe I should try some other pin for my servo...
Pin 6 should work fine. Also, after checking your code, the program looks correct and the output you get reasonable. It might be a good idea to go back to the hardware and check if you don't have some electrical problem there.
How about neatening up the code? You always do the delay (5), right? So you can move that out of each case. And for testing val against lots of values, use a switch statement. And how about doing the various cases in order? Like this:
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = VishalServo.read();
switch (val)
{
case 1:
Serial.println("Robot is turning left");
digitalWrite(MotorPin2, HIGH);
delay(500);
digitalWrite(MotorPin2, LOW);
break;
case 2:
Serial.println("Robot is On");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, HIGH);
delay (500);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, LOW);
break;
case 3:
Serial.println("Robot is turning right");
digitalWrite(MotorPin1, HIGH);
delay(500);
digitalWrite(MotorPin1, LOW);
break;
case 5:
Serial.println("Shoot");
digitalWrite(LedPin1, HIGH);
delay(50);
digitalWrite(LedPin1, LOW);
break;
case 7:
Serial.println("Turning left");
if(i <= 170)
{
VishalServo.write(i-5);
}
break;
case 8:
Serial.println("Centering");
VishalServo.writeMicroseconds(1500);
break;
case 9:
Serial.println("Turning right");
VishalServo.write(i+5);
break;
} // end of switch
delay(5);
} // end of loop
And if I click 1 it says Robot is turning left
And if I click 2 it says Robot is on
And if I click 3 it says Robot is turning right
And if I click 5 it says Shoot
And if I click 7 it says Turning left
And if I click 8 it says Centering
And if I click 9 it says Turning right