Pressing reset to execute the code with electhouse simple VR

HI, I'm a first time poster, Ive been playing around with Arduino for some time now learning as i go to write code!
I have written some code to run the elechouse simple Vr board and some other button functions but my issue that I'm having is that once i power up, i have to press reset for the code to run!
i have been playing around adjust code but with no fix, any help would be appreciated as i don't want to have to press reset for the project to run!
Not in the code i have a reset command, this is not to reset the arduino it's to control a power circuit!
Here is my code Thanks Paul

#include <SoftwareSerial.h>
#include "SimpleVR.h"

// Connection
// Arduino    VoiceRecognitionModule
// 2   ------->     TX
// 3   ------->     RX

VR myVR(2, 3);    // 2:RX 3:TX, you can choose your favourite pins.
uint8_t buf[65];
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 4;
int but1 = 7;
int but2 = 8;
int but3 = 5;
int reset = 6;

int state = 0;
int old = 0;
int buttonPoll = 0;

// Variables for millis function
unsigned long lastInputTime = 0;
const unsigned long interval = 120000; // 120000 milliseconds = 2 minutes

/**
   0: wait name
   1: wait command voice
*/
uint8_t sta;

#define VOICE_PULL       (0x2)

// Flag to track button press
bool button3Pressed = false;

void setup(void) {

  myVR.begin(9600);

  Serial.begin(115200);
  Serial.println(F("Elechouse SimpleVR Module <Control LED> sample."));

  if (myVR.checkVersion(buf) > 0) {
    Serial.println(F("Found SimpleVR"));
    Serial.print(F("SW VER:"));
    Serial.print(buf[0], DEC);
    Serial.print('.'); Serial.print(buf[1], DEC);
    Serial.print('.'); Serial.println(buf[2], DEC);
    Serial.print(F("HW VER:"));
    Serial.print(buf[3], DEC);
    Serial.print('.'); Serial.println(buf[4], DEC);
  }
  else {
    Serial.println(F("Can't find SimpleVR, please restart Arduino after checking the connection."));
    while (1);
  }

  if (myVR.setGroup(1) < 0) {
    Serial.println(F("Communication error, please restart Arduino after checking the connection."));
    while (1);
  }

  if (myVR.setEnable(true) < 0) {
    Serial.println(F("Communication error, please restart Arduino after checking the connection."));
    while (1);
  }

  if (myVR.checkSystemSettings(buf) < 0) {
    Serial.println(F("Communication error, please restart Arduino after checking the connection."));
    while (1);
  }
  else {
    if (buf[0]) {
      Serial.println(F("SimpleVR is enabled."));
    }
    else {
      Serial.println(F("SimpleVR is disabled."));
    }
    Serial.print(F("Group "));
    Serial.print(buf[1], DEC); Serial.println(F(" is selected."));
    if (buf[2] != 0xFF) {
      Serial.print(F("Threshold value is ")); Serial.println(buf[2], DEC);
      Serial.println("Get Ready To Shot Clays.");
      Serial.println();
    }
  }

  // set led off
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(reset, OUTPUT);
  pinMode(but1, INPUT);
  pinMode(but2, INPUT);
  pinMode(but3, INPUT); // Added pinMode for but3

  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(reset, LOW);

  // Wait name is called
}

void loop(void) {
  // Check if but3 is pressed
  if (digitalRead(but3) == HIGH && !button3Pressed) {
    delay(60); // Debounce
    if (digitalRead(but3) == HIGH) { // Check again
      state = (state + 1) % 3; // Change state cyclically between 0, 1, and 2
      button3Pressed = true;
      delay(500); // Wait for button release
      if (state == 1) {
        digitalWrite(led5, HIGH);
      } else {
        digitalWrite(led5, LOW);
      }
    }
  } else if (digitalRead(but3) == LOW && button3Pressed) {
    button3Pressed = false; // Reset button press flag when button is released

  }

  // Delay Buttons

  if (digitalRead(but1) == HIGH) {
    delay(2000);
    digitalWrite(led3, HIGH);
    delay(100);
  }
  else {
    digitalWrite(led3, LOW);
  }

  if (digitalRead(but2) == HIGH) {
    delay(2000);
    digitalWrite(led3, HIGH);
    delay(100);
    digitalWrite(led3, LOW);
    delay(2000);
    digitalWrite(led3, HIGH);
    delay(100);
    digitalWrite(led3, LOW);
  }
  else {
    digitalWrite(led4, LOW);
  }

  // millis function to make LED high if no inputs recorded within interval
  if (millis() - lastInputTime >= interval) {
    digitalWrite(reset, HIGH); // Make LED high
  }
  else {
    digitalWrite(reset, LOW); // Make LED low
  }

  int ret = myVR.recognize(buf, 50);
  if (ret > 0) {
    uint16_t voice = buf[0];
    voice <<= 8;
    voice += buf[1];


    switch (state) {
      case 0:
        digitalWrite(led5, LOW);
        if (voice == VOICE_PULL) {
          if (sta == 0) {
            sta = 0;
            Serial.println(F("Pull is called. Clay is thrown."));
            digitalWrite(led3, HIGH);
            delay(200);
            digitalWrite(led3, LOW);
            // Reset lastInputTime when input is detected
            lastInputTime = millis();
          }
          old = 1;
        }
        break;
      case 1:
        digitalWrite(led5, HIGH);
        if (voice == VOICE_PULL) {
          if (sta == 0) {
            sta = 0;
            Serial.println(F("Pull is called. Clay is thrown."));
            digitalWrite(led3, HIGH);
            delay(200);
            digitalWrite(led3, LOW);
            delay(2000);
            digitalWrite(led3, HIGH);
            delay(200);
            digitalWrite(led3, LOW);
            // Reset lastInputTime when input is detected
            lastInputTime = millis();
          }
          old = state;
          // Do nothing here, led5 control is handled above with but3
          break;
        default:
          digitalWrite(led5, LOW);
          if (voice == VOICE_PULL) {
            if (sta == 0) {
              sta = 0;
              Serial.println(F("Pull is called. Clay is thrown."));
              digitalWrite(led3, HIGH);
              delay(200);
              digitalWrite(led3, LOW);
              // Reset lastInputTime when input is detected
              lastInputTime = millis();
            }
            old = 0;
          }
          break;
        }

    }
  }
}

