Go Down

Topic: Servo motor not working as per code (Read 371 times) previous topic - next topic

Jovinufaber

The servo motor doesn't work as per code. There is no movement even though there is vibration of motor which also stops after some time. There are two servo motors in parallel connected to the 5V of L298N. I connected a fresh servo and even that doesn't work. The ground of the servos are common to the ground of Arduino and L298N. If I rotate the servo motor (by a small angle) before powering up the circuit, it comes to a fixed position when powered on. What can be the possible reason? please find the code attached for reference. I am making a bluetooth voice controlled toy.

AWOL

Quote
please find the code attached for reference
I can find it, but I can't open it.

Why not simply post the code?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

slipstick

What servos? L298 module or bare component? What is the L298 powered with?

It sound like a power problem but with no real information it's difficult to tell. If you mean you are using Vss from the L298N then that isn't intended to be an output...it's supposed to be used to input a clean 5V to the L298N internal logic.

Steve

Jovinufaber

#3
Sep 14, 2018, 02:03 pm Last Edit: Sep 15, 2018, 08:28 am by Jovinufaber
Code: [Select]
String voice;

int flashlight = 8;
int eyebrows = 2;

int front = 14;
int back = 15;
int right = 16;
int left = 17;
int fromslave = 18;
int racemode = 19;

#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
#include <Servo.h> //Servo library

Servo lefthand;
Servo righthand;  

TMRpcm speech;

#include <SoftwareSerial.h>
SoftwareSerial BT(4,3); //RX | TX

void setup() {
    BT.begin(9600);
    Serial.begin(9600);
    speech.speakerPin=9;
    Serial.begin(9600);
    if(!SD.begin(SD_ChipSelectPin))
    {
    Serial.println("SD fail");
    return;
    }
    speech.quality(1);
    speech.setVolume(6);

    lefthand.attach(5);
    righthand.attach(6);
   
    pinMode(flashlight, OUTPUT);
    pinMode(eyebrows, OUTPUT);

    pinMode(front, OUTPUT);
    pinMode(back, OUTPUT);
    pinMode(right, OUTPUT);
    pinMode(left, OUTPUT);
    pinMode(fromslave, INPUT);
    pinMode(racemode, INPUT);
   
}
void blink()
{
       digitalWrite(eyebrows, LOW);
       delay(200);
       digitalWrite(eyebrows, HIGH);
       delay(200);
       digitalWrite(eyebrows, LOW);
       delay(200);
       digitalWrite(eyebrows, HIGH);
       delay(200);
       digitalWrite(eyebrows, LOW);
       delay(200);
}
void loop() {

  digitalWrite(eyebrows, HIGH);
  int t = digitalRead(fromslave);
  if(t == 1)
  {
    blink();
  }
 
  digitalWrite(racemode,LOW);
 
  while(BT.available()) {
 
  delay(500);
 
 
  digitalWrite(right, LOW);
  digitalWrite(left, LOW);
     
  char c = BT.read();
  if(c=='#')
  {break; }
  voice += c;
  }

  if (voice.length() > 0) {
      if (voice == "move forward" || voice == "forward" || voice == "front") {
      speech.play("3.wav");
      delay(1500);
      digitalWrite(front, HIGH);
      delay(500);
      digitalWrite(front, LOW);
      blink();
      }
     else if (voice == "move backward" || voice == "backward" || voice == "back" || voice == "move back") {
     speech.play("2.wav");
     delay(1500);
     digitalWrite(back, HIGH);
     delay(500);
     digitalWrite(back, LOW);
     blink();
     }
     else if (voice == "move right" || voice == "turn right" || voice == "right") {
     speech.play("5.wav");
     delay(1500);
     digitalWrite(right, HIGH);
     delay(500);
     digitalWrite(right, LOW);
     blink();
     }
     else if (voice == "move left" || voice == "turn left" || voice == "left") {
     speech.play("4.wav");
     delay(1500);
     digitalWrite(left, HIGH);
     delay(500);
     digitalWrite(left, LOW);
     blink();
     }
     else if (voice == "hi" || voice == "hello" || voice == "hey" || voice == "hey there") {
     speech.play("1.wav");
     lefthand.write(90);
     righthand.write(90);
     delay(1000);
     lefthand.write(0);
     righthand.write(0);
       digitalWrite(eyebrows, LOW);
       delay(500);
       digitalWrite(eyebrows, HIGH);
       delay(500);
       digitalWrite(eyebrows, LOW);
       delay(500);
       digitalWrite(eyebrows, HIGH);
       delay(500);
       digitalWrite(eyebrows, LOW);
       delay(500);
       digitalWrite(eyebrows, HIGH);
       delay(2500);
     righthand.write(0);
     }
     else if (voice == "play a song" || voice == "song" || voice == "play song" || voice == "need a song") {
     speech.play("6.wav");
     blink();
     }
     else if (voice == "turn on flashlight" || voice == "flashlight on" || voice == "light on" || voice == "flashlight" || voice == "light") {
     digitalWrite(flashlight, HIGH);
     blink();
     }
     else if (voice == "turn off flashlight" || voice == "flashlight off" || voice == "light off") {
     digitalWrite(flashlight, LOW);
     blink();
     }
     else if (voice == "joke" || voice == "tell me a joke" || voice == "say a joke" || voice == "tell me joke") {
     int randnumber = random(1,10);
     
     switch (randnumber) {
     case 1:
     speech.play("7.wav");
     break;
     case 2:
     speech.play("8.wav");
     break;
     case 3:
     speech.play("9.wav");
      break;
     case 4:
     speech.play("10.wav");
     break;
     case 5:
     speech.play("11.wav");
     break;
     case 6:
     speech.play("12.wav");
     break;
     case 7:
     speech.play("13.wav");
     break;
     case 8:
     speech.play("14.wav");
     break;
     case 9:
     speech.play("15.wav");
     break;
     default:
     speech.play("7.wav");
      }
       delay(500);
     }
     else if (voice == "enter race mode" || voice == "race mode" || voice == "race" || voice == "race mode on" || voice == "move fast" || voice == "fast") {
      digitalWrite(racemode, HIGH);
      while(true){
      if(BT.available()){
      char input = BT.read();
      if(input == 'F'){
      digitalWrite(front, HIGH);
      }
      else if(input == 'B'){
      digitalWrite(back, HIGH);
      }
      else if(input == 'R'){
      digitalWrite(right, HIGH);  
      }
      else if(input == 'L'){
      digitalWrite(left, HIGH);  
      }
      else if(input == 'X'){
      break;  
      }
      else
      { digitalWrite(front, LOW);
      digitalWrite(back, LOW);
      digitalWrite(right, LOW);
      digitalWrite(left, LOW);
        }
       }
      }
      digitalWrite(racemode, LOW);
      blink();
     
     }
     else {
     Serial.print("I couldn't understand");
     }
 
  Serial.println(voice);
  voice="";
  }
}

