laptop side Python code:

Code:

import serial
import time

# Replace 'COM3' with the appropriate port for your system
ser = serial.Serial('COM3', 9600)
time.sleep(2)  # Wait for the serial connection to initialize

def send_servo_command(servo_index, servo_position):
    command = f"{servo_index} {servo_position}\n"
    ser.write(command.encode())
    print(f"Sent command: {command.strip()}")

# Example usage
send_servo_command(0, 90)  # Move servo 0 to 90 degrees
send_servo_command(1, 45)  # Move servo 1 to 45 degrees


servo skill .h code (you don't need to understand this code just add it to the Arduino program)

Code:

#ifndef DiServos_H
#define DiServos_H

#include <Arduino.h>
#include <Servo.h>
#include "LivinGrimoireLight.h"

class DiServos : public Skill {
private:
    const int numServos = 8;
    Servo servos[numServos];
    std::vector<int> servoPins; // Use a dynamic array for servo pins

public:
    DiServos(const std::vector<int>& pins);
    virtual void inOut(byte ear, byte skin, byte eye);
};

#endif


servo skill .c++ code (you don't need to understand this code just add it to the Arduino program)

Code:

#include <Arduino.h>
#include <Servo.h>
#include "DiServos.h"
#include "LivinGrimoireLight.h"

DiServos::DiServos(const std::vector<int>& pins)
{
    Serial.begin(9600);
    servoPins = pins; // Assign the provided pin numbers dynamically

    for (int i = 0; i < servoPins.size(); i++) {
        servos[i].attach(servoPins[i]);
    }
}
void DiServos::inOut(byte ear, byte skin, byte eye)
{
   // loop logic
  static int servoIndex = -1; // Static variable to keep track of the servo index
  if (Serial.available() > 0) {
    int value = Serial.parseInt(); // Read the next integer from the serial buffer
    if (servoIndex == -1) {
      // If servoIndex is -1, this value is the servo index
      servoIndex = value;
    } else {
      // Otherwise, this value is the servo position
      int servoPosition = value;
      if (servoIndex >= 0 && servoIndex < numServos) {
        servos[servoIndex].write(servoPosition); // Move the specified servo to the position
        Serial.print("Servo ");
        Serial.print(servoIndex);
        Serial.print(" moved to ");
        Serial.println(servoPosition);
      }
      servoIndex = -1; // Reset servoIndex for the next command
    }
  }
}


also add the LivinGrimoireLight .h and .c++ files to the Arduino program:

https://github.com/yotamarker/public-livinGrimoire/tree/master/livingrimoire%20start%20here/LivinGrimoire%20Arduino%20C%2B%2B/LivinGrimoireLight200324

and finally here is an example of using DiServo skill in the main Arduino program:

Code:

#include "DiServos.h"
#include "LivinGrimoireLight.h"

Chobit* c1; // Declare c1 as a global variable
DiServos* s2; // Declare s2 as a global variable

void setup() {
    std::vector<int> myPins = {2, 3, 4, 5}; // Customize with your desired pin numbers
    s2 = new DiServos(myPins); // Create the DiServos instance
    c1 = new Chobit(); // Create the Chobit instance
    c1->addSkill(s2);
}

void loop() {
    c1->think(0, 0, 0);
}