Go Down

Topic: Gyro and Servo Combo (Read 3295 times) previous topic - next topic


I need help writing the code for a gyro and servo that I want to control. I have the code for a gyro which displays the rotation rate on the Serial Monitor and for controlling the position of a servo. How to I get one to work with the other? I want to make the gyro control several servos that are placed at the corners of a cube. What would the basic structure of such a code be that would cause one to enteract with the other? So, for example the gyro would recognize where is level and compare that to its current orientation and the arduino would send a signal to the servos which are connected the the conrners of the face of the cube which is facing the ground. I want the servos to face down and remain level at a 180 degree angle. Can you fill in the gap between the code? Thanks.

Code: [Select]
#include <SoftwareServo.h>

SoftwareServo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object

void loop()
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

/* Keep track of gyro angle over time
* Connect Gyro to Analog Pin 0
* Sketch by eric barch / ericbarch.com
* v. 0.1 - simple serial output

int gyroPin = 0;               //Gyro is connected to analog pin 0
float gyroVoltage = 5;         //Gyro is running at 5V
float gyroZeroVoltage = 2.5;   //Gyro is zeroed at 2.5V
float gyroSensitivity = .007;  //Our example gyro is 7mV/deg/sec
float rotationThreshold = 1;   //Minimum deg/sec to keep track of - helps with gyro drifting

float currentAngle = 0;          //Keep track of our current angle

void setup() {
  Serial.begin (9600);

void loop() {
  //This line converts the 0-1023 signal to 0-5V
  float gyroRate = (analogRead(gyroPin) * gyroVoltage) / 1023;

  //This line finds the voltage offset from sitting still
  gyroRate -= gyroZeroVoltage;

  //This line divides the voltage we found by the gyro's sensitivity
  gyroRate /= gyroSensitivity;

  //Ignore the gyro if our angular velocity does not meet our threshold
  if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold) {
    //This line divides the value by 100 since we are running in a 10ms loop (1000ms/10ms)
    gyroRate /= 100;
    currentAngle += gyroRate;

  //Keep our angle between 0-359 degrees
  if (currentAngle < 0)
    currentAngle += 360;
  else if (currentAngle > 359)
    currentAngle -= 360;




A rate gyro can't tell you which way is up.  It can only tell you how fast you are rotating and in which direction.

If you want to detect level you might want to try an accelerometer.  If it isn't being moved it will tell you which way is DOWN.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up