#include <Servo.h>
#include <dht.h>
#include <LiquidCrystal.h>
#include <TM1637Display.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define dataPin 15
#define max_distance 200
int buzzer = 48;
#define T_C 262
#define T_D 294
#define T_E 330
#define T_F 349
#define T_G 392
#define T_A 440
#define T_B 100
#define T_H 500
int pos = 0;
int oos = 0;
const int H = 26;
const int Buzz = 33;
const int trigPin = 34;
const int echoPin = 35;
long duration;
int distance;
char sensorReading;
dht DHT;
LiquidCrystal lcd(12, 6, 22, 23, 24, 25);
const int sw = 2;
const int x = 0;
const int y = 1;
const int delay1 = 500;
const int delay2 = 300;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int val;
int val2;
#define CLK 13
#define DIN 28
#define CS 27
#define X_SEGMENTS 4
#define Y_SEGMENTS 4
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
byte fb[8 * NUM_SEGMENTS];
int ledPin = 13;
int pirPin = 14;
int pirStat = 0;
const int servo1Pin = 8;
const int servo2Pin = 9;
const int servo3Pin = 7;
const int servo4Pin = 5;
//سون سگمنت
#define CLK 50
#define DIO 51
TM1637Display display = TM1637Display(CLK, DIO);
const uint8_t allON[] = {0xff, 0xff, 0xff, 0xff};
const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};
const uint8_t done[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_C | SEG_D | SEG_E | SEG_G, // O
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G // g
};
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 27
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
tone(buzzer,T_B);
delay(833);
tone(buzzer,T_F);
delay(200);
tone(buzzer,T_G);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_B);
delay(833);
tone(buzzer,T_F);
delay(200);
tone(buzzer,T_G);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_B);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_G);
delay(833);
tone(buzzer,T_F);
delay(300);
tone(buzzer,T_G);
delay(300);
tone(buzzer,T_A);
delay(300);
tone(buzzer,T_G);
delay(300);
tone(buzzer,T_A);
delay(833);
tone(buzzer,T_B);
delay(100);
tone(buzzer,T_A);
delay(100);
tone(buzzer,T_G);
delay(100);
tone(buzzer,T_F);
delay(100);
tone(buzzer,T_E);
delay(833);
tone(buzzer,T_F);
delay(200);
tone(buzzer,T_G);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_B);
delay(833);
tone(buzzer,T_F);
delay(200);
tone(buzzer,T_G);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_B);
delay(200);
tone(buzzer,T_A);
delay(200);
tone(buzzer,T_G);
delay(833);
tone(buzzer,T_F);
delay(300);
tone(buzzer,T_G);
delay(300);
tone(buzzer,T_A);
delay(300);
tone(buzzer,T_G);
delay(300);
tone(buzzer,T_A);
delay(833);
noTone(buzzer);
pinMode(sw , INPUT_PULLUP);
pinMode(3, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
pinMode(H, INPUT_PULLUP);
pinMode(Buzz, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
servo4.attach(servo4Pin);
//دات ماتریکس
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
}
void loop() {
//جوی استیک
Serial.print("swich: ");
Serial.print(digitalRead(sw));
Serial.print("\t\t");
Serial.print("VRx: ");
Serial.print(analogRead(x));
Serial.print("\t\t");
Serial.print("VRy: ");
Serial.println(analogRead(y));
Serial.println("________________________________________________________________");
delay(50);
//راهنما چپ
if(analogRead(y)>700)
{
digitalWrite(3 , HIGH);
delay(delay1);
digitalWrite(3 , LOW);
delay(delay1);
digitalWrite(3 , HIGH);
delay(delay1);
digitalWrite(3 , LOW);
delay(delay1);
digitalWrite(3 , HIGH);
delay(delay1);
digitalWrite(3 , LOW);
delay(delay1);
}
//راهنما راست
if(analogRead(y)<200)
{
digitalWrite(4 , HIGH);
delay(delay1);
digitalWrite(4 , LOW);
delay(delay1);
digitalWrite(4 , HIGH);
delay(delay1);
digitalWrite(4 , LOW);
delay(delay1);
digitalWrite(4 , HIGH);
delay(delay1);
digitalWrite(4 , LOW);
delay(delay1);
}
//در راننده
for (pos = 0; pos <= 90; pos += 1) {
servo1.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
servo1.write(pos);
delay(15);}
//در عقب چپ
for (pos = 0; pos <= 90; pos += 1) {
servo2.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
servo2.write(pos);
delay(15);}
//در شاگرد
for (pos = 0; pos <= 90; pos += 1) {
servo3.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
servo3.write(pos);
delay(15);}
//در عقب راست
for (pos = 0; pos <= 90; pos += 1) {
servo4.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
servo4.write(pos);
delay(15);}
//سنسور dht
int readData = DHT.read22(dataPin);
float t = DHT.temperature;
float h = DHT.humidity;
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" ");
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" % ");
Serial.println("");
delay(200);
//نمایش دما و رطوبت
lcd.print("Temp = ");
lcd.print(t);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Humid = ");
lcd.print(h);
lcd.print(" ");
//بازر
while(digitalRead(H) == LOW){
tone(Buzz,T_H); }
noTone(Buzz);
//اولترا سونیک
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
myDisplay.setTextAlignment(PA_LEFT);
myDisplay.print(distance);
Serial.println(" cm");
}
delay(200);
//دنده عقب
digitalWrite(37, LOW);
if(analogRead(x)<200)
{
digitalWrite(37 , HIGH);
delay(300);
}
//سنسور rip
pirStat = digitalRead(pirPin);
if (pirStat == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("Hi, Wellcome");
lcd.setCursor(14, 0);
lcd.print("sl");
lcd.setCursor(14, 1);
lcd.print("ow");
}
else {digitalWrite(ledPin, LOW);
}
//7 segment
display.setBrightness(7);
display.clear();
display.setSegments(done);
}