#include SoftwareSerial.h
#include String.h
const int AOUTpin=0;
const int DOUTpin=8;
const int ledPin=13;
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 1;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
long duration;
int distance;
long distancemm;
int limit;
int value;
SoftwareSerial GSMserial(7, 8);
int num[10];
unsigned char text[300];
char mess[300];
char choice;
String message = "";
String phone = "";
String received = "";
String str= "";
int count = 0;
int i = 0;
boolean light = true;
int relayPin = 13;
void setup()
{
Serial.begin(9600);
GSMserial.begin(19200);
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
pinMode(DOUTpin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
lcd.setCursor(0, 1);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
distancemm = distance*10+30;
if (distancemm <= 50)
{
for (int i=0; i <= 50; i++){
digitalWrite(buzzerPin, HIGH);
delay(10);
}
}
else if (distancemm >= 121)
{
digitalWrite(buzzerPin, LOW);
}
else
{
//digitalWrite(buzzerPin, HIGH);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Distance: ");
Serial.print(distancemm);
Serial.println("mm");
delay(250); }
value= analogRead(AOUTpin);
limit= digitalRead(DOUTpin);
Serial.print("Alcohol value: ");
Serial.println(value);
Serial.print("Limit: ");
Serial.print(limit);//prints the limit reached
delay(100);
if (limit == HIGH){
digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
lcd.print("alcohol detected");
}
else{
digitalWrite(ledPin, LOW);//if threshold not reached, LED remains off
lcd.print("normal");
lcd.print(millis( ) / 1000);
}
}
if(GSMserial.available()) // if date is comming from softwareserial port ==> data is comm
ing from GSMserial shield
{
while(GSMserial.available()) // reading data into char array
{
text[count]=GSMserial.read(); // writing data into array
if(text[count] == '"') i++;
if(i >= 6 && text[count] != '"')
{
if((char)text[count] == '+') light = true;
if((char)text[count] == '-') light = false;
received += (char)text[count];
}

if(count == 300)break; cou
nt++;
}
Serial.println(received);
Serial.println(light);
digitalWrite(relayPin, light);
received = "";
Serial.write(text,count); // if no data transmission ends, write text to hardware serial port cleartextArray();
// call cleartextArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero
i = 0;
}
if (Serial.available())
{
while(Serial.available()) // reading data into char array
{
text[count]=Serial.read();
if(text[count] == '~')
{
text[count]=NULL; Serial.flush(); count = 0; function();
}
count++;
if(count == 300)break;
}
GSMserial.write(text,count); // if no data transmission ends, write text to hardware serial port
cleartextArray(); // call cleartextArray function to clear the storaged data from the array count = 0; // set counter of while loop to zero
}
}
void function()
{
int bytes_read = 0;
Serial.print("Send Text(s) or Exit(x): ");
while(bytes_read < 1)
{
if(Serial.available() > 0)
{
choice = Serial.read();
}
switch(choice)
{
case 's': Serial.println(choice); bytes_read++; choice = ' '; SendTextMessage();
break;
case 'x': Serial.println("AT COMMAND MODE"); bytes_read++;
break;
bytes_read++;
}
choice = ' ';
}
}
void setNumber()
{
Serial.print("Enter Phone Number: ");
int bytes_read = 0;
while (bytes_read < 10) //amount of numbers to read - corresponds to array decleration and
print loop
{
if(Serial.available()>0)
{
num[bytes_read] = Serial.read() - 48; //its adds 48 to it? i dont know
phone += num[bytes_read];
bytes_read ++;
}
}
}
void printNumber()
{
Serial.println(phone);
}
void setMessage()
{
Serial.print("Type Message: ");
int bytes_read = 0;
boolean done = false;
while (done == false) //amount of numbers to read - corresponds to array decleration and print
loop
{
//Serial.println("message"); //debug
if (Serial.available() > 0)
{
mess[bytes_read] = Serial.read();
if(mess[bytes_read] == '~')
{
done = true;
}
if(mess[bytes_read] != '~')
{
message += mess[bytes_read];
}
bytes_read ++;
}
}
}
void printMessage()
{
for(int i = 0; i < 300; i++)
if(mess[i] != '~')
{
Serial.print(mess[i]);
}
Serial.println(" ");
}
void SendTextMessage()
{
setNumber();
printNumber();
setMessage();
printMessage();
GSMserial.println("AT+CMGF=1"); //Because we want to send the SMS in text mode
delay(100);
String str = "";
str += "AT+CMGS = \"+1";
str += phone;
str += "\"";
GSMserial.println(str);
delay(100);
GSMserial.println(message); //the content of the message
delay(150);
GSMserial.println((char)26); //the ASCII code of the ctrl+z is 26
delay(200);
Serial.println("Message Sent!");
phone = "";
message = "";
clearMessage();
Serial.flush();
GSMserial.flush();
}
void cleartextArray() // function to clear text array
{
for (int i=0; i<300;i++)
{ text[i]=NULL;} // clear all index of array with command NULL
}
void clearMessage()
{
for (int i=0; i<300;i++)
{ mess[i]=NULL;}
}
}