I'm trying to get my ICM20948 to record readings and data into a log file. I can't find anything on the web on what code I need to write into my main in order to achieve this. Here is my code:
#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
#include <Adafruit_MCP23XXX.h>
#include <Adafruit_RGBLCDShield.h>
#include <Adafruit_ICM20948.h>
#include <Adafruit_ICM20X.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>
#include <Adafruit_Sensor.h>
#include <english.h>
#include <sound.h>
#include <TTS.h>
#include <Wire.h>
#include <SPI.h>
#include <utility/Adafruit_MCP23017.h>
// i2c, initialize the sensor breakout board
Adafruit_ICM20948 icm = Adafruit_ICM20948();
Adafruit_Sensor *icm_temp, *icm_accel, *icm_gyro, *icm_mag;
uint16_t measurement_delay_us = 65535; // Delay between measurements for testing
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define ICM_CS 10
#define ICM_SCK 25
#define ICM_SDA 21
#define ICM_SCL 22
#define ICM_MISO 23
#define ICM_MOSI 24
#define ICM20948_I2CADDR_DEFAULT 0x69 ///< ICM20948 default i2c address
#define ICM20948_MAG_ID 0x09 ///< The chip ID for the magnetometer
#define Adafruit_RGBLCDShield 0x20
void setupSensor()
{
// Set the magnetometer sensitivity
//icm.setupMag(icm.ICM20948_MAGGAIN_4GAUSS);
icm20948_accel_range_t getAccelRange(void);
void setAccelRange(icm20948_accel_range_t new_accel_range);
icm20948_gyro_range_t getGyroRange(void);
void setGyroRange(icm20948_gyro_range_t new_gyro_range);
ak09916_data_rate_t getMagDataRate(void);
bool setMagDataRate(ak09916_data_rate_t rate);
uint8_t readMagRegister(uint8_t reg_addr);
bool writeMagRegister(uint8_t reg_addr, uint8_t value);
//uint8_t getMagId(void);
//bool auxI2CBusSetupFailed(void);
bool setupMag(Adafruit_ICM20948);
//void scaleValues(void);
}
// Dictionary of the 1000 most common English words.
char *words[] = {"the", "of", "to", "and", "a", "in", "is", "it", "you", "that", "he", "was", "for", "on", "are", "with", "as", "I", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "hot", "word", "but", "what", "some", "we", "can", "out", "other", "were", "all", "there", "when", "up", "use", "your", "how", "said", "an", "each", "she", "which", "do", "their", "time", "if", "will", "way", "about", "many", "then", "them", "write", "would", "like", "so", "these", "her", "long", "make", "thing", "see", "him", "two", "has", "look", "more", "day", "could", "go", "come", "did", "number", "sound", "no", "most", "people", "my", "over", "know", "water", "than", "call", "first", "who", "may", "down", "side", "been", "now", "find", "any", "new", "work", "part", "take", "get", "place", "made", "live", "where", "after", "back", "little", "only", "round", "man", "year", "came", "show", "every", "good", "me", "give", "our", "under", "name", "very", "through", "just", "form", "sentence", "great", "think", "say", "help", "low", "line", "differ", "turn", "cause", "much", "mean", "before", "move", "right", "boy", "old", "too", "same", "tell", "does", "set", "three", "want", "air", "well", "also", "play", "small", "end", "put", "home", "read", "hand", "port", "large", "spell", "add", "even", "land", "here", "must", "big", "high", "such", "follow", "act", "why", "ask", "men", "change", "went", "light", "kind", "off", "need", "house", "picture", "try", "us", "again", "animal", "point", "mother", "world", "near", "build", "self", "earth", "father", "head", "stand", "own", "page", "should", "country", "found", "answer", "school", "grow", "study", "still", "learn", "plant", "cover", "food", "sun", "four", "between", "state", "keep", "eye", "never", "last", "let", "thought", "city", "tree", "cross", "farm", "hard", "start", "might", "story", "saw", "far", "sea", "draw", "left", "late", "run", "don't", "while", "press", "close", "night", "real", "life", "few", "north", "open", "seem", "together", "next", "white", "children", "begin", "got", "walk", "example", "ease", "paper", "group", "always", "music", "those", "both", "mark", "often", "letter", "until", "mile", "river", "car", "feet", "care", "second", "book", "carry", "took", "science", "eat", "room", "friend", "began", "idea", "fish", "mountain", "stop", "once", "base", "hear", "horse", "cut", "sure", "watch", "color", "face", "wood", "main", "enough", "plain", "girl", "usual", "young", "ready", "above", "ever", "red", "list", "though", "feel", "talk", "bird", "soon", "body", "dog", "family", "direct", "pose", "leave", "song", "measure", "door", "product", "black", "short", "numeral", "class", "wind", "question", "happen", "complete", "ship", "area", "half", "rock", "order", "fire", "south", "problem", "piece", "told", "knew", "pass", "since", "top", "whole", "king", "space", "heard", "best", "hour", "better", "true", "during", "hundred", "five", "remember", "step", "early", "hold", "west", "ground", "interest", "reach", "fast", "verb", "sing", "listen", "six", "table", "travel", "less", "morning", "ten", "simple", "several", "vowel", "toward", "war", "lay", "against", "pattern", "slow", "center", "love", "person", "money", "serve", "appear", "road", "map", "rain", "rule", "govern", "pull", "cold", "notice", "voice", "unit", "power", "town", "fine", "certain", "fly", "fall", "lead", "cry", "dark", "machine", "note", "wait", "plan", "figure", "star", "box", "noun", "field", "rest", "correct", "able", "pound", "done", "beauty", "drive", "stood", "contain", "front", "teach", "week", "final", "gave", "green", "oh", "quick", "develop", "ocean", "warm", "free", "minute", "strong", "special", "mind", "behind", "clear", "tail", "produce", "fact", "street", "inch", "multiply", "nothing", "course", "stay", "wheel", "full", "force", "blue", "object", "decide", "surface", "deep", "moon", "island", "foot", "system", "busy", "test", "record", "boat", "common", "gold", "possible", "plane", "stead", "dry", "wonder", "laugh", "thousand", "ago", "ran", "check", "game", "shape", "equate", "hot", "miss", "brought", "heat", "snow", "tire", "bring", "yes", "distant", "fill", "east", "paint", "language", "among", "grand", "ball", "yet", "wave", "drop", "heart", "am", "present", "heavy", "dance", "engine", "position", "arm", "wide", "sail", "material", "size", "vary", "settle", "speak", "weight", "general", "ice", "matter", "circle", "pair", "include", "divide", "syllable", "felt", "perhaps", "pick", "sudden", "count", "square", "reason", "length", "represent", "art", "subject", "region", "energy", "hunt", "probable", "bed", "brother", "egg", "ride", "cell", "believe", "fraction", "forest", "sit", "race", "window", "store", "summer", "train", "sleep", "prove", "lone", "leg", "exercise", "wall", "catch", "mount", "wish", "sky", "board", "joy", "winter", "sat", "written", "wild", "instrument", "kept", "glass", "grass", "cow", "job", "edge", "sign", "visit", "past", "soft", "fun", "bright", "gas", "weather", "month", "million", "bear", "finish", "happy", "hope", "flower", "clothe", "strange", "gone", "jump", "baby", "eight", "village", "meet", "root", "buy", "raise", "solve", "metal", "whether", "push", "seven", "paragraph", "third", "shall", "held", "hair", "describe", "cook", "floor", "either", "result", "burn", "hill", "safe", "cat", "century", "consider", "type", "law", "bit", "coast", "copy", "phrase", "silent", "tall", "sand", "soil", "roll", "temperature", "finger", "industry", "value", "fight", "lie", "beat", "excite", "natural", "view", "sense", "ear", "else", "quite", "broke", "case", "middle", "kill", "son", "lake", "moment", "scale", "loud", "spring", "observe", "child", "straight", "consonant", "nation", "dictionary", "milk", "speed", "method", "organ", "pay", "age", "section", "dress", "cloud", "surprise", "quiet", "stone", "tiny", "climb", "cool", "design", "poor", "lot", "experiment", "bottom", "key", "iron", "single", "stick", "flat", "twenty", "skin", "smile", "crease", "hole", "trade", "melody", "trip", "office", "receive", "row", "mouth", "exact", "symbol", "die", "least", "trouble", "shout", "except", "wrote", "seed", "tone", "join", "suggest", "clean", "break", "lady", "yard", "rise", "bad", "blow", "oil", "blood", "touch", "grew", "cent", "mix", "team", "wire", "cost", "lost", "brown", "wear", "garden", "equal", "sent", "choose", "fell", "fit", "flow", "fair", "bank", "collect", "save", "control", "decimal", "gentle", "woman", "captain", "practice", "separate", "difficult", "doctor", "please", "protect", "noon", "whose", "locate", "ring", "character", "insect", "caught", "period", "indicate", "radio", "spoke", "atom", "human", "history", "effect", "electric", "expect", "crop", "modern", "element", "hit", "student", "corner", "party", "supply", "bone", "rail", "imagine", "provide", "agree", "thus", "capital", "won't", "chair", "danger", "fruit", "rich", "thick", "soldier", "process", "operate", "guess", "necessary", "sharp", "wing", "create", "neighbor", "wash", "bat", "rather", "crowd", "corn", "compare", "poem", "string", "bell", "depend", "meat", "rub", "tube", "famous", "dollar", "stream", "fear", "sight", "thin", "triangle", "planet", "hurry", "chief", "colony", "clock", "mine", "tie", "enter", "major", "fresh", "search", "send", "yellow", "gun", "allow", "print", "dead", "spot", "desert", "suit", "current", "lift", "rose", "continue", "block", "chart", "hat", "sell", "success", "company", "subtract", "event", "particular", "deal", "swim", "term", "opposite", "wife", "shoe", "shoulder", "spread", "arrange", "camp", "invent", "cotton", "born", "determine", "quart", "nine", "truck", "noise", "level", "chance", "gather", "shop", "stretch", "throw", "shine", "property", "column", "molecule", "select", "wrong", "gray", "repeat", "require", "broad", "prepare", "salt", "nose", "plural", "anger", "claim", "continent", "oxygen", "sugar", "death", "pretty", "skill", "women", "season", "solution", "magnet", "silver", "thank", "branch", "match", "suffix", "especially", "fig", "afraid", "huge", "sister", "steel", "discuss", "forward", "similar", "guide", "experience", "score", "apple", "bought", "led", "pitch", "coat", "mass", "card", "band", "rope", "slip", "win", "dream", "evening", "condition", "feed", "tool", "total", "basic", "smell", "valley", "nor", "double", "seat", "arrive", "master", "track", "parent", "shore", "division", "sheet", "substance", "favor", "connect", "post", "spend", "chord", "fat", "glad", "original", "share", "station", "dad", "bread", "charge", "proper", "bar", "offer", "segment", "slave", "duck", "instant", "market", "degree", "populate", "chick", "dear", "enemy", "reply", "drink", "occur", "support", "speech", "nature", "range", "steam", "motion", "path", "liquid", "log", "meant", "quotient", "teeth", "shell", "neck"};
// Media pins
#define PWM A0
#define buttonPin 13
#define LED 12
#define abs(x) ((x) > 0 ? (x) : -(x))
#define sq(x) ((x) * (x))
//Talkie voice (PWM);
TTS text2speech(PWM); // default is digital pin 10
int buttonState = 0;
float lastMag = 0;
float lastTemp = 0;
void setup() {
pinMode(buttonPin, INPUT);
Wire.begin(0x69, 0x20);
lcd.begin(16, 2);
Serial.begin(115200);
// Try to initialise and warn if we couldn't detect the chip
bool begin_I2C(uint8_t i2c_addr = ICM20948_I2CADDR_DEFAULT,
TwoWire *wire = &Wire, int32_t sensor_id = 0);
if (!icm.begin_I2C()) {
//if (!icm.begin_SPI(ICM_CS)) {
//if (!icm.begin_SPI(ICM_CS, ICM_SCK, ICM_MISO, ICM_MOSI)) {
Serial.println("Could not discover the chip Failure");
while (1) {
setupSensor();
};
}
Serial.println("Welcome to Ovilus ITC Technologies");
icm_temp = icm.getTemperatureSensor();
icm_temp->printSensorDetails();
icm_accel = icm.getAccelerometerSensor();
icm_accel->printSensorDetails();
icm_gyro = icm.getGyroSensor();
icm_gyro->printSensorDetails();
icm_mag = icm.getMagnetometerSensor();
icm_mag->printSensorDetails();
Serial.print("Accelerometer range set to: ");
switch (icm.getAccelRange()) {
case ICM20948_ACCEL_RANGE_2_G:
Serial.println("+-2G");
break;
case ICM20948_ACCEL_RANGE_4_G:
Serial.println("+-4G");
break;
case ICM20948_ACCEL_RANGE_8_G:
Serial.println("+-8G");
break;
case ICM20948_ACCEL_RANGE_16_G:
Serial.println("+-16G");
break;
}
Serial.println("OK");
// icm.setGyroRange(ICM20948_GYRO_RANGE_2000_DPS);
Serial.print("Gyro range set to: ");
switch (icm.getGyroRange()) {
case ICM20948_GYRO_RANGE_250_DPS:
Serial.println("250 degrees/s");
break;
case ICM20948_GYRO_RANGE_500_DPS:
Serial.println("500 degrees/s");
break;
case ICM20948_GYRO_RANGE_1000_DPS:
Serial.println("1000 degrees/s");
break;
case ICM20948_GYRO_RANGE_2000_DPS:
Serial.println("2000 degrees/s");
break;
}
// icm.setAccelRateDivisor(4095);
uint16_t accel_divisor = icm.getAccelRateDivisor();
float accel_rate = 1125 / (1.0 + accel_divisor);
Serial.print("Accelerometer data rate divisor set to: ");
Serial.println(accel_divisor);
Serial.print("Accelerometer data rate (Hz) is approximately: ");
Serial.println(accel_rate);
// icm.setGyroRateDivisor(255);
uint8_t gyro_divisor = icm.getGyroRateDivisor();
float gyro_rate = 1100 / (1.0 + gyro_divisor);
Serial.print("Gyro data rate divisor set to: ");
Serial.println(gyro_divisor);
Serial.print("Gyro data rate (Hz) is approximately: ");
Serial.println(gyro_rate);
// icm.setMagDataRate(AK09916_MAG_DATARATE_10_HZ);
Serial.print("Magnetometer data rate set to: ");
switch (icm.getMagDataRate()) {
case AK09916_MAG_DATARATE_SHUTDOWN:
Serial.println("Shutdown");
break;
case AK09916_MAG_DATARATE_SINGLE:
Serial.println("Single/One shot");
break;
case AK09916_MAG_DATARATE_10_HZ:
Serial.println("10 Hz");
break;
case AK09916_MAG_DATARATE_20_HZ:
Serial.println("20 Hz");
break;
case AK09916_MAG_DATARATE_50_HZ:
Serial.println("50 Hz");
break;
case AK09916_MAG_DATARATE_100_HZ:
Serial.println("100 Hz");
break;
}
Serial.println();
text2speech.setPitch(4);
text2speech.sayText("Welcome to Ovilus I Tea Sea Technologies!");
//voice.say("Welcome to Ovilus ITC Technologies!");
}
void loop() {
buttonState = digitalRead(buttonPin);
// Check if the switch has been thrown. If so, start measuring and speaking.
if (buttonState == HIGH) {
lcd.setCursor(0, 1);
bool icm_read(); /* ask it to read in the data */
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t mag;
sensors_event_t temp;
icm.getEvent(&accel, &gyro, &temp, &mag);
Serial.print("\t\tTemperature ");
Serial.print(temp.temperature);
Serial.println(" deg C");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tAccel X: ");
Serial.print(accel.acceleration.x);
Serial.print(" \tY: ");
Serial.print(accel.acceleration.y);
Serial.print(" \tZ: ");
Serial.print(accel.acceleration.z);
Serial.println(" m/s^2 ");
Serial.print("\t\tMag X: ");
Serial.print(mag.magnetic.x);
Serial.print(" \tY: ");
Serial.print(mag.magnetic.y);
Serial.print(" \tZ: ");
Serial.print(mag.magnetic.z);
Serial.println(" uT");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tGyro X: ");
Serial.print(gyro.gyro.x);
Serial.print(" \tY: ");
Serial.print(gyro.gyro.y);
Serial.print(" \tZ: ");
Serial.print(gyro.gyro.z);
Serial.println(" radians/s ");
Serial.println();
delay(100);
// Get the latest magnetometer and temperature reading
float temperature;
float accX;
float accY;
float accZ;
float gyroX;
float gyroY;
float gyroZ;
float magX;
float magY;
float magZ;
float newMag = sqrt(sq(magX) + sq(magY) + sq(magZ));
float newTemp = temp.temperature;
// If this isn't our first reading...
if (lastMag != 0) {
// Calculate the change in magnetic field/temperature
float deltaMag = abs(newMag - lastMag);
float deltaTemp = abs(newTemp - lastTemp);
// Get the biggest change of the two
float index = max(deltaMag, deltaTemp);
// Convert to an index of our dictionary array
float wordindfloat = index * 1000.0;
int wordindint = round(wordindfloat);
// Speak the change on teh speaker
text2speech.setPitch(6);
text2speech.sayText(words[wordindint]);
//voice.say(words[wordindint]);
lcd.print(words[wordindint]);
Serial.println(words[wordindint]);
// Store the readings for the next loop
lastMag = newMag;
lastTemp = newTemp;
}
// Store our readings as a baseline
else {
lastMag = newMag;
lastTemp = newTemp;
}
delay(1000);
//Serial.println("Scanning...");
//delay(4000);
//text2speech.setPitch(1);
//text2speech.sayText("Scanning...!");
//voice.say(sp2_Scanning...);
//delay(8000);
}
}