**The issue with this code. I can get the motors to work such as going forward, backward, left and right. Problem is my servos won't work, and it keeps spinning when I turn on the system. Also, I'm using MIT-app so that's why the i have data==a. Its basically indicating my push button 'a' from the MIT-app. However the servo does work if I do the code like this separately:
SERVOS CODE
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(7); //Servo is on pin 7
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
char data = Serial.read();
if(data=='a') //the pushbutton that i assign from MIT-app is 'a' = 0 degree
{
myservo.write(0);
}
if(data=='b') //the pushbutton that i assign from MIT-app is 'b' = 45 degree
{
myservo.write(45);
}
if(data=='c') //the pushbutton that i assign from MIT-app is 'c' = 90 degree
{
myservo.write(90);
}
if(data=='d') //the pushbutton that i assign from MIT-app is 'd' = 180 degree
{
myservo.write(180);
}
}
}
THIS IS THE FULL CODE*
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
char t;
void setup() {
pinMode(13,OUTPUT); //left motor forward
pinMode(12,OUTPUT); //left motor reverse
pinMode(11,OUTPUT); //right motor forward
pinMode(10,OUTPUT); //right motor reverse
pinMode(9,OUTPUT); //Led
myservo.attach(7);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
t = Serial.read();
Serial.println(t);
}
if(Serial.available()>0)
{
char data = Serial.read();
if(data=='a')
{
myservo.write(0);
}
if(data=='b')
{
myservo.write(45);
}
if(data=='c')
{
myservo.write(90);
}
if(data=='d')
{
myservo.write(180);
}
}
if(t == 'F'){ //move forward(all motors rotate in forward direction)
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
}
else if(t == 'B'){ //move reverse (all motors rotate in reverse direction)
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
}
else if(t == 'L'){ //turn right (left side motor rotate in forward direction, right side motor doesn't rotate)
digitalWrite(11,HIGH);
digitalWrite(13,LOW);
}
else if(t == 'R'){ //turn left (right side motor rotate in forward direction, left side motor doesn't rotate)
digitalWrite(13,HIGH);
}
else if(t == 'W'){ //turn led on or off)
digitalWrite(9,HIGH);
}
else if(t == 'w'){
digitalWrite(9,LOW);
}
else if(t == 'S'){ //STOP (all motors stop)
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
delay(100);
}

