laptop side Python code:
servo skill .h code (you don't need to understand this code just add it to the Arduino program)
servo skill .c++ code (you don't need to understand this code just add it to the Arduino program)
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:
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
// using Arduino hardware codes outside main:
#include <Arduino.h>
#include <Servo.h>
#include "LivinGrimoireLight.h"
// example hello world by blinking default Led #13 once
class DiServos : public Skill {
private:
const int numServos = 8; // Number of servos
Servo servos[numServos]; // Array of servo objects
int servoPins[numServos] = {2, 3, 4, 5, 6, 7, 8, 9}; // Pins to which servos are connected
public:
DiServos();
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()
{
Serial.begin(9600); // Start serial communication at 9600 baud
for (int i = 0; i < numServos; i++) {
servos[i].attach(servoPins[i]); // Attach each servo to its pin
}
}
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;
void setup() {
Skill* s2 = new DiServos(); // servo skill created
c1 = new Chobit();
c1->addSkill(s2);
}
void loop() {
c1->think(0, 0, 0);
}