Go Down

Topic: SpeakJet speaking two arrays (Read 1 time) previous topic - next topic

kdecker

An interesting phenonenon...this code is supposed to check the value of the light sensor and speak a message based on the value - one of two messages. All logic works fine except for one thing. There are two arrays of values that are sent to the SpeakJet - lightOn and goodMorning.

The problem is that if the first condition is true, SpeakJet will say the goodMorning array. But if the condition is false, it will say the lightOn array and then say the goodMorning array.

And if I reverse the order of the listing of the two arrays, it will have the opposite effect. So it appears that if SpeakJet is supposed to say the 2nd array, it will. But if it's supposed to say the 1st array, it will say both of them.

I have tried a vast many things to solve this including the flush() method, etc but no effect. Any ideas?

here's my code:
----------------------
#include <NewSoftSerial.h>
// set up the SoftwareSerial port to connect to the SpeakJet
int lightPin = 0;

NewSoftSerial SpeakJetSerial =  NewSoftSerial(2, 3);

char lightOn[]= {20, 96, 21, 114, 22, 128, 23, 2, 199, 7, 145, 128, 167, 8, 191, 151, 141, 8, 169, 8, 128, 145, 155, 191, 136, 136, 142};
char goodMorning[] = {20, 96, 21, 114, 22, 128, 23, 2, 8, 179, 138, 138, 177, 140, 7, 137, 7, 153, 141, 129, 143, 30, 10, 187, 191, 8, 131, 186, 132, 132, 141, 128, 128};

void setup()
{
  SpeakJetSerial.begin(9600);
}

void loop()
{
  int val;
  int threshold = 500;
  val=analogRead(lightPin);
  if (val <= threshold) // light level goes from 0 to 900. Higher number is darker.
  {
    SpeakJetSerial.print(goodMorning); // send it to the SpeakJet
  }
  else
  {
    SpeakJetSerial.print(lightOn); // send it to the SpeakJet
  } 
  delay (10000);
}

PaulS

The SpeakJetSerial.print() function expects a string, not an array of characters.

A string is an array of characters THAT IS NULL TERMINATED.

Your array of characters are not NULL terminated, therefore they are not strings.

Add a NULL (0) to the end of each array, the function will stop at that NULL.

kdecker

Thanks! I figured this out late last night just after I posted this and it works great now. I also changed the code to check the change in light level and decide whether or not to say hello. Now it only says hello if someone comes in the room and turns on the light. Great fun. Driving my wife crazy.

PaulS

Quote
Great fun. Driving my wife crazy.

Add some additional statements that get randomly said throughout the day. Really drive her nuts.

"Bring me another beer"
"Hey, is dinner ready"

Go Up