I am making a vertical sliding doggie door and I'm not sure how to begin programming it. Essentially, I'd like my dog to be able to access the back yard when it isn't raining and when the temperature is between 50-90 degrees Fahrenheit. The goal is to use RF transmitters to send the signals from outside to inside. I'll be 3D printing 2 separate boxes to house the components, one for the sensors and one for the motor. The motor box will be powered by an 8 AA battery box and the sensor box will be powered by a 3 AA battery box. I cannot figure out how to control these motors. No function I've tried has worked with my code. I've written my own, I've found some online and modified them, all to no avail. I'm at a blank with these functions. Is there something wrong with the rest of the code? Or am I just bad at writing code for motors with encoders?
Components (I already had all of this stuff lying around except the RF transmitters):
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
const int rainSensorPin = A0;
const int tempSensorPin = A1;
// transmitter pin 12
void setup() {
Serial.begin(9600);
if (!rf_driver.init()) {
Serial.println("RF driver initialization failed");
}
}
void loop() {
// int rainValue = analogRead(rainSensorPin);
int tempValue = analogRead(tempSensorPin);
// Check if it's raining
if (
//rainValue > 512 && //due to post #2
tempValue > 610) sendCommand("ALLOWED");
else sendCommand("noAllow");
delay(600000); // Delay for 10 minutes
}
void sendCommand(const char* command) {
rf_driver.send((uint8_t*)command, strlen(command));
rf_driver.waitPacketSent();
}
#include <Wire.h>
#include <RH_ASK.h>
#include <Adafruit_MotorShield.h>
#include <SPI.h>
RH_ASK rf_driver;
// Define motor and encoder
Adafruit_DCMotor *motor = AFMS.getMotor(1);
// receiver pin 11
void setup() {
Serial.begin(9600);
if (!rf_driver.init()) {
Serial.println("RF driver initialization failed");
}
// Setup motor and encoder
AFMS.begin();
motor->setPWM(255);
pinMode(encoderPinA, INPUT);
}
void loop() {
if (rf_driver.available()) {
uint8_t buflen = 8;
char buf[buflen];
if (rf_driver.recv(buf, &buflen)) {
buf[buflen] = '\0'; // Null-terminate the received string
Serial.println(buf);
// Handle different commands and control motor accordingly
if (strcmp(buf, "ALLOWED") == 0) motor->run(FORWARD);
if (strcmp(buf, "noAllow") == 0) motor->run(BACKWARD);
for (int i = 0; i < 255; i += 5) { // acceleration
motor->setSpeed(i);
delay(10);
}
delay(1000); // <------------- ajust time to open/close
motor->setSpeed(0);
motor->run(RELEASE); // motor power off to save energy
}
}
}
Once you get it working you should change the sending of Strings. Strings are for humans. You can send a single byte which shows what state you want the motor to be in. This means you dont have to be using string compare. Less rf data to send as well.
0 for raining
1 for outside temp bounds
2 for good
If you want human readability just use some defines for readability for comparing #define RAINING 0
Another thing you have to factor in. You only want your motor to run when there is a state change. If it recieves 2 commands to close, will it try to close twice? Or if its closed already and a command is sent to close, will it try to close again, potentially breaking something or burning the motor.
You are probably best of also having a limit switch for hitting the open and close on your doggy door. As a safety measure you could also have a timeout for how long motor can try to run for (in case your dog was stuck) and if it hasnt hit the limit switch for close after that timeout, reopen the door until you hit the open limit switch
To save power your probably going to want both devices to power down and power up every 30 minutes or so. Batteries arent infinite.
Does your rf driver have checking to see if the packet was recieved?
You also need to factor as well. What if the temperature heats up and the dog is outside then the door closes on him.
You should address this sooner rather than later. Once the delay starts NOTHING will happen for the set time. As in, no inputs checked, no computations done, no outputs updated.
Welcome! FYI the L298N Motor Driver is a bad choice. Find something that uses MOSFETs as output devices. The L298N uses bipolar output devices in a darlington configuration so you lose about 3V from the battery to the motor which is burnt up as heat.
I found this, I hope it helps.
This JGA25-371 DC gearmotor features an integrated encoder which provides a resolution of 12 counts per revolution, ensuring an accurate control of motor's speed.