bluetooth servo control

Hi everyone

have no idea of what im doing basically! I have searched and no avail but i want to control a single servo with HM-10 module that i can connect my iphone 6 to. All i want it to do is when i slide up it goes up and stay there then when i slide down it goes down and stay there.

can anyone help please and no stupid answers as well

thank you

ok i have this code which works with a joy stick but how do i add the bluetooth code to it and take the joystick out?

#include <Servo.h>

#define mainArm 0


Servo servo[1];      //code used for attaching upto 4 servos

byte angle[1] = {90};       // middle point for servo angle

byte potPin[4] = {A0, A1};  // input pins to attach your potentiometers
byte servoPin[1] = {3};    // input pins to attach servos

void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");


  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
}

void readPotentiometers() {
  int potVal;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    
    if (potVal < 200) {         // dead zone for the joystick I used is 200 to 550.
      angle[n] += 1;
      if (angle[n] > 160) {
        angle[n] = 170;
      }
    }
    
    if (potVal > 550) {         // deadzone upper value
      angle[n] -= 1;
      if (angle[n] < 10) {
        angle[n] = 10;
      }
    }

  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].write(angle[n]);
  }
}

Your moveServos() function expects to find values in the array angle[]

You need to write a function to receive data using Bluetooth and save it in that array.

Have a look at the examples in Serial Input Basics which should also be suitable for receiving Bluetooth data. There is also a parse example that shows how to convert received text into numbers.

You should be aware that the Servo library does not like SoftwareSerial and you may need to use the ServoTimer2 library

...R

i have this code that will work with the app i want to use but i dont know what to take out and put a servo in, i.e theres motors and buzzers which i want to take out and replace them with a single servo

#include <Servo.h>

int val = 0;
int horizontal = 50;
int vertical = 50;
int A_state = 0;
int B_state = 0;
char data[5];
int m1;
int m2;

void setup()
{

   Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");


  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

  // silnik M1
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  // silnik M2
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);

  //buzzer
  pinMode(16, OUTPUT);
  // diody LED
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);




  Serial.begin(9600); // komunikacja Arduino->Komputer przez kabel USB
  Serial.begin(9600); // komunikacja Arduino->HM-10 bluetooth

    motor1(1, 255);
  motor2(1, 255);

}

void loop()
{


  if (Serial.available())
  {

    Serial.readBytes(data, 5); // odbior danych z aplikacji

    horizontal = int(data[1]); // joystick os pozioma
    vertical = int(data[2]); // joystick os pionowa
    A_state = int(data[3]); // lewy przycisk
    B_state = int(data[4]); // prawy przycisk


    horizontal = horizontal * 2 - 100;
    vertical = vertical * 2 - 100;


    Serial.print("horizontal = ");
    Serial.print(horizontal);
    Serial.print(" vertical = ");
    Serial.print(vertical);
    Serial.print(" A = ");
    Serial.print(A_state);
    Serial.print(" B = ");
    Serial.println(B_state);
  }

  digitalWrite(16, A_state);
  digitalWrite(10, B_state);
  digitalWrite(9, B_state);

  m1 = vertical - horizontal;
  m2 = vertical + horizontal;

  int mm1 = min(255, 2.7 * abs(vertical - horizontal));
  int mm2 = min(255, 2.7 * abs(vertical + horizontal));


  if (m1 > 0)
    motor1(true, mm1);
  else
    motor1(false, mm1);


  if (m2 > 0)
    motor2(true, mm2);
  else
    motor2(false, mm2);


}


void motor1(boolean direction, int power)
{
  digitalWrite(2, direction);
  digitalWrite(4, !direction);
  analogWrite(3, power);
}

// funkcja sterująca silnikiem podłączonym do gniazda M2
void motor2(boolean direction, int power)
{
  digitalWrite(7, direction);
  digitalWrite(8, !direction);
  analogWrite(5, power);
}

so if pos want the first code combined with this one

firephinix: so if pos want the first code combined with this one

Maybe this Simple Merge Demo will give you a starting point.

...R

yeah doesnt help at all cos i have no idea what bits to take out and what bits to put in

firephinix: yeah doesnt help at all cos i have no idea what bits to take out and what bits to put in

That sounds like you want someone to re-write the code for you. If so I think you need to ask in the Gigs and Collaborations section and be prepared to pay.

Alternatively you could make make your best attempt at merging the code and if it does not work you could post your code with an explanation of what it actually does and what you want it to do.

...R