Troubleshooting

Dfr0760 Speech Synthesis code help

userHead jessiekitzmiller 2022-05-18 14:19:58 380 Views1 Replies

Hello all. It would see the company has since switched to a version 2 of the speech synthesis chip. I had a friend write this code for me for the original version 1 of the chip. Since the new version my code does not work. Could someone please help?

 

 


#include <Wire.h>
#include <SPI.h>

#include <Adafruit_ICM20X.h>
#include <Adafruit_ICM20948.h>
#include <Adafruit_Sensor.h>

#include <Adafruit_GFX.h>      // Needed by SSD1306 library
#include <Adafruit_SSD1306.h>

#include "DFRobot_SpeechSynthesis.h"

//==============================
// Pin assignments and defines
//==============================

// reserved pin for Serial1 (used by DFRobt Gravity TTS)
const int ser1TXPin = 18;
const int ser1RXPin = 19;

// reserved pins for I2C (used by sensor and display)
// The pins for I2C are defined by the Wire-library. 
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
const int i2cSDAPin = 20;
const int i2cSCLPin = 21;

//----------------------
// General defines
//----------------------

//#define MY_DEBUG_VERSION

#define SERIAL_MONITOR_BAUD_RATE 115200

#define DFROBOT_SERIAL_BAUD_RATE 115200

#define DELAY_BETWEEN_WORDS_MS 90000UL

#define SPEECH_VOLUME 10 // you can modify the volume here, range [0..10]

//---------------------
// ICM-20948 defines
//---------------------

// nothing here

//---------------------
// SSD1306 defines
//---------------------

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// reset pin for OLED display, it is -1 because the I2C module doesn't have a reset pin
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define SCREEN_ADDRESS 0x3C

#define TEXT_SIZE 2

//==========================
// Variables
//==========================

#include "words.h"

Adafruit_ICM20948 icm;
Adafruit_Sensor *icm_temp, *icm_mag;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

DFRobot_SpeechSynthesis_UART tts;

float lastMag = 0;
float lastTemp = 0;

char wordBuffer[max(maxWordLen, 80) + 1];

//===================================
// Words management
//===================================

bool getWord(const int index)
{
 if ((index < 0) || (index >= numWords))
 {
   Serial.print(F("Internal error: word index "));
   Serial.print(index);
   Serial.println(F(" out of bounds"));
   
   return false;
 }

 strcpy_P(wordBuffer, (char *)pgm_read_word(&(words[index])));  
 
 return true;
}

// display the word centered on the screen
void displayWord()
{
 int16_t x0, y0, w, h;
 
 display.clearDisplay();

 display.getTextBounds(wordBuffer,0,0,&x0,&y0,&w,&h);

 if (w < SCREEN_WIDTH)
   x0 = (((int16_t)SCREEN_WIDTH) - w) / 2;
 else
   x0 = 0;  

 if (h < SCREEN_HEIGHT)
   y0 = (((int16_t)SCREEN_HEIGHT) - h) / 2;
 else
   y0 = 0;  

 display.setCursor(x0, y0);
 
 display.print(wordBuffer);

 display.display();
}

void speakWord()
{
 tts.speak((const void *)wordBuffer); 
}

void processWord(const int index)
{
 if (!getWord(index))
 {
   // index out of bounds, do nothing
   return;
 }

 Serial.println(wordBuffer);  
 
 // display the word on OLED  
 displayWord(); 
 
 // speak the word on the speaker
 speakWord();
}

void pickAndDisplayNextWord()
{
 // read the current magnetometer and temperature sensor values
 
 sensors_event_t mag;
 sensors_event_t temp;

 icm_temp->getEvent(&temp);
 icm_mag->getEvent(&mag);

#ifdef MY_DEBUG_VERSION

 Serial.print("Temperature ");
 Serial.print(temp.temperature);
 Serial.println(" deg C");

 Serial.print("Mag 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");

#endif  
 
 float newMag = sqrt(sq(mag.magnetic.x) + sq(mag.magnetic.y) + sq(mag.magnetic.z)); // magnetic field absolute value, in uT
 
 float newTemp = temp.temperature; // value in degrees Celsius
 
 // If this is our first reading...
 if (lastMag == 0) 
 {
   // Store our readings as a baseline and return
   lastMag = newMag;
   lastTemp = newTemp;

   return;
 }
   
 // Calculate the absolute variation 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
 long wordIndex = round( index * 1050.0 );

 while( wordIndex >= numWords )
   wordIndex -= numWords;

 // Store the readings for the next loop            
 lastMag = newMag;
 lastTemp = newTemp;

 // speak and display the word
 processWord((int)wordIndex);
}

//===================================
// Arduino setup() and loop()
//===================================

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

 wordBuffer[0] = '\0';

 //
 // Display setup
 //
 
 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
 if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
 {
   Serial.println(F("SSD1306 allocation failed, program halted"));
   
   while(true)
     delay(10); // Don't proceed, loop forever
 }

 // Clear the buffer
 display.clearDisplay();  

 // Show the display buffer on the screen. You MUST call display() after
 // drawing commands to make them visible on screen!
 // display.display() is NOT necessary after every single drawing command,
 // unless that's what you want...rather, you can batch up a bunch of
 // drawing operations and then update the screen all at once by calling
 // display.display(). 
 display.display();

 display.setTextSize(TEXT_SIZE);             // Normal 1:1 pixel scale
 display.setTextColor(WHITE);        // Draw white text  

 //
 // ICM-20948 setup
 //

 // Try to initialize!
 if (!icm.begin_I2C()) 
 {
   // if (!icm.begin_SPI(ICM_CS)) {
   // if (!icm.begin_SPI(ICM_CS, ICM_SCK, ICM_MISO, ICM_MOSI)) {

   Serial.println(F("Failed to find ICM20948 chip, program halted"));
   
   while (true)
     delay(10);    
 }

 Serial.println("ICM20948 Found!");

 // accelerometer and gyro unused, but set the range anyway
 icm.setAccelRange(ICM20948_ACCEL_RANGE_16_G);
 
 icm.setGyroRange(ICM20948_GYRO_RANGE_2000_DPS);

 // the magnetometer has fixed range 4900 uT, set the number of lectures per second
 icm.setMagDataRate(AK09916_MAG_DATARATE_10_HZ);

 icm_temp = icm.getTemperatureSensor();
 icm_temp->printSensorDetails();

 icm_mag = icm.getMagnetometerSensor();
 icm_mag->printSensorDetails();
 
 //
 // Text to speech setup  
 //  

 Serial1.begin(DFROBOT_SERIAL_BAUD_RATE);
 
 //Init speech synthesis module
 tts.begin(Serial1);

 tts.setLanguage(tts.ENGLISHL);

 tts.setVolume(SPEECH_VOLUME);

 // Default initialization, done in tts.begin
 //Set voice volume to 10
 //tts.setVolume(10);
 //Set playback speed to 4
 //tts.setSpeed(4);
 //Set speaker to female 
 //tts.setSoundType(tts.FEMALE1);
 //Set tone to 5
 //tts.setTone(5);
 //For English, speak word 
 //tts.setEnglishPron(tts.WORD);

 // print some statistics

 Serial.print("Num words: ");
 Serial.println(numWords);
 Serial.print("Max word length: ");
 Serial.println(maxWordLen);
}

void loop() 
{
 pickAndDisplayNextWord();
 
 delay(DELAY_BETWEEN_WORDS_MS);  
}

2022-05-19 06:20:58

anyone?

userHeadPic jessiekitzmiller