hanisrozali
Published

Voice-controlled Lego Car

This project is to teach high school students about programming in an interesting way possible.

BeginnerFull instructions provided1,051
Voice-controlled Lego Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Arduino Compatible Stepper Motor Controller Module
×1

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Wheel
9V Battery
DC motor
Battery snap

Story

Read more

Schematics

VoiceCar Sch

Schematic Diagram of the Voice-Controlled Lego Car

Code

voiceCar

Arduino
/* Voice Controlled Lego Car
 * Created by Hanis Rozali
 */
 
String voice;

//motor A
int ENA = 10;
int IN1 = 9;
int IN2 = 8;
// motor B
int ENB = 5;
int IN3 = 7;
int IN4 = 6;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
}

// put your main code here, to run repeatedly:
void loop() {                    
  while (Serial.available()){   //Check if there is an available byte to read
  delay(10);                        //Delay added to make thing stable
  char words = Serial.read();       //Conduct a serial read
  if (words == '#') {break;}        //Exit the loop when the # is detected after the word
  voice += words;                   //Shorthand for voice = voice + words
  
  } 

  if (voice.length() > 0){
    Serial.print(voice);
  
  if (voice == "*go" || voice == "*now") {
    forward();
  }else if(voice == "*back" || voice == "*move backward") {
    backward();
  }else if (voice == "*turn right") {
    right();
  }else if (voice == "*turn left") {
    left();
  }else if (voice == "*move faster") {
    faster();
  }else if (voice == "*start") {
    start();
  }else if (voice == "*one" || voice == "*1") {
    one();
  }else if (voice == "*three" || voice == "*3") {
    three();
  }else if (voice == "*five" || voice == "*5") {
    five();
  }else if (voice == "*zero" || voice == "*stop") {
    Stop();
  }

  voice=""; //Reset the variable after initiating
}
}
void forward(){
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        analogWrite(ENA, 180);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        analogWrite(ENB, 180);     
        delay(5000);
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);  
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        
}
    
void backward() {
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        delay(5000);
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);  
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
}

void right() {
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        delay(500);
        forward();
}

void left() {
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        delay(500);
        forward();  
}

void start(){
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        analogWrite(ENA, 50);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        analogWrite(ENB, 50);
}

void one() {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
}

void three() {
  analogWrite(ENA, 180);
  analogWrite(ENB, 180);
}
void five() {
  analogWrite(ENA, 225);
  analogWrite(ENB, 225);
}

void faster() {
  analogWrite(ENA, 210);
  analogWrite(ENB, 210);
}

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

Credits

hanisrozali
0 projects • 0 followers

Comments