1

I want to control the 2 servos(pan tilt) using accelerometer data value from android app. Im using nodeMCU v3, 2 servos(pan tilt) and smartphone.

Connection:

servos(pan tilt) is connected to 5 v battery charger and in pin GPIO12 and GPIO14.

nodeMCU is connected to laptop port.

My problem is, my servos (pan tilt) can't read my accelerometer data value.

Here my code:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Servo.h>
#define DEBUG 1

const char* ssid = "Connect";
const char* password = "ececonnect";

WiFiUDP Udp;
unsigned int port = 5514;
char packet[255];

// Create servo objects
Servo panServo;
Servo tiltServo;

// Center servos
int tiltVal = 90; 
int panVal =90; 

String inText;
float value0, value1, value2;

void setup()
{
panServo.attach(14); 
tiltServo.attach(12);


Serial.begin(115200);
Serial.println();

Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connection Successful");
Udp.begin(port);
Serial.printf("Listener started at IP %s, at port %d\n", 
WiFi.localIP().toString().c_str(), port);

Serial.println("Android Sensor Type No: ");
Serial.println("1- ACCELEROMETER  (m/s^2 - X,Y,Z)");
Serial.println("2- GYROSCOPE (rad/s - X,Y,Z)");
Serial.flush();
}



void loop()
{
Serial.flush();
int packetSize = Udp.parsePacket();
if (packetSize)
{
//Serial.printf("Received %d bytes from %s, port %d\n", packetSize, 
Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(packet, 255);
if (len > 0)
{
packet[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", packet);

int sensorType = 0;
unsigned long logCount  = 0L;

char getChar = ' ';  //read serial
getChar = Serial.read();

sensorType = Serial.parseInt(); // read sensor typr
logCount  = Serial.parseInt();  // read total logged sensor readings
value0 = Serial.parseFloat();  // 1st sensor value
value1 = Serial.parseFloat();  // 2rd sensor value if exists

if (DEBUG) {
Serial.print("Sensor type: ");
Serial.println(sensorType);
Serial.print("Sensor log#: ");
Serial.println(logCount );
Serial.print("Val[0]: ");
Serial.println(value0);
Serial.print("Val[1]: ");
Serial.println(value1);
Serial.print("Val[2]: ");
Serial.println(value2);
Serial.println("-----------------------");
delay(10);
}



if (sensorType !=1) return;   

panVal = value0; // value0 = X sensor reading
tiltVal = value1;  // value1 = Y sensor reading

tiltVal = map(tiltVal, 10, -10, 0, 179);   // Map Accelerometer Y value to 
tilt servo angle. 
tiltServo.write(tiltVal);
delay(10);

panVal = map(panVal, -10, 10, 0, 179);  // Map Accelerometer X value to pan 
servo angle.
panServo.write(panVal);     
delay(10); 
}

}
6
  • 1
    Information we`re missing: Your complete wireup / circuit, actual output, expected output. – Maximilian Gerhardt May 16 '18 at 15:18
  • Possible duplicate of im noob, help for control 2 servos using accelerometer – gre_gor May 16 '18 at 15:45
  • i solve that problem earlier sir.. and im stuck with another problem.. sorry for this.. – Wyeth Gamba May 16 '18 at 16:05
  • If you found the solution yourself then you should have posted it as an answer, not deleted it. – gre_gor May 16 '18 at 16:16
  • sorry sir... i didnt know, my mistake for not reading properly the instruction... – Wyeth Gamba May 16 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.