0% found this document useful (0 votes)
2 views4 pages

Code 113950

This document contains an Arduino sketch for controlling a voice-activated wheelchair using a voice recognition module and an ultrasonic sensor for obstacle detection. It defines motor control functions, voice command IDs, and implements a state machine to manage the wheelchair's movement based on voice commands. The setup initializes the system, and the loop continuously checks for obstacles and processes voice commands to control the wheelchair's movements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Code 113950

This document contains an Arduino sketch for controlling a voice-activated wheelchair using a voice recognition module and an ultrasonic sensor for obstacle detection. It defines motor control functions, voice command IDs, and implements a state machine to manage the wheelchair's movement based on voice commands. The setup initializes the system, and the loop continuously checks for obstacles and processes voice commands to control the wheelchair's movements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <SoftwareSerial.

h>
#include "VoiceRecognitionV3.h"

// ================= VOICE MODULE =================


VR myVR(2, 3); // RX, TX
uint8_t buf[64];

// ================= MOTOR PINS (L298) =================


const int IN1 = 7;
const int IN2 = 8;
const int IN3 = 9;
const int IN4 = 10;

// ================= ULTRASONIC SENSOR =================


const int TRIG_PIN = 4;
const int ECHO_PIN = 5;
const int SAFE_DISTANCE = 150; // cm

// ================= VOICE RECORD IDs =================


#define CMD_GO 0
#define CMD_LEFT 1
#define CMD_BACK 2
#define CMD_RIGHT 3
#define CMD_STOP 4

// ================= STATE =================


enum State { STOPPED, MOVING_FORWARD, MOVING_BACKWARD, TURN_LEFT,
TURN_RIGHT };
State currentState = STOPPED;

// ================= FAST VOICE CONTROL =================


uint8_t lastCommand = 255;
unsigned long lastCmdMillis = 0;
const unsigned long MIN_GAP = 120; // ms (VERY FAST)

// ================= SETUP =================


void setup() {
[Link](115200);
[Link](9600);

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

stopMotors();

// Load ONLY required commands (BEST accuracy)


uint8_t records[] = {
CMD_GO,
CMD_LEFT,
CMD_BACK,
CMD_RIGHT,
CMD_STOP
};

for (uint8_t i = 0; i < sizeof(records); i++) {


[Link](records[i]);
}

[Link]("✅ Voice Controlled Wheelchair READY");


}

// ================= LOOP =================


void loop() {

// ===== ULTRASONIC EMERGENCY STOP =====


long distance = getDistanceCM();
if (currentState == MOVING_FORWARD && distance <= SAFE_DISTANCE) {
currentState = STOPPED;
stopMotors();
[Link]("⚠ OBSTACLE - AUTO STOP");
return;
}

// ===== FAST VOICE RECOGNITION =====


int ret = [Link](buf, 20); // very fast window
if (ret <= 0) return;

uint8_t cmd = buf[1];

// ===== STOP HAS HIGHEST PRIORITY =====


if (cmd == CMD_STOP) {
currentState = STOPPED;
stopMotors();
[Link]("STOP");
lastCommand = cmd;
lastCmdMillis = millis();
return;
}

// Prevent echo spam (still allows fast changes)


if (cmd == lastCommand && millis() - lastCmdMillis < MIN_GAP) return;

lastCommand = cmd;
lastCmdMillis = millis();

// ===== COMMAND EXECUTION =====


switch (cmd) {

case CMD_GO:
currentState = MOVING_FORWARD;
moveForward();
break;

case CMD_BACK:
currentState = MOVING_BACKWARD;
moveBackward();
break;

case CMD_LEFT:
currentState = TURN_LEFT;
turnLeft();
break;

case CMD_RIGHT:
currentState = TURN_RIGHT;
turnRight();
break;

default:
return;
}

[Link]("CMD EXECUTED: ");


[Link](cmd);
}

// ================= FUNCTIONS =================

// ---- Ultrasonic Distance ----


long getDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH, 25000);


if (duration == 0) return 999;
return duration * 0.034 / 2;
}

// ---- Motor Control ----


void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

void moveForward() {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}

void moveBackward() {
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}

void turnLeft() {
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}

void turnRight() {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}

You might also like