Arduino + RC Reciver + Attiny84

Hi, I have a little/big problem :confused:
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 :confused:

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; 
    }
  }
}

Welcome! You have an interesting project, but first please read the advice in the topic "How to get the best from this forum". There is a proper way to post code, this allows us to help you. Taking what you posted and putting into a readable form takes a lot of time many do not want to spend. It will only take you another 10 seconds after your figure it out. l have no clue as to what each hardware device is or how it interacts with your code, post an annotated schematic, the language of electronics, showing how you have wired it, not pictures such as frizzy (wiring) drawings they are useless for troubleshooting. Do include links to "Technical information" on the hardware parts, links to sales outlets like azon are next to useless. Doing this will have a big positive effect on your answers and will improve the accuracy of the answer a lot.

I would sure like to help you but since you did not take the time to read the forum guidelines I will not burn the time trying to figure out the gibberish you posted. All I can say is good luck!

1 Like

Welcome @adam_cz

Your topic does not indicate a problem with the IDE and hence has been moved to a more suitable location on the forum.

Thanks for trying to use code tags; it unfortunately did not quite work as it's not < followed by your code followed by /> :wink: I've fixed it.

Code tags are three backticks ( ``` ) on their own line before and after the code; the <CODE/> in the <CODE/> button symbolises that.

No powering of the UNO is shown.
Don't power the servo from the UNO.

1 Like

Hi, thanks for your reply. But after this project is done it will be battery powered. :confused: any option to separate the power supply?

Battery powering the controller demands a separate servo power supply that matches the, for now, unknown servo needs.