#include <LiquidCrystal.h>
#include <Stepper.h>
#include <Servo.h>
// LCD screen pins
const int LCD_RS = 0, LCD_E = 1, LCD_D4 = 4, LCD_D5 = 5, LCD_D6 = 6, LCD_D7 = 7;
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Photo resistor
#define LDR_PIN A1
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
// Temp sensor
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
#define PR_PIN A0
// LED pin
#define LED_PIN 9
// stepper motor
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
Stepper bigStepper(stepsPerRevolution, 35, 33, 31, 29);
// Button
#define BUTTON_PIN 19
int lastState = HIGH;
// Servo
const int SERVO_PIN = 8; // Servo control pin
Servo myservo;
// distance transducer
const int TRIG_PIN = 21; // Trigger pin for the ultrasonic sensor
const int ECHO_PIN = 20; // Echo pin for the ultrasonic sensor
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(LDR_PIN, INPUT);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT);
// set the speed at 60 rpm:
bigStepper.setSpeed(60);
// Button
pinMode(BUTTON_PIN, INPUT_PULLUP);
//Servo
myservo.attach(SERVO_PIN); // Attach servo to designated pin
}
void loop() {
// put your main code here, to run repeatedly:
//Button
int value = digitalRead((BUTTON_PIN));
if (lastState != value) {
lastState = value;
if (value == HIGH) {
Serial.println(" released");
}
if (value == LOW) {
Serial.println(" pressed");
}
}
// photo resistor
int analogValue1 = analogRead(A1);
float voltage = analogValue1 / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print(lux);
Serial.print(" lux. ");
// Temp sensor
int analogValue2 = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue2 - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" C");
// LED
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
// big stepper
// step one revolution in one direction:
Serial.println(" Direction :: clockwise ");
bigStepper.step(stepsPerRevolution);
// Servo
myservo.write(180); // Open door
delay(1000);
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
// step one revolution in the other direction:
Serial.println(" Direction :: counterclockwise ");
bigStepper.step(-stepsPerRevolution);
myservo.write(0); // Open door
delay(1000);
}