//SPI Master #include <Wire.h> #include <SPI.h> #include "SPI_anything.h" #include <Adafruit_LIS3DH.h> #include <Adafruit_Sensor.h>
//#define LIS3DH_CLK 13 //#define LIS3DH_MISO 12 //#define LIS3DH_MOSI 11 //#define LIS3DH_CS 10
// I2C Adafruit_LIS3DH lisch1 = Adafruit_LIS3DH(); Adafruit_LIS3DH lisch2 = Adafruit_LIS3DH(); //Adafruit_LIS3DH lisch3 = Adafruit_LIS3DH(); struct event3;
#if defined(ARDUINO_ARCH_SAMD) // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero! #define Serial SerialUSB #endif
void setup() { #ifndef ESP8266 while (!Serial); // will pause Zero, Leonardo, etc until serial console opens #endif
Serial.begin(9600);
// have to send on master in, slave out pinMode(MISO, OUTPUT);
// turn on SPI in slave mode SPCR |= _BV(SPE);
Serial.println("LIS3DH test!");
if (! lisch1.begin(0x19)) { // change this to 0x19 for alternative i2c address - Voltage SDO
// SPI Master
#include <Wire.h>
#include <SPI.h>
#include "SPI_anything.h"
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//#define LIS3DH_CLK 13
//#define LIS3DH_MISO 12
//#define LIS3DH_MOSI 11
//#define LIS3DH_CS 10
// I2C
Adafruit_LIS3DH lisch1 = Adafruit_LIS3DH();
Adafruit_LIS3DH lisch2 = Adafruit_LIS3DH();
// Adafruit_LIS3DH lisch3 = Adafruit_LIS3DH();
struct event3;
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using
// programming port to program the Zero!
#define Serial SerialUSB
#endif
void setup() {
#ifndef ESP8266
while (!Serial)
; // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
Serial.println("LIS3DH test!");
if (!lisch1.begin(0x19)) { // change this to 0x19 for alternative i2c address
// - Voltage SDO
Serial.println("Couldnt start");
while (1)
;
} Serial.println("LIS3DH1 found!"); if (! lisch2.begin(0x18)) { // change this to 0x18 for alternative i2c address - Ground SDO Serial.println("Couldnt start"); while (1); } Serial.println("LIS3DH2 found!");
lisch1.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G! lisch2.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lisch1.getRange());
Serial.print("Range = "); Serial.print(2 << lisch2.getRange());
Serial.println("G");
}
void loop() {
lisch1.read(); // get X Y and Z data at once
/* Or....get a new sensor event, normalized */ sensors_event_t event;
lisch1.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */ Serial.print(" \t "); Serial.print(event.acceleration.x); Serial.print(" \t "); Serial.print(event.acceleration.y); Serial.print(" \t "); Serial.print(event.acceleration.z);
lisch2.read(); // get X Y and Z data at once lisch2.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */ Serial.print(" \t "); Serial.print(event.acceleration.x); Serial.print(" \t "); Serial.print(event.acceleration.y); Serial.print(" \t "); Serial.print(event.acceleration.z);
struct event;
SPI_readAnything(event); // sensors_event_t event;
// lisch3.getEvent(&event); Serial.print(" \t "); Serial.print(float2s(event.acceleration.x,6); Serial.print(" \t "); Serial.print(event.acceleration.y); Serial.print(" \t "); Serial.print(event.acceleration.z); Serial.println ();
delay(500); }
/SPI Slave
#include <Wire.h> #include <SPI.h> #include "SPI_anything.h" #include <Adafruit_LIS3DH.h> #include <Adafruit_Sensor.h>
#define LIS3DH_CLK 13 #define LIS3DH_MISO 12 #define LIS3DH_MOSI 11 #define LIS3DH_CS 10
// I2C Adafruit_LIS3DH lisch3 = Adafruit_LIS3DH();
#if defined(ARDUINO_ARCH_SAMD) // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero! #define Serial SerialUSB #endif
void setup() { #ifndef ESP8266 while (!Serial); // will pause Zero, Leonardo, etc until serial console opens #endif
Serial.begin(9600); // start serial for output
SPI.begin ();
// Slow down the master a bit SPI.setClockDivider(SPI_CLOCK_DIV8);
Serial.println("LIS3DH3 test!");
if (! lisch3.begin(0x19)) { // change this to 0x19 for alternative i2c address - Voltage SDO
}
Serial.println("LIS3DH1 found!");
if (!lisch2.begin(0x18)) { // change this to 0x18 for alternative i2c address
// - Ground SDO
Serial.println("Couldnt start");
while (1)
;
}
Serial.println("LIS3DH2 found!");
lisch1.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
lisch2.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
Serial.print("Range = ");
Serial.print(2 << lisch1.getRange());
Serial.print("Range = ");
Serial.print(2 << lisch2.getRange());
Serial.println("G");
}
void loop() {
lisch1.read(); // get X Y and Z data at once
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lisch1.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print(" \t ");
Serial.print(event.acceleration.x);
Serial.print(" \t ");
Serial.print(event.acceleration.y);
Serial.print(" \t ");
Serial.print(event.acceleration.z);
lisch2.read(); // get X Y and Z data at once
lisch2.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print(" \t ");
Serial.print(event.acceleration.x);
Serial.print(" \t ");
Serial.print(event.acceleration.y);
Serial.print(" \t ");
Serial.print(event.acceleration.z);
struct event;
SPI_readAnything(event);
// sensors_event_t event;
// lisch3.getEvent(&event);
Serial.print(" \t "); Serial.print(float2s(event.acceleration.x,6);
Serial.print(" \t "); Serial.print(event.acceleration.y);
Serial.print(" \t "); Serial.print(event.acceleration.z);
Serial.println ();
delay(500);
}
/ SPI Slave
#include <Wire.h>
#include <SPI.h>
#include "SPI_anything.h"
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
#define LIS3DH_CS 10
// I2C
Adafruit_LIS3DH lisch3 = Adafruit_LIS3DH();
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using
// programming port to program the Zero!
#define Serial SerialUSB
#endif
void setup() {
#ifndef ESP8266
while (!Serial)
; // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600); // start serial for output
SPI.begin();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
Serial.println("LIS3DH3 test!");
if (!lisch3.begin(0x19)) { // change this to 0x19 for alternative i2c address
// - Voltage SDO
Serial.println("Couldnt start");
while (1)
;
}
Serial.println("LIS3DH3 found!");
lisch3.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
Serial.print("Range = ");
Serial.print(2 << lisch3.getRange());
Serial.println("G");
}
void loop() {
lisch3.read(); // get X Y and Z data at once
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lisch3.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print(" \t ");
Serial.print(event.acceleration.x);
Serial.print(" \t ");
Serial.print(event.acceleration.y);
Serial.print(" \t ");
Serial.print(event.acceleration.z);
Serial.println();
digitalWrite(SS, LOW); // SS is pin 10
SPI_writeAnything(event);
digitalWrite(SS, HIGH);
// delay (1000); // for testing
delay(100);
}
} Serial.println("LIS3DH3 found!");
lisch3.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lisch3.getRange());
Serial.println("G"); }
void loop() {
lisch3.read(); // get X Y and Z data at once
/* Or....get a new sensor event, normalized */ sensors_event_t event;
lisch3.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */ Serial.print(" \t "); Serial.print(event.acceleration.x); Serial.print(" \t "); Serial.print(event.acceleration.y); Serial.print(" \t "); Serial.print(event.acceleration.z); Serial.println ();
digitalWrite(SS, LOW); // SS is pin 10 SPI_writeAnything (event); digitalWrite(SS, HIGH); // delay (1000); // for testing
delay(100); }