Question: //im trying to create a code for arduino that will open and close a door using a servo and photocell and display on lcd when
//im trying to create a code for arduino that will open and close a door using a servo and photocell and display on lcd when the door open and closes, im not sure how to make it work i created functions but somehow its not calling the servo to function. the photocell is supposed to open door at 7am or when theres 30% light and close door when theres 40% darkness. it should call servo to function when it meets those requirements my code could be way off im not sure. any help is welcome step by step thanks.
int h=12; int m=0; int s=0; int flag=7; int TIME; const int hs=8; const int ms=9; int photocellPin = 0; int servoPin = 9; int pos = 0; int state1; int state2; #include
// Set the pins on the I2C chip used for LCD connections: // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol LiquidCrystal_I2C lcd(lcdAddress, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() { // put your setup code here, to run once: lcd.begin(lcdColumns, lcdRows); lcd.backlight(); lcd.clear(); // toRun(); Serial.print(9600);
//Brought in from other file Serial.begin(9600); servo1.attach(9); }
//Loop from other file void toRun() {
lcd.print("Cell = "); lcd.println(analogRead(photocellPin)); pos = analogRead(photocellPin); pos = constrain(pos,220,740); int servoPos = map(pos, 220, 740, 255, 0); int servoDegree = map(servoPos, 0, 255, 0, 180); servo1.write(servoDegree); //lcd.print("Servo Degree = "); //lcd.println(servoDegree); }
/*void cellOn() {
if(flag == 7){
photocellPin = 1;
//Servo open servo1.attach(9); }
if(flag == 17){
photocellPin = 0;
//Servo close servo1.attach(9); } } */ void loop() { // put your main code here, to run repeatedly: lcd.setCursor(0,0); cellOn();
//delay(1000); lcd.print("Time"); lcd.print(h); lcd.print(":"); if(m < 10) { lcd.print("0"); } lcd.print(m); lcd.print(":"); if(s < 10){ lcd.print("0"); } lcd.print(s); lcd.print(" ");
if(flag<12)lcd.print("AM"); if(flag==12)lcd.print("PM"); if(flag>12)lcd.print("PM"); if(flag==24)flag=0;
s=s+1;
//Check to see if photocell needs to be turned on or off //cellOn();
//loop from previous file will now run //toRun();
delay(1000);
lcd.clear(); if(s==60){ s=0; m=m+1; } if(m==60) { m=0; h=h+1; flag=flag+1; } if(h==13) { h=1; } lcd.setCursor(0,1); lcd.print("Your Time");
//-------Time // setting-------// //Remove comments if this is really needed /** state1=digitalRead(hs); if(state1==1) { h=h+1; flag=flag+1; if(flag<12)lcd.print("AM"); if(flag==12)lcd.print("PM"); if(flag>12)lcd.print("PM"); if(flag==24)flag=0; if(h==13)h=1; } state2=digitalRead(ms); if(state2==1){ s=0; m=m+1; } **/ }
void cellOn() {
if(flag == 2 && m == 0 && s == 0){
photocellPin = 1;
//Servo open servo1.write(servoDegree); }
if(flag == 3 && m == 0 && s == 5){
photocellPin = 0;
//Servo close servo1.write(servoPos); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
