I have 3 servos on a breadboard which is connected to a 6f22 9v battery, the servos are grounded to the GND pin near 13.
I am using an adxl345 accelerometer with scl and sda connected to a5 and a4 respectively.
the vcc is connected to 5v and GND to GND near 5v.
I have mapped the accelerometer's values to be within 0 to 180.
My issue:
initially only my x axis sensor moved in respone to moving the adxl345.
Now none of them move.
My end goal is to make a servo driven arm (built with icecream sticks) that move in response to an accelerometer.
Code:
#include <Wire.h>
#include <ADXL345.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
ADXL345 adxl;
int x,y,z;
int rawX, rawY, rawZ;
int mappedRawX, mappedRawY, mappedRawZ;
void setup(){
Serial.begin(9600);
adxl.powerOn();
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
}
void loop(){
adxl.readAccel(&x, &y, &z);
rawX = x-7;
rawY = y-6;
rawZ = z+10;
if (rawX < -255) rawX = -255; else if (rawX > 255) rawX = 255;
if (rawY < -255) rawY = -255; else if (rawY > 255) rawY = 255;
if (rawZ < -255) rawZ = 255; else if (rawZ > 255) rawZ = 255;
mappedRawX = map(rawX, -255, 255, 0, 180);
mappedRawY = map(rawY, -255, 255, 0, 180);
mappedRawZ = map(rawZ, -255, 255, 0, 180);
servo1.write(mappedRawX);
delay(20);
servo2.write(180 - mappedRawY);
delay(20);
servo3.write(mappedRawZ);
delay(20);
Serial.print("MappedRawX:");Serial.print(mappedRawX);
Serial.print(" MappedRawY:");Serial.print(mappedRawY);
Serial.print(" MappedRawZ:");Serial.print(mappedRawZ);
Serial.println();
}
Please Help!
