Skip to content

Instantly share code, notes, and snippets.

@VincentK16
Created November 30, 2013 09:16
Show Gist options
  • Save VincentK16/7716913 to your computer and use it in GitHub Desktop.
Save VincentK16/7716913 to your computer and use it in GitHub Desktop.
int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 10, 9, A0, A1, A2);
void setup() {
// sets the pins as outputs:
lcd.begin(16, 2);
pinMode(buzzer,OUTPUT);
pinMode(f_b_light,OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '0' the DC motor will turn off
if (state == '0') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("STOP");
flag=1;
}
}
// if the state is '1' the motor will forward
else if (state == '1') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("FORWARD");
flag=1;
}
}
// if the state is '2' the motor will backward
else if (state == '2') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("REVERSE");
flag=1;
}
}
else if (state == '3') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("RIGHT");
flag=1; //Left wheel forward, Right wheel static
flag=1;
}
}
else if (state == '4') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("LEFT");
flag=1;
}
}
else if (state == '5') {
digitalWrite(buzzer,HIGH);
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("Buzzer ON");
flag=1;
}
}
else if (state == '6') {
digitalWrite(f_b_light,HIGH);
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("LIGHT ON");
flag=1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment