Files

copied

There are no circuits or boards in this repository.

Last update 6 years 4 months by Dmitro Guyvan
Files
README
accelToServo.fzz
accelToServo.ino
accelToServo.ino
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_LSM303.h> #include <Servo.h> Servo ForeAft; // Define a Servo for Forward and Reverse Motion Servo LeftRight; // Define a Servo for Left and Right Motion Adafruit_LSM303 lsm; int accelMin = 16350; int accelMax = -16350; int servoMin = 0; int servoMax = 180; int ForeAft_Pin = 0; // Plug Joystick Fore/Aft into Analog pin 0 int LeftRight_Pin = 1; int def_y; int def_x; void setup() { #ifndef ESP8266 while (!Serial); // will pause Zero, Leonardo, etc until serial console opens #endif Serial.begin(9600); // Try to initialise and warn if we couldn't detect the chip if (!lsm.begin()) { Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); while (1); } ForeAft.attach(6); // Plug a servo signal line into digital output pin 6 LeftRight.attach(7); // Plug a servo signal line into digital output pin 7 // def_y = analogRead(ForeAft_Pin) ; // def_x = analogRead(LeftRight_Pin) ; } void loop() { lsm.read(); Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); int ForeAft_Output = convertAccelToServo(lsm.accelData.x); int LeftRight_Output = convertAccelToServo(lsm.accelData.y); ForeAft.write(ForeAft_Output); // Command the Fore/Aft servo to a position LeftRight.write(LeftRight_Output); // Command the Left/Right servo to a position delay(100); } int convertAccelToServo(int y) { int result; result = map(y, accelMin, accelMax, servoMin, servoMax); return result; }
Report a bug