Look at other programs that use the serial monitor for input. There is a test to see if the serial connection has been established before continuing with the program.

Thanks very much Paul_KD7HB, this has solved my problem!
Here's the fixed code!

#include <SoftwareSerial.h>
#include "SimpleVR.h"

// Connection
// Arduino    VoiceRecognitionModule
// 2   ------->     TX
// 3   ------->     RX

VR myVR(2, 3);    // 2:RX 3:TX, you can choose your favourite pins.
uint8_t buf[65];


int led1 = 10;
int led2 = 4;
int but1 = 7;
int but2 = 8;
int but3 = 5;
int power_off = 6;

int state = 0;
int old = 0;
bool button3Pressed = false;

unsigned long lastInputTime = 0;
const unsigned long interval = 90000;
uint8_t sta;

#define VOICE_PULL       (0x2)
void setup() {
  myVR.begin(9600);
  Serial.begin(115200);
  Serial.println(F("Elechouse SimpleVR Module <Control LED> sample."));

  // Setting up pins
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(power_off, OUTPUT);
  pinMode(but1, INPUT);
  pinMode(but2, INPUT);
  pinMode(but3, INPUT);

  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(power_off, LOW);
}

void loop() {
  // Check if but3 is pressed
  if (digitalRead(but3) == HIGH && !button3Pressed) {
    delay(60); // Debounce
    if (digitalRead(but3) == HIGH) { // Check again
      state = (state + 1) % 3; // Change state cyclically between 0, 1, and 2
      button3Pressed = true;
      delay(500); // Wait for button release
      if (state == 1) {
        digitalWrite(led2, HIGH);
      } else {
        digitalWrite(led2, LOW);
      }
    }
  } else if (digitalRead(but3) == LOW && button3Pressed) {
    button3Pressed = false; // Reset button press flag when button is released
  }
  // Update millis function
  if (millis() - lastInputTime >= interval) {
    digitalWrite(power_off, HIGH); // Make LED high
  } else {
    digitalWrite(power_off, LOW); // Make LED low
  }
  // Handle button 1 and button 2
  if (digitalRead(but1) == HIGH) {
    delay(2000);
    digitalWrite(led1, HIGH);
    delay(100);
  } else {
    digitalWrite(led1, LOW);
  }

  if (digitalRead(but2) == HIGH) {
    delay(2000);
    digitalWrite(led1, HIGH);
    delay(100);
    digitalWrite(led1, LOW);
    delay(2000);
    digitalWrite(led1, HIGH);
    delay(100);
    digitalWrite(led1, LOW);
  } else {
    digitalWrite(led1, LOW);
  }

  // Update millis function
  if (millis() - lastInputTime >= interval) {
    digitalWrite(power_off, HIGH); // Make LED high
  } else {
    digitalWrite(power_off, LOW); // Make LED low
  }

  // Recognize voice command
  int ret = myVR.recognize(buf, 50);
  if (ret > 0) {
    uint16_t voice = buf[0];
    voice <<= 8;
    voice += buf[1];

    switch (state) {
      case 0:
        if (voice == VOICE_PULL) {
          Serial.println(F("Pull is called. Clay is thrown."));
          digitalWrite(led1, HIGH);
          delay(200);
          digitalWrite(led1, LOW);
          lastInputTime = millis(); // Reset lastInputTime when input is detected
        }
        break;

      case 1:
        if (voice == VOICE_PULL) {
          Serial.println(F("Pull is called. Clay is thrown."));
          digitalWrite(led1, HIGH);
          delay(200);
          digitalWrite(led1, LOW);
          delay(2000);
          digitalWrite(led1, HIGH);
          delay(200);
          digitalWrite(led1, LOW);
          lastInputTime = millis(); // Reset lastInputTime when input is detected
        }
        break;

      default:
        if (voice == VOICE_PULL) {
          Serial.println(F("Pull is called. Clay is thrown."));
          digitalWrite(led1, HIGH);
          delay(200);
          digitalWrite(led1, LOW);
          lastInputTime = millis(); // Reset lastInputTime when input is detected
        }
        break;
    }
  }
}

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.