Optimizing code


#include <ESP32Servo.h>
#include <WiFi.h>
#include <BleKeyboard.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BleKeyboard Blekeys;
Servo myservo;  // Create a servo object
int startPos = 180;  // Starting position for the servo
int endPos = 40;    // Target position for the servo movement

void setup() {
  Serial.begin(115200); // Initialize serial communication
  myservo.attach(2);  // Attach the servo to pin 2
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  Blekeys.begin();
}
void loop() {
  Blynk.run();
}
void Power() {
    for (int pos = startPos; pos >= endPos; pos -= 1) {
    myservo.write(pos);            
              
    delay(15);                       
  }
  for (int pos = endPos; pos <= startPos; pos += 1) {
    myservo.write(pos);
    delay(15);
}
}

void off(){
  Blekeys.println("hii");
}

You should probably change your Blynk auth token now that you've displayed it to the whole internet in plain text. It won't take long to get hacked.

What is the output from the compiler about memory usage?

There's not much there as far as code except for a bunch of libraries. Those are where the bulk of the memory usage is coming from. How adept are you at C++? Do you think you're up to rewriting those libraries?

Good deal. Just as a note, the old version is still viewable so you should still go to Blynk as soon as possible and change the token.

1 Like

i found that most of the bulk is coming from that ble library as when i complie it with the library it takes up 118% compared to 58% without the library

i deleted it

A library that takes 50% of esp32 memory????
My guess is that you should look for another library...

You didn't ask to rewrite, just optimize)

#include <ESP32Servo.h>
#include <WiFi.h>
#include <BleKeyboard.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define MY_SERIAL_SPD 115200
#define SERVO_WRITE_TIMEOUT 15
#define SERVO_PIN 2 //or const uint8_t servoPin = 2;
#define DEFAULT_PORT 80

BleKeyboard Blekeys;
Servo myservo;  

// The natural word size of the AVR's RAM datapaths is 8 bits, 
// so a "byte" type (8 bits) will be fetched into a CPU register more quickly than an "int" type (16 bits),
// and an "int" will require more program memory to hold the extra instructions to fetch and manipulate it.
// uint8_t variables are in 0-255 range
const uint8_t startPos = 180;  
// Also if you are not planning to change "borders" dynamically- make it constant,
// so programmer wont change stuff that isn't meant to be changed accidentally
// Also you can #denine START_POSITION 180
const uint8_t endPos = 40;    // Target position for the servo movement

void setup() {
  Serial.begin(MY_SERIAL_SPD);
  myservo.attach(SERVO_PIN);
  Blynk.begin(auth, ssid, pass, "blynk.cloud", DEFAULT_PORT);
  Blekeys.begin();
}

void loop() {// its not a mistake, but definitely not a good habit to write without spaces
  Blynk.run();
}

void Power() {
  // and try to match {} tabs
  for (uint8_t pos = startPos; pos >= endPos; pos -= 1) {
    myservo.write(pos);            
    delay(SERVO_WRITE_TIMEOUT); //DO NOT USE DELAYS. That is the first thing that i teach my students.
    // Write define or make structure for such configurations.                      
  }
  
  for (uint8_t pos = endPos; pos <= startPos; pos += 1) {
    myservo.write(pos);
    delay(SERVO_WRITE_TIMEOUT);  //Use software timers instead (google about millis() function) 
  }
}

void off(){
  Blekeys.println("hii"); // i dont know what that function does exatly ,
  //but I believe that it is a good idea to keep such strings in FLASH memory
}