Need help ! combing two programs

I'm trying to combine 2 programs i want to move servos with a key stroke over wifi but i don't know how to get the program to read what it resaves

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <Servo.h>

Servo servo1; //wrist
Servo servo2; //hand
Servo servo3; //shoulder
Servo servo4; //elbow
Servo servo5; //bottom

//define initial positions of servos
const int init1 = 90;
const int init2 = 90
;
const int init3 = 90;
const int init4 = 90;
const int init5 = 90;

//define the increment in degrees. This is what we will use to move the servos
int inc = 1;

//define the delay between each iteration of movement. I will call it speed_ for simplicity
int speed_ = 10;

//define the initial postions of the servos
int pos1 = init1;
int pos2 = init2;
int pos3 = init3;
int pos4 = init4;
int pos5 = init5;

AsyncWebServer server(80);

const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Your WiFi SSID
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Your WiFi Password

void recvMsg(uint8_t *data, size_t len){
WebSerial.println("Received Data...");
String d = "";
for(int i=0; i < len; i++){
d += char(data[i]);
}

i need help here!!!!i think

}
void setup() {
Serial.begin(115200);
servo1.attach(7);
servo2.attach(6);
servo3.attach(5);
servo4.attach(4);
servo5.attach(3);

servo1.write(pos1);
servo2.write(pos2);
servo3.write(pos3);
servo4.write(pos4);
servo5.write(pos5);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// WebSerial is accessible at "/webserial" in browser
WebSerial.begin(&server);
WebSerial.msgCallback(recvMsg);
server.begin();
}

void loop() {
//possibly not needed?
Serial.flush();

//val is the variable we store the received data in
int val = Serial.read(d);

//and here

//condtional needed for loops to work properly
int i = 1;

//--------------------------------MOVE servo5 METHOD-----------------------------------------
while(val=='w' && i==1){
val = Serial.read();

//and and this point

//limitations that I put in so I can't break the servo cords
if(pos5 <= 180){

//increment position to move servo

pos5=pos5+inc;
servo5.write(pos5);

//used to break out of the loop and move different servo or do nothing
if(val!='w'){
i=0;
}
delay(speed_);
}
}
//---------------------------------change speed of arm method------------------------
if(val=='n' && i==1){
val = Serial.read();
if(inc < 5 && inc >= 1){

  //changing the increment size changes the speed essentially
  inc = inc - 1;
  i=0;
}
if(inc == 5){
  inc = inc - 1;
  i = 0;
}

}
if(val=='m' && i==1){
val = Serial.read();
if(inc < 5 && inc >= 1){
inc = inc + 1;
i=0;
}
if(inc == 0){
inc = 1;
i=0;
}
}
}

Welcome to the forum

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

im new to this just want help

If you format and present your code correctly then there is more chance of getting that help

Edit your post and add the code tags and see how much easier it is to read and copy for examination. To make it even easier use Auto Format in the IDE before posting it here in code tags

iv no idea what your on about dude

Did you read the advice in the link I posted ?
If not, do it now and come back with any queries

The problem when you post without code tags is that some characters in your code are mistaken for formatting markdown by the forum software. Therefore, if someone tries to copy your code it is likely not what you intended to post.

1 Like

Ok ill figer out how to do that when im home that makea a bit more sense

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-webserial-library/
  
  This sketch is based on the WebSerial library example: ESP32_Demo
  https://github.com/ayushsharma82/WebSerial
*/

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <Servo.h>


Servo servo1;       //wrist
Servo servo2;       //hand
Servo servo3;       //shoulder
Servo servo4;       //elbow
Servo servo5;       //bottom

//define initial positions of servos
const int init1 = 90;
const int init2 = 90
;
const int init3 = 90;
const int init4 = 90;
const int init5 = 90;

//define the increment in degrees. This is what we will use to move the servos
int inc = 1;

//define the delay between each iteration of movement. I will call it speed_ for simplicity
int speed_ = 10;

//define the initial postions of the servos
int pos1 = init1;
int pos2 = init2;
int pos3 = init3;
int pos4 = init4;
int pos5 = init5;





AsyncWebServer server(80);

const char* ssid = "REPLACE_WITH_YOUR_SSID";          // Your WiFi SSID
const char* password = "REPLACE_WITH_YOUR_PASSWORD";  // Your WiFi Password

void recvMsg(uint8_t *data, size_t len){
  WebSerial.println("Received Data...");
  String d = "";
  for(int i=0; i < len; i++){
    d += char(data[i]);
  }
  
}



void setup() {
  Serial.begin(115200);
  servo1.attach(7);
servo2.attach(6);
servo3.attach(5);
servo4.attach(4);
servo5.attach(3);
  
  servo1.write(pos1);
servo2.write(pos2);
servo3.write(pos3);
servo4.write(pos4);
servo5.write(pos5);
  
  
  
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  // WebSerial is accessible at "<IP Address>/webserial" in browser
  WebSerial.begin(&server);
  WebSerial.msgCallback(recvMsg);
  server.begin();
}

void loop() {  
//possibly not needed?  
Serial.flush();

//val is the variable we store the received data in
int val = Serial.read();

//condtional needed for loops to work properly
int i = 1;


//--------------------------------MOVE servo5 METHOD-----------------------------------------
while(val=='w' && i==1){
  val = Serial.read(); 

   //limitations that I put in so I can't break the servo cords
  if(pos5 <= 180){

    //increment position to move servo
  pos5=pos5+inc;
  servo5.write(pos5);

  //used to break out of the loop and move different servo or do nothing
  if(val!='w'){
    i=0;
  }
  delay(speed_);
  }
  }
  //---------------------------------change speed of arm method------------------------
  if(val=='n' && i==1){
    val = Serial.read();
    if(inc < 5 && inc >= 1){

      //changing the increment size changes the speed essentially
      inc = inc - 1;
      i=0;
    }
    if(inc == 5){
      inc = inc - 1;
      i = 0;
    }
  }
  if(val=='m' && i==1){
    val = Serial.read();
    if(inc < 5 && inc >= 1){
      inc = inc + 1;
      i=0;
    }
    if(inc == 0){
      inc = 1;
      i=0;
    }
  } 
 } 

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