Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Check the condition as well as run the function

Status
Not open for further replies.

manojkl

Newbie level 5
Joined
Aug 16, 2015
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
143
hello
I am trying to control 230V lamp brightness using a triac and zero crossing detector using voice recognition module.whenever i say something to voice module it takes that value and compare it with the stored value if it matches(using switch statement) then execute certain code,but the function i am calling in case is executing only once so my bulb will glow only once but i need to glow bulb until next command is given through voice module i.e i need to keep on checking the condition also update the values to the voice module if that condition is not met then it should come out of the loop and goto the given condition.How can i do that? i think while is not a good option any other suggestion?
 

Hi,

The problem is in line 24 of your code.

You got what I mean?

--> how can we help when you don't show us your code?

Or do expext someone writing the code for you?

Klaus
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
  ******************************************************************************
  * @file    vr_sample_control_led.ino
  * @brief   This file provides a demostration on 
              how to control led by using VoiceRecognitionModule
  ******************************************************************************
  * @note:
        voice control led
  ******************************************************************************
   */
  
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
 
/**        
  Connection
  Arduino    VoiceRecognitionModule
   2   ------->     TX
   3   ------->     RX
*/
VR myVR(4,5);    // 2:RX 3:TX, you can choose your favourite pins.
 
uint8_t records[7]; // save record
uint8_t buf[64];
 
int led1 = 8;
int led2 = 9;
int led3 = 11;
#define oneRecord    (0)
#define twoRecord   (1)
#define threeRecord  (2)
#define fourRecord  (3)
#define TRIACPULSE 13
#define INTER 2
/**
  @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()
{
  /** initialize */
  myVR.begin(9600);
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);
  pinMode(TRIACPULSE,OUTPUT);
  Serial.begin(115200);
  Serial.println(" \nControl LED sample");
  
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT); 
  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)oneRecord) >= 0){
    Serial.println("oneRecord loaded");
  }
  
  if(myVR.load((uint8_t)twoRecord) >= 0){
    Serial.println("twoRecord loaded");
  }
  
  if(myVR.load((uint8_t)threeRecord) >= 0){
    Serial.println("threeRecord loaded");
  }
  
  if(myVR.load((uint8_t)fourRecord) >= 0){
    Serial.println("fourRecord loaded");
  }
}
/*
after waiting for 9msec the optocoupler will give input to the gate of triac now it will switch on the load
*/
void triacon10percent()
{
 
  delayMicroseconds(9000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  digitalWrite(led3,LOW);
  //delay(1500);
  //printVR(buf);
 }  
 
void triacon30percent()
{
  delayMicroseconds(7000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  //delay(1500);
  //printVR(buf);   
}
void triacon60percent()
{ 
  delayMicroseconds(4000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW); 
  //delay(1500);
  //printVR(buf); 
 }  
 
void triacon80percent()
{ 
  delayMicroseconds(2000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  //delay(1500);
  //printVR(buf);
}  
void case1()
{
  if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,triacon10percent,FALLING);
 
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500);
  //printVR(buf);
   
}
 
void case2()
{
  while(buf[1]==twoRecord)
  {
  
  if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,triacon30percent,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500); 
  //printVR(buf);
  }
}
 
void case3()
{
  while(buf[1]==threeRecord)
  {
   if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,triacon60percent,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500);
  //printVR(buf);
  } 
}
 
void case4()
{
   if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,triacon80percent,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  } 
  //delay(1500);
  //printVR(buf);
  }
void loop()
{
  int ret;
  ret = myVR.recognize(buf, 50);
  if(ret>0){
    switch(buf[1]){
      case oneRecord:
          digitalWrite(led1,HIGH); 
          case1();
          break;
      case twoRecord:
          digitalWrite(led2,HIGH);
          case2();
          break;
      case threeRecord:
          digitalWrite(led3,HIGH);
          case3();
          break;  
      case fourRecord:
          case4();
          break;  
      default:
        Serial.println("Record function undefined");
        break;
    }
    /** voice recognized */
    printVR(buf);
  }
}




- - - Updated - - -

i missed a while condition in 'case1'. So any suggestion other than using while condition because if i use while condition then it is not able to update voice signal for comparison. also i need to run printVR(buf) inside while condition because it takes the input from the voice recognition module.thank u.
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top