Hello fellow arduino users! I have a question regarding using the Sparkfun Canbus shield in conjunction with the LSM6DS33 sensor on a dual SPI configuration. I have managed to get theese two to initialize but after reviewing the data it seems that the accelerometer is sending wrong readings... I am guessing the SPI devices mess with each other. Any idea on how to fix this? I am using an UNO R3 and here is the code:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <Canbus.h>
#include <TinyGPS++.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <Keypad.h>
#include <Adafruit_LSM6DS33.h>
#define LSM_CS 9
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11
Adafruit_LSM6DS33 lsm6ds33;
TinyGPSPlus gps;
SoftwareSerial ss(3,2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad membrane = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String s;
int IATMP;
float MAF;
int VSPD;
int RPM;
float MAF2=0.0;
float DeltaMAF=0.0;
char iatbuffer[64];
char mafbuffer[64];
char vspdbuffer[64];
char rpmbuffer[64];
unsigned long currtime;
unsigned long deltatime;
const unsigned char UBLOX_INIT[] PROGMEM={
0xB5,0x62,0x06,0x08,0x06,0x00,0xC8,0x00,0x01,0x00,0x01,0x00,0xDE,0x6A
};
//********************************Setup Loop*********************************//
void setup() {
ss.begin(9600);
//Initialize Serial communication for debugging
Serial.begin(115200);
while (!Serial){
delay(10); }
for(unsigned int i=0;i<sizeof(UBLOX_INIT);i++){
ss.write(pgm_read_byte(UBLOX_INIT+i));
}
if(Canbus.init(CANSPEED_500)) /* Initialize MCP2515 CAN controller at the specified speed */
{
Serial.println(F("CAN Init Ok"));
delay(1500);
}
else
{
Serial.println(F("Can't init CAN"));
return;
}
if (!lsm6ds33.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
Serial.println(F("Failed to find LSM6DS33 chip"));
while (1) {
delay(10);
}
}
Serial.println(F("LSM6DS33 Found!"));
lsm6ds33.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
lsm6ds33.setGyroRange(LSM6DS_GYRO_RANGE_125_DPS);
lsm6ds33.setAccelDataRate(LSM6DS_RATE_12_5_HZ);
lsm6ds33.setGyroDataRate(LSM6DS_RATE_12_5_HZ);
lsm6ds33.configInt1(false, false, false);
/*int i=0;
char input = membrane.getKey();
char usrid[4]={0};
while(!input){}
if (input){
usrid[i]=input;
i++;
}
input = membrane.getKey();
while(!input){}
if (input){
usrid[i]=input;
i++;
}
input = membrane.getKey();
while(!input){}
if (input){
usrid[i]=input;
i++;
}
input = membrane.getKey();
while(!input){}
if (input){
usrid[i]=input;
i++;
}
s= String(usrid);
*/
}
//********************************Main Loop*********************************//
void loop(){
while(ss.available()>0){
gps.encode(ss.read());
if(gps.location.isUpdated()){
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);
currtime=millis();
Serial.print(s);
Serial.print(",");
Canbus.ecu_req(MAF_SENSOR,mafbuffer);
MAF2=MAF;
MAF = atof(mafbuffer);
DeltaMAF=MAF-MAF2;
// Serial.print("Engine RPM: "); //Uncomment for Serial debugging
Serial.print(mafbuffer);
Serial.print(F(","));
Serial.print(DeltaMAF);
Serial.print(F(","));
delay(10);
Canbus.ecu_req(VEHICLE_SPEED,vspdbuffer);
VSPD = atoi(vspdbuffer);
//Serial.print("Engine RPM: "); //Uncomment for Serial debugging
Serial.print(vspdbuffer);
Serial.print(F(","));
delay(10);
Canbus.ecu_req(IAT,iatbuffer);
IATMP = atoi(iatbuffer);
//Serial.print("Engine RPM: "); //Uncomment for Serial debugging
Serial.print(iatbuffer);
Serial.print(F(","));
delay(10);
Canbus.ecu_req(ENGINE_RPM,rpmbuffer);
RPM = atoi(rpmbuffer);
//Serial.print("Engine RPM: "); //Uncomment for Serial debugging
Serial.print(rpmbuffer);
Serial.print(F(","));
delay(10);
if (RPM>800){
Serial.print(F("idling"));
Serial.print(F(","));
}else{
Serial.print(F("running"));
Serial.print(F(","));
}
Serial.print(accel.acceleration.x);
Serial.print(F(","));
Serial.print(accel.acceleration.y);
Serial.print(F(","));
Serial.print(accel.acceleration.z);
Serial.print(F(","));
Serial.print(gyro.gyro.x);
Serial.print(F(","));
Serial.print(gyro.gyro.y);
Serial.print(F(","));
Serial.print(gyro.gyro.z);
Serial.print(F(","));
Serial.print(temp.temperature);
Serial.print(F(","));
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
Serial.print(F(","));
deltatime=millis()-currtime;
Serial.println(deltatime);
}
}
}