#include <LiquidCrystal.h>
#include <LedControl.h>
#include <Servo.h>
#include <IRremote.h>
/*
Arduino Pin LCD Pin
12 RS
11 E
10 D4
9 D5
8 D6
7 D7
*/
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// ---------------- MATRIX IMAGES --------------
const uint8_t moon[][8] = {
{
0b00111100,
0b01111110,
0b11110000,
0b11100000,
0b11100000,
0b11110000,
0b01111110,
0b00111100
}};
const int moon_len = sizeof(moon)/8;
const uint8_t sun[][8] = {
{
0b00111100,
0b01111110,
0b11110000,
0b11100000,
0b11100000,
0b11110000,
0b01111110,
0b00111100
}};
const int sun_len = sizeof(sun)/8;
// --------- IR Remote -----------
const int REMOTE_PIN = 51;
const long int leftCheck = 534839040;
const long int rightCheck= 1871773440;
const long int powerCheck = 1570963200;
long int userInput;
/*
Arduino Pin Matrix Pin
16 DIN
15 CS
14 CLK
*/
const int DIN = 16;
const int CS = 15;
const int CLK = 14;
const int device = 0;
LedControl matrix = LedControl(DIN, CLK, CS, 1);
// Ultra Sonic Sensor Pins
const int trigPin = 49;
const int echoPin = 47;
const int threshold = 10;
// DC motors for Wheels (controlled by ultra sonic)
const int rightWheelMotorPin = 52;
const int leftWheelMotorPin = 50;
// Headlights and Photoresistor
const int photoPin = 2;
const int leftLEDPin = 45;
const int rightLEDPin = 43;
boolean isDark = false;
// Servo Pins and setup
Servo steeringServo;
const int steeringServoPin = 3;
// Hatch and Temp Sensor
const int tempSensorPin = 13;
Servo hatchServo;
const int hatchServoPin = 4;
boolean isCold = false;
void setup() {
// Matrix startup code
// 0 refers to the first unit aka the one we are using
matrix.shutdown(device,false);
matrix.setIntensity(device,8);
matrix.clearDisplay(device);
// IR Remote startup code
IrReceiver.begin(REMOTE_PIN);
// lcd startup code
lcd.begin(16,2);
lcd.print("Smart Car");
delay(1500); //1500 ms => 1.5 second
lcd.clear();
lcd.setCursor(0,0);
// Ultra Sonic ni sozlash kodi
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// G'ildiraklar va Rulda Servo
pinMode(rightWheelMotorPin, OUTPUT);
pinMode(leftWheelMotorPin, OUTPUT);
steeringServo.attach(steeringServoPin);
// Farlar va fotorezistor
pinMode(photoPin, INPUT);
pinMode(leftLEDPin, OUTPUT);
pinMode(rightLEDPin, OUTPUT);
// Harorat sensori va Hatch Servo
pinMode(tempSensorPin, INPUT);
hatchServo.attach(hatchServoPin);
Serial.begin(9600);
}
int i = 0;
void loop(){
//Quyida faralarni boshqaradigan foto rezistorning kodi keltirilgan
int lightLevel = analogRead(photoPin);
int mappedLightLevel = map(lightLevel, 0, 1023, 0, 255);
if(mappedLightLevel < 100){
digitalWrite(leftLEDPin, HIGH);
digitalWrite(rightLEDPin, HIGH);
isDark = true;
}
else if(mappedLightLevel >= 100){
digitalWrite(leftLEDPin, LOW);
digitalWrite(rightLEDPin, LOW);
isDark = false;
}
int celsius = map( ((analogRead(tempSensorPin) - 20) * 3.04), 0, 1023, 0, 30 );
if(celsius < 15){
hatchServo.write(0);
isCold = true;
}
else if(celsius >= 15){
hatchServo.write(90);
isCold = false;
}
if(isDark && isCold){
displayImage(moon[i]);
if(++i >= moon_len){
i = 0;
}
}
else if(!isDark && !isCold){
displayImage(sun[i]);
if(++i >= sun_len){
i = 0;
}
}
send_pulse();
// Avtomobil yo'nalishi kodi (Ultrasensor va g'ildirak motorlari)
long int duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
if(distance <= threshold){
stop_car();
}
else if(distance > threshold){
start_car();
}
lcd.setCursor(0,0);
lcd.print("Distance: ");
if(distance < 10){
lcd.print(distance);
lcd.print("cm ");
}
else if(10 <= distance && distance <= 99){
lcd.print(distance);
lcd.print("cm ");
}
else{ // masofa >= 100 bo'lganda
lcd.print(distance);
lcd.print("cm");
}
// Foydalanuvchi pultdagi tugmani bosganda ishlaydigan kod (Rul boshqaruvi)
if(IrReceiver.decode() == true){
userInput = IrReceiver.decodedIRData.decodedRawData;
IrReceiver.resume();
if(userInput == rightCheck){
Serial.println("Turning right");
steeringServo.write(0);
}
if(userInput == leftCheck){
Serial.println("turning left");
steeringServo.write(180);
}
if(userInput == powerCheck){
stop_car();
}
}
}
void displayImage(const byte* image){
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
matrix.setLed(0,i,j, bitRead(image[i], 7-j));
}
}
}
void send_pulse(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
void stop_car(){
analogWrite(leftWheelMotorPin, 0);
analogWrite(rightWheelMotorPin, 0);
}
void start_car(){
analogWrite(leftWheelMotorPin, 255);
analogWrite(rightWheelMotorPin, 255);
}