const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
/**
Connection
Arduino VoiceRecognitionModule
2 -------> TX
3 -------> RX
*/
VR myVR(2, 3); // 2:RX 3:TX, you can choose your favourite pins.
uint8_t records[7]; // save record
uint8_t buf[64];
int led = 13;
int mrw1 = 6;
int mrw2 = 7;
int mlw1 = 5;
int mlw2 = 4;
#define Forward (0)
#define Stop (1)
#define left (2)
#define right (3)
#define back (4)
/**
@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);
Serial.begin(115200);
Serial.println("Elechouse Voice Recognition V3 Module\r\nControl Wheel chair sample");
pinMode(led, OUTPUT);
pinMode(mrw1, OUTPUT);
pinMode(mrw2, OUTPUT);
pinMode(mlw1, OUTPUT);
pinMode(mlw2, OUTPUT);
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
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)Forward) >= 0) {
Serial.println("Forward");
}
if (myVR.load((uint8_t)Stop) >= 0) {
Serial.println("STOP");
}
if (myVR.load((uint8_t)left) >= 0) {
Serial.println("LEFT");
}
if (myVR.load((uint8_t)right) >= 0) {
Serial.println("RIGHT");
}
if (myVR.load((uint8_t)back) >= 0) {
Serial.println("BACK");
}
}
void loop()
{
int ret;
ret = myVR.recognize(buf, 50);
if (ret > 0) {
switch (buf[1]) {
case Forward:
digitalWrite(mrw1, HIGH);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, HIGH);
digitalWrite(mlw2, LOW);
delay(1000);
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
buf[1] = 10;
break;
case left:
digitalWrite(mrw1, HIGH);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
delay(1000);
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
buf[1] = 10;
break;
case back:
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, HIGH);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, HIGH);
delay(1000);
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
buf[1] = 10;
break;
case right:
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, HIGH);
digitalWrite(mlw2, LOW);
delay(1000);
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
buf[1] = 10;
break;
case Stop:
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
break;
default:
Serial.println("undefined");
digitalWrite(mrw1, LOW);
digitalWrite(mrw2, LOW);
digitalWrite(mlw1, LOW);
digitalWrite(mlw2, LOW);
break;
}
/** voice recognized */
printVR(buf);
}
}
digitalWrite(ledPin, HIGH);
}
else {
// Motor A
int in1 = 6;
int in2 = 7;
// Motor B
int in3 = 4;
int in4 = 5;
// Joystick Input
int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal
// Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512;
void setup()
{
// Set all the motor control pins to outputs
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Start with motors disabled and direction forward
// Motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void loop() {
// Read the Joystick X and Y positions
joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);
// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction
if (joyposVert < 460)
{
// This is Backward
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
else if (joyposVert > 564)
{
// This is Forward
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//
}
else if (joyposHorz < 460)
{
// This is left
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B left
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
else if (joyposHorz > 564)
{
// Set Motor A right
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// Set Motor B right
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
digitalWrite(ledPin, LOW);
}
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags
Where is the closing } of the loop() function ?
in the end
Auto Format your code in the IDE and tell me again where the end of the loop() function is
And how many loop() functions can you have in a sketch ?
We can't see it. Post you code in code tags.
If you post your code as described in the how to use this forum sticky, more forum members will read it.
now it is done please find where i am going wrong?
Apart from anything else you have 2 loop() functions and 2 setup() functions. Is that deliberate ?
yes
need to add two different programs in a if else case
You can only have ONE loop() and ONE setup().
The compiler will complain if you have duplicate function names in your sketch.
OK, but you can only have 1 loop() function and 1 setup() function
In any case your program is a mess. What is the last program statement in each of your loop() functions, for instance ?
Why aren't all of the #defines at the start o the program ?
I suspect that the OP just appended two sketches into one.
OP- Is this your first Arduino project? What is the source of the code?
So do I, and even then he did it wrong
yes, it's my first Arduino project. For two sketches I got it from different sources but mostly edited to my needs.
Do either of them work on their own ?
Do you understand that you cannot have 2 functions each named loop() and setup() ?
yes individual sketches work and I got it that we cannot have two
any idea how to combine them?
I have not looked at your code in detail but try this
- put all the #defines at the start of the program and deal with an conflicts such as a name being used for 2 things
- declare all the global variables after the #defines and and deal with an conflicts such as a variable name being used for 2 things
- combine the code in the setup() functions into one and deal with an conflicts such as a pin being used for 2 things
- rename the loop() function from one program to loop1()
- rename the loop() function from the other program to loop2()
- write a new loop() function that reads an input and calls either loop1() or loop2() when required
It helps considerably if you understand how both programs work
Do you ?
yes, I understood. t=Thanks got a clear idea. I will try it.
@anon72594159, your topic has been movd to a more suitable location on the foum as it has nothing to do with bootloaders, stk500 or avrdude.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.