1x MG995 (the one that is broken rn)
2x TD-8125MG 25KG 180°
1x an older one i got form my dad's boxes of servo's and motors that i turned into a 360° servo (i think it is quite similair to the MG995 servo.
as for the code i can show you it but i dont think it will be of any use because the 'broken' servo has been 'broken' for a while now and i was using a different code back then that i don't have anymore.
note: the 'broken servo also didn't move when i had a code dedicated to it
but here you go:
dedicated code:
#include<Servo.h>
Servo servo;
void setup() {
servo.attach(5);
servo.write(90);
}
void loop() {
servo.write(80);
delay(800);
servo.write(90);
delay(800);
servo.write(100);
delay(800);
servo.write(90);
delay(800);
}
real code: (something is still wrong with it bcs it doesn't work)
transmitter:
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#define CE_PIN 7
#define CSN_PIN 6
RF24 radio(CE_PIN, CSN_PIN);
const byte pipe[] = "00001";
const byte joystickPins[] = {A7, A2, A3, A5};
const byte joysticksCount = sizeof joystickPins / sizeof * joystickPins;
struct __attribute__ ((packed)) t_message {
int16_t rawValues[joysticksCount];
} payload, previousPayload;
void setup() {
pinMode(10, OUTPUT);
radio.begin();
Serial.begin(9600);
radio.openWritingPipe(pipe);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
// read the joysticks
for (byte i = 0; i < joysticksCount; i++) {
payload.rawValues[i] = analogRead(joystickPins[i]);
payload.rawValues[i] = analogRead(joystickPins[i]) & 0xFFFD; // two reads for stability, dropping the 2 LSb to filter out instability
}
// broadcast the data if it has changed
if (memcmp(&payload, &previousPayload, sizeof(t_message)) != 0) { // returns 0 when they match, https://cplusplus.com/reference/cstring/memcmp/
radio.write(&payload, sizeof(payload));
previousPayload = payload;
}
}
receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 49
#define CSN_PIN 48
RF24 radio(CE_PIN, CSN_PIN);
const byte pipe[] = "00001";
const byte servoPins[] = {2, 5, 4, 3}; //s1 =(2) s2 =(5) s3 =(4) s4 =(3)
const byte servosCount = sizeof servoPins / sizeof * servoPins;
const int initialPositions[servosCount] = {90, 90, 90, 84};
const int mappedRanges[servosCount][2] = {{80, 100}, {65, 115}, {65, 115}, {80, 88}};
Servo servos[servosCount];
struct __attribute__ ((packed)) t_message {
int16_t rawValues[servosCount];
} payload;
uint8_t messageBuffer[sizeof(t_message)];
void setup() {
pinMode(10, OUTPUT);
for (byte i = 0; i < servosCount; i++) {
servos[i].write(initialPositions[i]); //set starting positions
servos[i].attach(servoPins[i]);
}
// Serial
Serial.begin(9600);
//radio
radio.begin();
radio.openReadingPipe(0, pipe);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(messageBuffer, sizeof messageBuffer);
memcpy(&payload, messageBuffer, sizeof payload);
Serial.print(F("New positions: "));
for (byte i = 0; i < servosCount; i++) {
int angle = map(payload.rawValues[i], 0, 1023, mappedRanges[i][0], mappedRanges[i][1]);
Serial.print(angle); Serial.write('\t');
servos[i].write(angle); //set starting positions
}
Serial.println();
}
}
note 2: i did not write this code, someone else wrote it for me