Go Down

Topic: 3 euros for 6 code lines :) (Read 126 times) previous topic - next topic

Simonas

Hello all,
I am doing room automation project with ELECHOUSE Voice Recognition Module V3.
https://www.elechouse.com/elechouse/images/product/VR3/VR3_manual.pdf

but don't afraid yet :) my question is quite simple.

Besides I have very basic knowledge of programing, I got this system to work, but there is 1 problem. First about the system: I am using V3 module to switch on and off 3 things:

1) Room main light (with servo)
2) Room night light (with reley)
3) Sound amplifier for the music (with servo)

You could wonder why I am using servos and not relays for all that stuff, its because its not my house and i do not want to disassemble main room switches etc... But that doesn't matter. Servos working well in my case, they physically switching on/off other switches.

The problem with V3 module is that if you record sound commands in quiet conditions and near the microphone. Later you have to shout in the room and recognition is quite poor. If you recording commands from about 2m. distance, later recognition is much better, but if you are watching movies o listening music (especially louder) the system goes nuts and recognizes commands then it shouldnt  that at least few times in half a hour. What I want to do is simply take 1 voice command for system to listen for lets say 10 sec for other 6 commands (on/off switching commands), and after that time system will listen again for that specific 1 voice command. As I said I have very basics of programming, so need your help guys. This is my working code so far and it needs to be a little adjusted, but I dont know how to add this functionality.

 
Code: [Select]
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
#include <Servo.h>
Servo servo1;
Servo servo2;
VR myVR(12, 13);    // 2:RX 3:TX, you can choose your favourite pins.
uint8_t records[7];
uint8_t buf[64];
int led = 13;
int in1 = 5; //rele1
int in2 = 6; //rele2
#define lightON (0)
#define lightOFF (1)
#define switchON (2)
#define switchOFF (3)
#define soundON (4)
#define soundOFF (5)
#define listeningON (6)
/**
  @brief   Print signature, if the character is invisible,
           print hexible value instead.
  @param   buf     --> command length
           len     --> number of parameters
*/
void printSignature(uint8_t *buf, int len)
{
    int i;
    for(i = 0; i < len; i++) {
        if (buf[i]>0x19 && buf[i]<0x7F) {
            Serial.write(buf[i]);
        } else {
            Serial.print("[");
            Serial.print(buf[i], HEX);
            Serial.print("]");
        }
    }
}
/**
  @brief   Print signature, if the character is invisible,
           print hexible value instead.
  @param   buf  -->  VR module return value when voice is recognized.
             buf[0]  -->  Group mode(FF: None Group, 0x8n: User, 0x0n:System
             buf[1]  -->  number of record which is recognized.
             buf[2]  -->  Recognizer index(position) value of the recognized record.
             buf[3]  -->  Signature length
             buf[4]~buf[n] --> Signature
*/
void printVR(uint8_t *buf)
{
    Serial.println("VR Index\tGroup\tRecordNum\tSignature");
    Serial.print(buf[2], DEC);
    Serial.print("\t\t");
    if (buf[0] == 0xFF) {
        Serial.print("NONE");
    } else if (buf[0]&0x80) {
        Serial.print("UG ");
        Serial.print(buf[0]&(~0x80), DEC);
    } else {
        Serial.print("SG ");
        Serial.print(buf[0], DEC);
    }
    Serial.print("\t");
    Serial.print(buf[1], DEC);
    Serial.print("\t\t");
    if (buf[3] > 0) {
        printSignature(buf+4, buf[3]);
    } else {
        Serial.print("NONE");
    }
    Serial.println("\r\n");
}
void setup()
{
    myVR.begin(9600);
    Serial.begin(115200);
    Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
    pinMode(led, OUTPUT);
    pinMode(in1, OUTPUT);
    servo1.attach(9);
    servo2.attach(8);
    servo1.write(90);
    servo2.write(90);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
    if (myVR.clear() == 0) {
        Serial.println("Recognizer cleared.");
    } else {
        Serial.println("Not find VoiceRecognitionModule.");
        Serial.println("Please check connection and restart Arduino.");
        while(1);
    }
    if (myVR.load((uint8_t)lightON) >= 0) {
        Serial.println("rele1on loaded");
    }
    if (myVR.load((uint8_t)lightOFF) >= 0) {
        Serial.println("rele1off loaded");   
    }
    if (myVR.load((uint8_t)switchON) >= 0) {
        Serial.println("sviesaON loaded");
    }
    if (myVR.load((uint8_t)switchOFF) >= 0) {
        Serial.println("sviesaOFF loaded");
    }
    if (myVR.load((uint8_t)soundON) >= 0) {
        Serial.println("stipON loaded");
    }
    if (myVR.load((uint8_t)soundOFF) >= 0) {
        Serial.println("stipOFF loaded");
    }
    if (myVR.load((uint8_t)LinsteningON) >= 0) {
        Serial.println("viskasOFF loaded");
    }
}
void loop()
{
    int ret;
    ret = myVR.recognize(buf, 50);
    if (ret > 0) {
        switch(buf[1]){                 
            case lightON:
                digitalWrite(in1, HIGH);
                break;
            case lightOFF:
                digitalWrite(in1, LOW);
                break;
            case switchON:
                digitalWrite(in2, LOW);
                delay (300);
                servo1.write(140);
                delay (500);
                servo1.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case switchOFF:
                digitalWrite(in2, LOW);
                delay (300);
                servo1.write(40);
                delay (500);
                servo1.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case soundON:
                digitalWrite(in2, LOW);
                delay (300);
                servo2.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case soundOFF:
                digitalWrite(in2, LOW);
                delay (300);
                servo2.write(30);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            default:
                Serial.println("Record function undefined");
                break;
        }
        printVR(buf);
    }
}



