Skip to content

Instantly share code, notes, and snippets.

@Technicus
Created July 9, 2014 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Technicus/ef105d251d457830f942 to your computer and use it in GitHub Desktop.
Save Technicus/ef105d251d457830f942 to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
int command = 0;
int parameter = 0;
int xPosition = 90;
int yPosition = 65;
Servo servoCam[2]; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
servoCam[0].attach(10); // X
servoCam[1].attach(9); // Y
Serial.println("Ready!");
}
void loop() {
delay(100);
}
void receiveData(int byteCount){
while(Wire.available()) {
command = Wire.read();
parameter = Wire.read();
if (command == 'X') {
moveX(parameter);
Serial.print("X: ");
Serial.println(parameter);
}
if (command == 'Y') {
moveY(parameter);
Serial.print("Y: ");
Serial.println(parameter);
}
}
}
void sendData(){
Wire.write(parameter);
}
void moveX(int directionX) {
if (directionX < 20 or directionX > 110) {
//return;
}
xPosition = directionX;
servoCam[0].write(xPosition);
}
void moveY(int directionY) {
if (directionY < 20 or directionY > 110) {
//return;
}
yPosition = directionY;
servoCam[1].write(yPosition);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment