Can't get my code to work for the Servos and motor

**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);
}

you are doing 'Serial.available()' twice in your combined code which is probably why it isnt working as intended IMHO

try something like this:
(Compiles, NOT tested!)

#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 (t == 'a')
  {
    myservo.write(0);
  }
  else if (t == 'b')
  {
    myservo.write(45);
  }
  else if (t == 'c')
  {
    myservo.write(90);
  }
  else if (t == 'd')
  {
    myservo.write(180);
  }
  else 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);
}

hope that helps....

What Arduino are you using?

Why have you included SoftwareSerial.h in your full code when you are not using SoftwareSerial for anything? It interferes with Servo.h.

What is your MIT-app running on and how is that connected to the Arduino? Is it sending both the commands for the servo AND the commands for the motors?

Steve

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

sherzaad:
you are doing 'Serial.available()' twice in your combined code which is probably why it isnt working as intended IMHO

try something like this:
(Compiles, NOT tested!)

#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 (t == 'a')
 {
   myservo.write(0);
 }
 else if (t == 'b')
 {
   myservo.write(45);
 }
 else if (t == 'c')
 {
   myservo.write(90);
 }
 else if (t == 'd')
 {
   myservo.write(180);
 }
 else 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);
}




hope that helps....

sherzaad:
you are doing 'Serial.available()' twice in your combined code which is probably why it isnt working as intended IMHO

try something like this:
(Compiles, NOT tested!)

#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 (t == 'a')
 {
   myservo.write(0);
 }
 else if (t == 'b')
 {
   myservo.write(45);
 }
 else if (t == 'c')
 {
   myservo.write(90);
 }
 else if (t == 'd')
 {
   myservo.write(180);
 }
 else 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);
}




hope that helps....

Thank you. It does help. This is my first time posting in this forum, so I'm not sure if the format that i posted was correct.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.