This code looks complicaded, but infact it isnt. What is most important here is this last part:

Code: [Select]

            case lightON:
                digitalWrite(in1, HIGH);
                break;
            case lightOFF:
                digitalWrite(in1, LOW);
                break;
            case switchON:
                digitalWrite(in2, LOW);
                delay (300);
                servo1.write(140);
                delay (500);
                servo1.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case switchOFF:
                digitalWrite(in2, LOW);
                delay (300);
                servo1.write(40);
                delay (500);
                servo1.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case soundON:
                digitalWrite(in2, LOW);
                delay (300);
                servo2.write(90);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            case soundOFF:
                digitalWrite(in2, LOW);
                delay (300);
                servo2.write(30);
                delay (500);
                digitalWrite(in2, HIGH);
                break;
            default:
                Serial.println("Record function undefined");
                break;



This is what my system does in these "cases" It listens to "cases" and act accordingly. I need to add another "listening" case (it even defined in the code) i guess with "IF" statement and some mills() functionality, but because i am shitty programmer, It will take few days at least to figure out  how to do it :) I rather give someone few euros (paypal or revolut) for this few code line if someone will help me. Even for lvl 3 beginners :) it should not be so difficult to write such code.

P.S. If you are looking into this "case" part of the code and you dont understand why there are 2 relays. One relay is turning on power supply for my servos, otherwise they always on power mode and make noise, so I am switching them on, then necessary. Other relay is controlling lamp. Servos making turns in one direction and after time going back to previous state. So that's all that is going  in "cases"

PaulS

If you know that 6 lines of code are needed, it seems to me that you have a pretty good idea what those 6 lines are. Go ahead and add them.

It would not be difficult to add a case to deal with heyStupidListenUp. In that case, you simply record what time it is (using millis()).

Then, in the other cases, you get the current time, and compare that to the heyStupidListenUp time. If the interval is less than your threshold, execute the body of the cases. If not, ignore the VR data.
The art of getting good answers lies in asking good questions.

Simonas

#2
Oct 12, 2017, 11:52 am Last Edit: Oct 12, 2017, 12:13 pm by Simonas
If you know that 6 lines of code are needed, it seems to me that you have a pretty good idea what those 6 lines are. Go ahead and add them.

It would not be difficult to add a case to deal with heyStupidListenUp. In that case, you simply record what time it is (using millis()).

Then, in the other cases, you get the current time, and compare that to the heyStupidListenUp time. If the interval is less than your threshold, execute the body of the cases. If not, ignore the VR data.
Ye it should look like something like this:

If
case listeningON
then
;
           case lightON:
                digitalWrite(in1, HIGH);
                break;
            case lightOFF:
                digitalWrite(in1, LOW);
             blabla......................
Else
            default:
                Serial.println("Record function undefined");
                break;
 
And somethere mills function. Now you see how shitty programmer i am ? :) I dont know syntax and I am not familiar with mills function. i will manage that if no one will help, but it will take more than a day for sure. First i need to get to know mills function. So thats why I am offering not a big but at least some prize for the effort.

PaulS

Quote
Ye it should look like something like this:

If case listeningON then;
           case lightON:
                digitalWrite(in1, HIGH);
                break;
You don't seem to understand that cases are mutually exclusive.

You might have a case, listeningON, that sets a flag (bool amListening; // global) to true AND records when listening started (unsigned long startedListening; // global).

Code: [Select]
     case listeningON:
        amListening = true;
        startedListening = millis();
        break;


In the other cases:
Code: [Select]
     case lightON:
        if(amListening)
        {
           if(millis() - startedListening < 30000) // If it has been less than 30 seconds since we started listening
           {
               digitalWrite(in1, HIGH);
           }
        }
        break;


You can see, though, that the amListening flag is unnecessary, though. You are listening is startedListening is not 0. You could have
Code: [Select]
     case lightON:
        if(startedListening > 0 && millis() - startedListening < 30000) // If it has been less than 30 seconds since we started listening
        {
            digitalWrite(in1, HIGH);
        }
        break;

instead.
The art of getting good answers lies in asking good questions.

Simonas

#4
Oct 12, 2017, 12:45 pm Last Edit: Oct 12, 2017, 01:04 pm by Simonas
You don't seem to understand that cases are mutually exclusive.
I am beginner lvl 2 :) I understand less than you imagine. Maybe not understanding the problem here, but lack of knowing code syntax etc.. But with your explanation we are getting somewhere i see...

Ok, before I will try to play with what you just explained, I guess I have to add these:

unsigned long startedListening;
bool amListening;

in the beggining of the code?




PaulS

Quote
in the beggining of the code?
Yes, that is what the // global comment meant.
The art of getting good answers lies in asking good questions.

Go Up