hi all
I’ve been trying to move 2 futaba s3000 servos according to the x-y readings from LIS3LV02DQ, i end up moving the servo at pin 9 …
the sketch i downloaded and pasted in the servo control from the knob servo library.
here is the code:
/*
LIS3LV02DQ
VDD -> 3.3V
GND -> GND
INT -> N/C
SDO -> Arduino 12
SDA -> Arduino 11
SCL -> Arduino 13
CS -> Arduino 10
*/
#include <Spi.h>
#include <Servo.h>
Servo myservox; // create servo object to control servo for x
Servo myservoy; // create servo object to control servo for y
int valx; // variable to read the value from x axis
int valy; // variable to read the value from y axis
// reads a register
char read_register(char register_name)
{
char in_byte;
// need to set bit 7 to indicate a read
register_name |= 128;
// SS is active low
digitalWrite(SS_PIN, LOW);
// send the address of the register we want to read first
Spi.transfer(register_name);
// send nothing, but here's when the device sends back the register's value as an 8 bit byte
in_byte = Spi.transfer(0);
// deselect the device
digitalWrite(SS_PIN, HIGH);
return in_byte;
}
// write to a register
void write_register(char register_name, byte data)
{
// char in_byte;
// clear bit 7 to indicate we're doing a write
register_name &= 127;
// SS is active low
digitalWrite(SS_PIN, LOW);
// send the address of the register we want to write
Spi.transfer(register_name);
// send the data we're writing
Spi.transfer(data);
digitalWrite(SS_PIN, HIGH);
//return in_byte;
}
void setup()
{
myservox.attach(8); // attaches the servo on pin 8 to the servo object
myservoy.attach(9); // attaches the servo on pin 9 to the servo object
char in_byte = 0;
Serial.begin(9600);
digitalWrite(SS_PIN,HIGH); //disable device
// start up the device
// this essentially activates the device, powers it on, enables all axes, and turn off the self test
// CTRL_REG1 set to 10000111
write_register(0x20, 135);
// query the WHO_AM_I register of the LIS3LV02DQ
// this should return 0x3A, a factory setting
in_byte = read_register(0x0F);
Serial.print("LIS3LV02DQ: WHO_AM_I [");
Serial.print(in_byte, HEX);
Serial.println("]");
Serial.println("----");
delay(250);
}
void loop()
{
int x_val, y_val, z_val;
byte x_val_l, x_val_h, y_val_l, y_val_h, z_val_l, z_val_h;
//X
x_val_h = read_register(0x29); //read the outx_h register
x_val_l = read_register(0x28); // read the outx_l register
x_val = x_val_h;
x_val <<= 8; //shift high byte to be high 8 bits
x_val += x_val_l;
Serial.print(" x_val="); Serial.print(x_val, DEC);
valx = x_val; // reads the value of x (value between-1075 and 1099)
valx = map(valx, 1099, -1075, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservox.write(valx); // sets the servo position according to the scaled value
//Y
y_val_h = read_register(0x2B); //Read outx_h
y_val_l = read_register(0x2A); //Read outx_l
y_val = y_val_h;
y_val <<= 8; //shift high byte to be high 8 bits
y_val += y_val_l;
Serial.print(", y_val="); Serial.print(y_val, DEC);
valy = y_val; // reads the value of y (value between -1335 and 1005)
valy = map(valy, 1005, -1335, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservoy.write(valy); // sets the servo position according to the scaled value
//Z
z_val_h = read_register(0x2D); //Read outz_h
z_val_l = read_register(0x2C); //Read outz_l
z_val = z_val_h;
z_val <<= 8; //shift high byte to be high 8 bits
z_val += z_val_l;
Serial.print(", z_val="); Serial.println(z_val, DEC);
// the LIS3LV02DQ according to specs, these values are:
// 2g = 2^12/2 = 2048
// 1g = 1024
// if you use the sign, that gives a range of +/-2g should output +/-2048
delay(15);
}
i am sure its a pin issue, but not sure how to solve it.
appreciate the help