0

I am working on a robotics project where I have a robot that includes a few different sensors and an Arduino. One of my sensors is a gyro that communicates over SPI. I have figured out how to communicate and get data from the gyro but the problem is that after I get a few bytes of data the gyro then just sends the same bytes over and over, no matter which register I read.

Data from gyro on serial console:

data from gyro on serial console

I'm not sure how to continuously get refreshed data. I am pulling the SS pin from HIGH to LOW each time before I read the data, but it doesn't seem to help.

Here is my code:

#include <SPI.h>
const int slavePin = 10;

// set up the speed, data order and data mode
SPISettings settings(3000000, MSBFIRST, SPI_MODE0); //mode_0

void setup() {
  // set the Slave Select Pins as outputs:
  SPI.beginTransaction(settings);

  pinMode (slavePin, OUTPUT);
  Serial.begin(9600);
  // initialize SPI:
  SPI.begin();
}

byte stat, val1, val2, result;
void loop() {
  digitalWrite(slavePin, LOW);//select slave
  SPI.transfer(0x08);//register to read
  stat = SPI.transfer(0);//store data
  val1 = SPI.transfer(0);
  val2 = SPI.transfer(0);

  result = val1 + val2;//combine the input data
  Serial.print(stat);
  Serial.print(":");
  Serial.print(result);

  //  Serial.print(val1); //was sending data to console seperatly
  //  Serial.print(":");
  //  Serial.print(val2);
  Serial.println();

  digitalWrite(slavePin, HIGH);//deselect slave
}

Link to data sheet

Also, as a small side question, I'm not sure in which order I should add the bytes together to get the correct value. And, I'm not sure how to handle signed bytes.

1 Answer 1

0

I found a solution on the arduino forums! http://forum.arduino.cc/index.php?topic=95901.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.