Jovinufaber

What servos? L298 module or bare component? What is the L298 powered with?

It sound like a power problem but with no real information it's difficult to tell. If you mean you are using Vss from the L298N then that isn't intended to be an output...it's supposed to be used to input a clean 5V to the L298N internal logic.

Steve
It's a L298N module. I faced the same problem when I used arduino power supply.

Jovinufaber

You really need to study that assertion.
Sorry, I didn't get you. Do you mean the topic? I mean that the servo does work. But not the way it is coded to. It comes to the default position but does nothing else.

I posted the wrong code. Please find the edited post

AWOL

That code looks nothing like the code you posted before.

Still no code tags.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Jovinufaber

That code looks nothing like the code you posted before.

Still no code tags.
I know

slipstick

It's a L298N module. I faced the same problem when I used arduino power supply.
What module? Link please. And what about all the other questions?

Steve

Idahowalker

When I look through the 'code' I see:
Code: [Select]

  lefthand.write(90);
       righthand.write(90);
       delay(1000);
       lefthand.write(0);
       righthand.write(0);

as the only servo torque thingies.

Just maybe:

I also see the use of the default servo set up of using attach(). If one was to look at this page: https://www.arduino.cc/en/Reference/ServoAttach about servo attach, one will find that servo attach has 2, yes 2 ways to initialize the servo and it may be the 2nd use of servo attach that might interest you.

BTW:

Have you ever discovered the for loop and code tags?

Idahowalker

