Turning servo on and off using the bluetooth module

I have created the codes for randomly running four servos using arduino. Now, I'm trying to turn the sketch on and off using bluetooth. Any ideas on how to do that?

Have you managed to send a character to the Arduino using Bluetooth ?

There are plenty of examples around I would have thought.

There are plenty of examples around I would have thought.

Over 2 million hits for a Google search for "arduino Bluetooth servo control".

You are right! And I have managed to connect a servo via bluetooth and got it to move in angles. However, I can't seem to just get it to turn on and off.

Please describe what you mean by "turn the sketch on and off"?

When you say

get it to turn on and off

what do you mean?

Turn on and turn off power to the arduino board?
or
Pause movement of the servos and resume movement of the servos?

The second one so I can pause and re-start.

Awesome. Pause and then resume servo movement.

Have you done any work with bluetooth yet? Walked through and of the examples?

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial mySerial(0, 1); // RX | TX

Servo servo;

int servoPin = 9;
int servoAngle = 0; // servo position in degrees

char command;

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("You're connected via Bluetooth");
servo.attach(9);
}

void loop() {
if (mySerial.available())
{
command=(mySerial.read());
if (command=='1')
{
Serial.println("Servo motor to 10 degrees");
servo.write(160);
delay(2000); // Wait for 2000 millisecond(s)
servo.write(70);
delay(2000); // Wait for 2000 millisecond(s)
servo.write(20);
delay(2000); // Wait for 2000 millisecond(s)

}

else if (command=='0')
{
Serial.println("Servo motor to 120 degrees");
servo.write(120);
delay(500);
}

}

}

SoftwareSerial mySerial(0, 1); // RX | TX

Why use the hardware serial pins for software serial? What does that code actually do? What do you want the code to do?

Why haven't you read the "how to use this forum-please read" stickies?

I believe that this has nothing to do with the location of the pins. This code is an example for a single servo. I have a sketch for randomly moving four servos and I would like to just turn it on and off via bluetooth (using the MIT app inventor).

As mentioned in reply #9, go read the sticky post named How to use this forum - please read.
Pay attention to how to post code.
Consider auto formatting your code. Makes it a lot easier to read.

Help me out. Describe what you expect your sketch to do.
Then describe how the the actual results differ from your expectation.

This sketch instructs the servo to move using degrees (from 160 to 70 to 20). I designed this to make sure that the arduino and bluetooth communicate. This works. However, I would like to just get it to turn on and off and I can't get the servo to do that without using angles.

If your loop does not do a write() to the servo object, then the servos stop moving don't they?

You use the character 1 to do the 160,70,20 dance.
Character 0 moves the servo to 120.
What happens if you give it ANY other command? No servo.write(), right?
Doesn't that pause your servos until you give it a 1 or 0 again?

I would like to just get it to turn on and off

Do you mean that you want the servos to immediately stop their movement and stay there until commanded to move again ?

If so, then you will need to stop using delay() and use millis() for timing as delay() cannot be interrupted. You will also need to move your servos in small steps so that they can be stopped at their current angle rather than moving to the commanded angle.

This is the sketch that I have designed for arduino and it works when I connect it to a 9V battery. I would like to control this sketch using bluetooth by simply turning it on and off or pause remotely.

#include <Servo.h> 

///////////// 
Servo s2;  //     #######################
Servo s3;  //     #                     #
Servo s4;
Servo s5;  //     #  (2)    (3)    (4)  #   #######################
/////////////

Servo exec [] = {s2, s3, s4, s5};

int whichMotors;
int quantatyMotor;



void setup(){ 

 Serial.begin(9600);
 
 s2.attach(5); 
 s3.attach(7);
 s4.attach(9);
 s5.attach(3);
   
 randomSeed(analogRead(0));
} 

void loop(){
 

 quantatyMotor = random(0, 4);
 
 
 
 
  for (int i=0; i <= quantatyMotor; i++){
    
     
     
     
 int quantaty = random(4);
 for (int i=0; i<quantaty; i++){
   int servo = random(4);
   exec[servo].write(160);
   delay(900);
   exec[servo].write(70);
 delay(5000);
 exec[servo].write(5);
 delay(3000);
 }
    

}
  }

Step 1 would be to refactor the program to use millis() for timing instead of delay() so that it can be responsive to the Bluetooth input. As it is it spends most of its time (nearly 9 seconds) doing nothing during each iteration of the for loop. During that time it cannot read or react to serial input from Bluetooth.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Thank you for the advise but it hasn't worked so far probably because we are over-complicating things. I just want to use bluetooth to turn the sketch on and off.

tomstell:
Thank you for the advise but it hasn't worked so far probably because we are over-complicating things. I just want to use bluetooth to turn the sketch on and off.

Have you tried using millis() for timing in your sketch ? If so then please post what you tried and explain what problems you had.

I just want to use bluetooth to turn the sketch on and off.

That makes no sense. The sketch must be running if it is going to be able to detect incoming data on the bluetooth data.

You can turn a sketch off. Or on.

So, try again. What ARE you trying to do?