Go Down

Topic: controlling a servo using potentiometer and I2C (Read 412 times) previous topic - next topic

mihoshii

Hi! I'm creating a program that will allow the user to control a servo using a potentiometer but the servo is connected in one arduino and the servo is connected to another arduino. I am connecting the two arduinos using EasyTransferI2C library. The program I'm trying to do is similar to the example servo and pot program in the EasyTransfer library. If you're wondering why I didn't just use 1 arduino is because I'll be using the other pins which is not enough to put all my input and output devices so I'll be needing the extra pins on the other arduino.

Here's the problem. I tried to write the code. However, the servo is still not moving no matter how many times I rotate the potentiometer. I think there's no problem with my connection since I just followed the same connection as the sample program in the EasyTransfer library, except for the TX and RX pins which I changed to A4 and A5 for the I2C. I don't know if the problem is on the delay? If it's the delay, do you have any idea how to compute the delay I must use for this? I hope someone could help me. Thanks in advance!  :)

by the way, here my code for the pot:
Code: [Select]

#include <Wire.h>
#include <EasyTransferI2C.h>

//create object
EasyTransferI2C ET;

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int servoval;
  //int pause;
};

//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

//define slave i2c address
#define I2C_SLAVE_ADDRESS 9

void setup(){
  Wire.begin();
  //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  ET.begin(details(mydata), &Wire);
 

 
}

void loop(){
  //this is how you access the variables. [name of the group].[variable name]
  mydata.servoval = analogRead(9);
  //send the data
  ET.sendData(I2C_SLAVE_ADDRESS);
 
  delay(5000);
}


and here's for the servo:
Code: [Select]

#include <Servo.h>
#include <Wire.h>
#include <EasyTransferI2C.h>

//create object
EasyTransferI2C ET;
//create servo
Servo myservo;

struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int servoval;
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

//define slave i2c address
#define I2C_SLAVE_ADDRESS 9

void setup(){
  Wire.begin(I2C_SLAVE_ADDRESS);
  //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  ET.begin(details(mydata), &Wire);
  //define handler function on receiving data
  Wire.onReceive(receive);
  myservo.attach(9);
 
}

void loop() {
  //check and see if a data packet has come in.
  ET.receiveData();
    //this is how you access the variables. [name of the group].[variable name]
      myservo.write(map(mydata.servoval, 0, 1023, 0, 179));
}

void receive(int numBytes) {}

mihoshii

It's working now. I just used almost the same code as the one used for the EasyTransfer library. I only changed the setup for the I2C.

Go Up