And another thing
Code: [Select]
else if (voice == "hi" || voice == "hello" || voice == "hey" || voice == "hey there") {
       speech.play("1.wav");
       lefthand.write(90);
       righthand.write(90);
       delay(1000);
       lefthand.write(0);
       righthand.write(0);


Your code is expecting a 'voice' command to rotate. I bet you may not have figured that if you say "hello" how long the "hello" must persist in the air, before your code gets around to... Just count up the delays it takes to get to this art of the code and compare that time to the time of "hello".

Next are you powering the servo by connecting it to the Arduino 5V line. Try, the Vin, you might get better results.

And have you discovered how to write functions?

jremington

#11
Sep 15, 2018, 02:32 am Last Edit: Sep 15, 2018, 02:33 am by jremington
OP: please edit your reply #3 to add code tags.

Always use a separate power supply for the servos and connect the grounds.

Jovinufaber

What module? Link please. And what about all the other questions?

Steve
https://www.amazon.com/Controller-Module-Bridge-Stepper-Arduino/dp/B00HNHUYSG

When I look through the 'code' I see:
Code: [Select]

  lefthand.write(90);
       righthand.write(90);
       delay(1000);
       lefthand.write(0);
       righthand.write(0);

as the only servo torque thingies.

Just maybe:

I also see the use of the default servo set up of using attach(). If one was to look at this page: https://www.arduino.cc/en/Reference/ServoAttach about servo attach, one will find that servo attach has 2, yes 2 ways to initialize the servo and it may be the 2nd use of servo attach that might interest you.

BTW:

Have you ever discovered the for loop and code tags?
Thanks for the 2 methods. I just discovered what code tag is. I haven't used for loop much but can use it.

And another thing
Code: [Select]
else if (voice == "hi" || voice == "hello" || voice == "hey" || voice == "hey there") {
       speech.play("1.wav");
       lefthand.write(90);
       righthand.write(90);
       delay(1000);
       lefthand.write(0);
       righthand.write(0);


Your code is expecting a 'voice' command to rotate. I bet you may not have figured that if you say "hello" how long the "hello" must persist in the air, before your code gets around to... Just count up the delays it takes to get to this art of the code and compare that time to the time of "hello".

Next are you powering the servo by connecting it to the Arduino 5V line. Try, the Vin, you might get better results.

And have you discovered how to write functions?
I will try out some delay() statements to check if timing is the issue. I know to write basic functions.

Jovinufaber

#13
Sep 15, 2018, 10:35 am Last Edit: Sep 15, 2018, 10:53 am by Jovinufaber
The servo motor works perfectly fine when it is asked to move at the beginning of the void loop() as seen in the revised code below. So it's not a power supply issue. It doesn't however work after entering the if-else comparison in the while loop. It's like the execution just jumps over the servo part after the audio file is played.
I even tried putting the servo commands within a function and called it, but it's not working within the if-else inside the while loop.



Code: [Select]
String voice;

int flashlight = 8;
int eyebrows = 2;

int front = 14;
int back = 15;
int right = 16;
int left = 17;
int fromslave = 18;
int racemode = 19;

#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
#include <Servo.h> //Servo library
 
Servo lefthand;
Servo righthand; 

TMRpcm speech;

#include <SoftwareSerial.h>
SoftwareSerial BT(4,3); //RX | TX

void setup() {
      BT.begin(9600);
      speech.speakerPin=9;
      Serial.begin(9600);
      if(!SD.begin(SD_ChipSelectPin))
      {
      Serial.println("SD fail");
      return;
      }
      speech.quality(1);
      speech.setVolume(6);

      lefthand.attach(5);
      righthand.attach(6);
     
      pinMode(flashlight, OUTPUT);
      pinMode(eyebrows, OUTPUT);

      pinMode(front, OUTPUT);
      pinMode(back, OUTPUT);
      pinMode(right, OUTPUT);
      pinMode(left, OUTPUT);
      pinMode(fromslave, INPUT);
      pinMode(racemode, INPUT);
     
}
void blink()
{
         digitalWrite(eyebrows, LOW);
         delay(200);
         digitalWrite(eyebrows, HIGH);
         delay(200);
         digitalWrite(eyebrows, LOW);
         delay(200);
         digitalWrite(eyebrows, HIGH);
         delay(200);
         digitalWrite(eyebrows, LOW);
         delay(200);
}
void loop() {
 
    lefthand.write(0);
    righthand.write(0);
    delay(500);
    lefthand.write(90);
       righthand.write(90);
       delay(500);
     lefthand.write(0);
    righthand.write(0); 
    digitalWrite(eyebrows, HIGH);
    int t = digitalRead(fromslave);
    if(t == 1)
    {
      blink();
    }
   
    digitalWrite(racemode,LOW);
   
    while(BT.available()) {
   
    delay(500);
   
   
    digitalWrite(right, LOW);
    digitalWrite(left, LOW);
       
    char c = BT.read();
    if(c=='#')
    {break; }
    voice += c;
    }

    if (voice.length() > 0) {
        if (voice == "move forward" || voice == "forward" || voice == "front") {
        speech.play("3.wav");
        delay(1500);
        digitalWrite(front, HIGH);
        delay(500);
        digitalWrite(front, LOW);
        blink();
        }
       else if (voice == "move backward" || voice == "backward" || voice == "back" || voice == "move back") {
       speech.play("2.wav");
       delay(1500);
       digitalWrite(back, HIGH);
       delay(500);
       digitalWrite(back, LOW);
       blink();
       }
       else if (voice == "move right" || voice == "turn right" || voice == "right") {
       speech.play("5.wav");
       delay(1500);
       digitalWrite(right, HIGH);
       delay(500);
       digitalWrite(right, LOW);
       blink();
       }
       else if (voice == "move left" || voice == "turn left" || voice == "left") {
       speech.play("4.wav");
       delay(1500);
       digitalWrite(left, HIGH);
       delay(500);
       digitalWrite(left, LOW);
       blink();
       }
       else if (voice == "hi" || voice == "hello" || voice == "hey" || voice == "hey there") {
       speech.play("1.wav");
       delay(1500);
       lefthand.write(90);
       righthand.write(90);
       delay(1000);
       lefthand.write(0);
       righthand.write(0);
       blink();
       }
       else if (voice == "play a song" || voice == "song" || voice == "play song" || voice == "need a song") {
       speech.play("6.wav");
       blink();
       }
       else if (voice == "turn on flashlight" || voice == "flashlight on" || voice == "light on" || voice == "flashlight" || voice == "light") {
       digitalWrite(flashlight, HIGH);
       blink();
       }
       else if (voice == "turn off flashlight" || voice == "flashlight off" || voice == "light off") {
       digitalWrite(flashlight, LOW);
       blink();
       }
       else if (voice == "joke" || voice == "tell me a joke" || voice == "say a joke" || voice == "tell me joke") {
       int randnumber = random(1,10);
       
       switch (randnumber) {
       case 1:
       speech.play("7.wav");
       break;
       case 2:
       speech.play("8.wav");
       break;
       case 3:
       speech.play("9.wav");
        break;
       case 4:
       speech.play("10.wav");
       break;
       case 5:
       speech.play("11.wav");
       break;
       case 6:
       speech.play("12.wav");
       break;
       case 7:
       speech.play("13.wav");
       break;
       case 8:
       speech.play("14.wav");
       break;
       case 9:
       speech.play("15.wav");
       break;
       default:
       speech.play("7.wav");
        }
         delay(500);
       }
       else if (voice == "enter race mode" || voice == "race mode" || voice == "race" || voice == "race mode on" || voice == "move fast" || voice == "fast") {
        digitalWrite(racemode, HIGH);
        while(true){
        if(BT.available()){
        char input = BT.read();
        if(input == 'F'){
        digitalWrite(front, HIGH);
        }
        else if(input == 'B'){
        digitalWrite(back, HIGH);
        }
        else if(input == 'R'){
        digitalWrite(right, HIGH); 
        }
        else if(input == 'L'){
        digitalWrite(left, HIGH); 
        }
        else if(input == 'X'){
        break; 
        }
        else
        { digitalWrite(front, LOW);
        digitalWrite(back, LOW);
        digitalWrite(right, LOW);
        digitalWrite(left, LOW);
          }
         }
        }
        digitalWrite(racemode, LOW);
        blink();
       
       }
       else {
       Serial.print("I couldn't understand");
       }
   
    Serial.println(voice);
    voice="";
    }
}

slipstick

So, obvious question...does it correctly play 1.wav which it should do before moving the servos?

Why not add a few Serial.prints so you can see what exactly is in the variable voice and so what bits of the code it really gets into?

Steve


Go Up