Why my servo only run once?

I have a very huge problem with my servo.
It should be run DanceServo when the serial monitor shows number 49.
But the servo turns only on the first detecting 49. other only print out the word.
What is the problem with it? Sorry for my bad english.

Here is the code;

#include <SPI.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SD.h>
#include <Servo.h>
#define servoPin 5
#define SD_ChipSelectPin 4
Servo myservo;
int md;
int val;
int angle = 0;
int potpin = A0;
TMRpcm tmrpcm;

void setup() {
myservo.attach(servoPin);
Serial.begin(9600);
tmrpcm.speakerPin=9;
tmrpcm.setVolume(5);

if(!SD.begin(SD_ChipSelectPin)){
Serial.println("SD fails");
return;
}

}

void DanceServo()
{

      myservo.write(90);
      Serial.println("turn 90");
      delay(1000);
      myservo.write(180);
      Serial.println("turn 180");
      delay(1000);
      myservo.write(0);
      Serial.println("turn 0");
      delay(1000);

}

void RandomMusic()
{
int Music = random(3);
switch(Music)
{
case 0 :
tmrpcm.stopPlayback();

          tmrpcm.play("1.wav");
          Serial.println("1v");
          
         break;
      case 1 : 
      tmrpcm.stopPlayback();
      
          tmrpcm.play("2.wav");
          Serial.println("2v");
          
         break; 
      case 2 : tmrpcm.stopPlayback();
      
          tmrpcm.play("3.wav");
          Serial.println("3v");

         break;

}

void loop() {

while(1)
{
if(Serial.available()>0)
{
md=Serial.read();
switch(md)
{
case 49:
Serial.println("Got it");
DanceServo();
break;
}

Your code is not complete.

Where in the code do you print "8v"?

Where in the code do you print "49"?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your loop() does nothing until it receives the character '1' (the character with the ASCII code 49). How many times did you send the character '1'?

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