I have code that allows for key entry and voice entry to trigger different events but when I try merging them I break everything. Is it possible to continually test for both in my loop? For example:
if(Serial.available()>0) {
state = Serial.read() && bitVoicer.getData();
}
No. That will set 'state' to 1 if both values are non-zero. Probably not what you want.
Is there some way (like with Serial.available()) to see if there is voice data available??? That would be very useful. That way you could work with whichever data arrived.
state = Serial.read() && bitVoicer.getData();
That will make state true if both Serial.read() and bitVoicer.getData() don't return false/0...
So in order to make more sens, post the code where you try to merge it and tell us what goes wrong. "Does not work" isn't an answer.
void loop() {
if(Serial.available()>0) {
state = Serial.read();
}
/*
if (bitVoicer.strData == "")
{
return;
}
*/
int x = 2000; //set ping distance for stopping
uint8_t i;
if (state =='1' || bitVoicer.strData == "forward"){
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.println(uS);
if (uS <= x){
Serial.println("STOP");
myMotor1->run(RELEASE);
myMotor2->run(RELEASE);
}
else {
Serial.println("FORWARD");
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
for (i=0; i<125; i++) {
myMotor1->setSpeed(i);
myMotor2->setSpeed(i);
}
}
bitVoicer.strData = "";
}
above works for key entry and
void loop() {
bitVoicer.getData();
/*
if (bitVoicer.strData == "")
{
return;
}
*/
int x = 2000; //set ping distance for stopping
uint8_t i;
if (state =='1' || bitVoicer.strData == "forward"){
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.println(uS);
if (uS <= x){
Serial.println("STOP");
myMotor1->run(RELEASE);
myMotor2->run(RELEASE);
}
else {
Serial.println("FORWARD");
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
for (i=0; i<125; i++) {
myMotor1->setSpeed(i);
myMotor2->setSpeed(i);
}
}
bitVoicer.strData = "";
}
works for voice. Trying to merge them so I can use either.
Please edit you post to use code-tags. See How to use the forum.
Second, that's in no way an attempt to merge it...
I'm very sorry. This is all new to me but I'm excited to learn more and I appreciate your patience and support. I've re-pasted my two code sample below (hopefully using proper tags). I'm trying to figure out how to combine the code so I can execute commands with either voice or number entry. Currently each example below works separately. I'm not entirely familiar with everything Arduino at my disposal so any advice is much appreciated.
void loop() {
if(Serial.available()>0) {
state = Serial.read();
}
int x = 2000; //set ping distance for stopping
uint8_t i;
if (state =='1'){
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.println(uS);
if (uS <= x){
Serial.println("STOP");
myMotor1->run(RELEASE);
myMotor2->run(RELEASE);
}
else {
Serial.println("FORWARD");
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
for (i=0; i<125; i++) {
myMotor1->setSpeed(i);
myMotor2->setSpeed(i);
}
}
}
and
void loop() {
bitVoicer.getData();
int x = 2000; //set ping distance for stopping
uint8_t i;
if (bitVoicer.strData == "forward"){
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.println(uS);
if (uS <= x){
Serial.println("STOP");
myMotor1->run(RELEASE);
myMotor2->run(RELEASE);
}
else {
Serial.println("FORWARD");
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
for (i=0; i<125; i++) {
myMotor1->setSpeed(i);
myMotor2->setSpeed(i);
}
}
bitVoicer.strData = "";
}
The BitVoicer library uses Serial so you can't use Serial Monitor and BitVoicer at the same time on an Arduino UNO. If you use an Arduino Leonardo or Arduino Micro you could use Serial1 for BitVoicer by editing the BitVoicer library. See the BitVoicer manual, page 18. If you have an Arduino MEGA you can use Serial2, Serial3 or Serial4, again by editing the library.
when combining you can try to use one in hardware serial and other in software serial