Hi, I have a little/big problem
I am making a satbilizer for my model.
the stabilizer has the task of taking control under certain conditions. I therefore chose Attiny84 as the CPU controller. I have an Arduino uno as the main processor. they communicate with each other via i2c and the arduino tells when the servos are switched to control from the arduino. The signal reception from the rc receiver works well for me and direct control of the servos is not a problem. unfortunately, the BIG problem only occurs after connecting the I2C.
After connecting I2C servo begin vibrate :o
I also tested another connection of adding a capacitor between +/-.
and another pattern of communication.
in the example I provide here I do not use void receiveEvent.
the connection is according to the picture.
Unfortunately I can't upload the video
I don't know how to proceed, can someone please help me?
This is code for Attiny84
Arduino is only I2C reading and Serial(9600) showing value for pc.
I will do the rest of the code when I have solved the servos
#include <SoftwareServo.h>
#include "TinyWireS.h"
#define SLAVE_ADDR 0x10
#define relays_input 9
#define test_out 0
#define servoI1 5
unsigned long servo1_duration;
int servo1_last_angel = 90;
#define servoI2 1
unsigned long servo2_duration;
int servo2_last_angel = 90;
#define servoI3 2
unsigned long servo3_duration;
int servo3_last_angel = 90;
SoftwareServo servoO1;
SoftwareServo servoO2;
SoftwareServo servoO3;
volatile byte byteToSend[4];
unsigned long numberToSend = 0;
const int minAngel = 40;
const int maxAngel = 135;
const int initialPosition = 90;
int firstNum = 1;
int angle1 = 1;
int angle2 = 1;
int angle3 = 1;
int rele_state = 0;
bool servo_mode = false;
byte byteReceived[4];
int servo_cpu_1 = 1;
int servo_cpu_2 = 1;
int servo_cpu_3 = 1;
void setup()
{
TinyWireS.begin(SLAVE_ADDR);
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(requestEvent);
pinMode(relays_input, INPUT);
pinMode(test_out, OUTPUT);
pinMode(servoI1, INPUT);
pinMode(servoI2, INPUT);
pinMode(servoI3, INPUT);
servoO1.attach(3);
servoO2.attach(7);
servoO3.attach(8);
servoO1.write(initialPosition);
servoO2.write(initialPosition);
servoO3.write(initialPosition);
SoftwareServo::refresh();
}
void loop()
{
rele_state = digitalRead(relays_input);
if (servo_mode == false)
{
servo1_duration = pulseIn(servoI1, HIGH, 25000);
if (servo1_duration > 0){
angle1 = map(servo1_duration, 1050, 1880, minAngel, maxAngel);
angle1 = constrain(angle1, minAngel, maxAngel);
}
servo2_duration = pulseIn(servoI2, HIGH, 25000);
if (servo2_duration > 0){
angle2 = map(servo2_duration, 1050, 1880, minAngel, maxAngel);
angle2 = constrain(angle2, minAngel, maxAngel);
}
servo3_duration = pulseIn(servoI3, HIGH, 25000);
if (servo3_duration > 0){
angle3 = map(servo3_duration, 1050, 1880, minAngel, maxAngel);
angle3 = constrain(angle3, minAngel, maxAngel);
}
if (rele_state == 1){
servoO1.write(angle1);
servoO2.write(angle2);
servoO3.write(angle3);
digitalWrite(test_out, HIGH);
SoftwareServo::refresh();
} else {
digitalWrite(test_out, LOW);
}
}
else {
servoO1.write(servo_cpu_1);
servoO2.write(servo_cpu_2);
servoO3.write(servo_cpu_3);
SoftwareServo::refresh();
}
delay(5);
}
void requestEvent()
{
char buffer[11];
sprintf(buffer, "%01d%03d%03d%03d", firstNum, angle1, angle2, angle3);
numberToSend = atol(buffer);
byteToSend[0] = (numberToSend >> 24) & 0xFF;
byteToSend[1] = (numberToSend >> 16) & 0xFF;
byteToSend[2] = (numberToSend >> 8) & 0xFF;
byteToSend[3] = numberToSend & 0xFF;
for (int i = 0; i < 4; i++) {
TinyWireS.write(byteToSend[i]);
}
}
void receiveEvent(int howMany) {
if (howMany >= 4) {
for (int i = 0; i < 4; i++) {
byteReceived[i] = TinyWireS.read();
}
unsigned long numberReceived = 0;
numberReceived |= ((unsigned long)byteReceived[0] << 24);
numberReceived |= ((unsigned long)byteReceived[1] << 16);
numberReceived |= ((unsigned long)byteReceived[2] << 8);
numberReceived |= byteReceived[3];
int servoMode = numberReceived / 1000000000;
servo_cpu_1 = (numberReceived % 1000000000) / 1000000;
servo_cpu_2 = (numberReceived % 1000000) / 1000;
servo_cpu_3 = numberReceived % 1000;
if (servoMode == 2) {
servo_mode = true;
} else {
servo_mode = false;
}
}
}