Another Function inside void Loop()?

Is it possible to use another function in void loop() or declare and define anyother function and use it in loop()

Yes, you can use other functions inside loop(). You just can't define a function inside another function.

The IDE is packed full of examples sketches.

Luckily yes (at the first), otherwise functions would be pretty useless, don't they?

The thing is that i am making collison dectecting robot (blimp) without servo mount and there are some computations in void loop() and i am using a nodemcu to control the motor in function Void Webserver() and i have to merge these codes .

Code for wifibot :slight_smile:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
const char* ssid = “ehlab”;
const char* password = “Ihavechangedpassword”;

ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
delay(10);

// prepare GPIO2
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
digitalWrite(D5, LOW);// at start turn off the GPIO
digitalWrite(D6, LOW);// at start turn off the GPIO
digitalWrite(D7, LOW);// at start turn off the GPIO
digitalWrite(D8, LOW);// at start turn off the GPIO

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
createWebServer();
// Start the server
server.begin();
Serial.println(“Server started”);

// Print the IP address
Serial.println(WiFi.localIP());
}
int gpioStatus=0;

void createWebServer()
{
server.on("/f", {
gpioStatus=0;
digitalWrite(D5, LOW);
digitalWrite(D6, HIGH);
digitalWrite(D7, LOW);
digitalWrite(D8, HIGH);
server.send(200, “text”, “Forward”);
});
server.on("/b", {
gpioStatus=1;
digitalWrite(D5, HIGH);
digitalWrite(D6, LOW);
digitalWrite(D7, HIGH);
digitalWrite(D8, LOW);
server.send(200, “text”, “BackWord”);
});
server.on("/l", {
gpioStatus=1;
digitalWrite(D5, LOW);
digitalWrite(D6, HIGH);
digitalWrite(D7, HIGH);
digitalWrite(D8, LOW);
server.send(200, “text”, “BackWord”);
});
server.on("/r", {
gpioStatus=1;
digitalWrite(D5, HIGH);
digitalWrite(D6, LOW);
digitalWrite(D7, LOW);
digitalWrite(D8, HIGH);
server.send(200, “text”, “BackWord”);
});
server.on("/s", {
gpioStatus=1;
digitalWrite(D5, LOW);
digitalWrite(D6, LOW);
digitalWrite(D7, LOW);
digitalWrite(D8, LOW);
server.send(200, “text”, “BackWord”);
});

}

void loop() {
// Check if a client has connected
server.handleClient();
}

Code for Ultrasonic :frowning: d loop() {[/sup]

** long duration, distance;**
** digitalWrite(TRIGGER, LOW); **
** delayMicroseconds(2);**

** digitalWrite(TRIGGER, HIGH);**
** delayMicroseconds(10);**

** digitalWrite(TRIGGER, LOW);**
** duration = pulseIn(ECHO, HIGH);**
** distance = (duration/2) / 29.1;**
** if (distance >50) {**
//Move forward

}
** else {**
** //Move backward**
** }**
if (distance <50) {

}
** else {**

** }**





** delay(200);**

}
And i want to merge these codes and if i want to call the Webserver functions like f,b,l,r do i have to call it with the if in the loop() .And i need Distance to be checked before moving! If i get the distance from loop() can i use it on WebServer() ??? Or what can i do to get the distance value in realtime and call the functions :confused: :confused: :confused: :confused: :o :o :slight_smile:

OK, lambda (anonymous) functions are an exception.

Please remember to use code tags when posting code

OK, lambda (anonymous) functions are an exception.

In that they can be defined inside another function, not in that they can't call other functions.

Your other "function" isn't a function. Make it one.