Vertical Sliding Doggie Door

Hello all,

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):

2 Arduino Unos

L298N Motor Driver

https://www.amazon.com/Wireless-Transmitter-Receiver-Arduino-Raspberry/dp/B08TMKKZ74/ref=asc_df_B08TMKKZ74/?tag=hyprod-20&linkCode=df0&hvadid=647314916867&hvpos=&hvnetw=g&hvrand=2236717111144598902&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1028087&hvtargid=pla-1968245714851&psc=1

Here's what I have for the code so far:

Sensor Box Code:

#include <Wire.h>
#include <RH_ASK.h>

RH_ASK rf_driver;

const int rainSensorPin = A0;
const int tempSensorPin = A1;

void setup() {
Serial.begin(9600);
if (!rf_driver.init()) {
Serial.println("RF driver initialization failed");
}
}

void loop() {
// Read sensor values
int rainValue = analogRead(rainSensorPin);
int tempValue = analogRead(tempSensorPin);

// Convert analog readings to actual values 
float rainVoltage = map(rainValue, 0, 1023, 0, 5);
float temperature = map(tempValue, 0, 1023, 0, 100);

// Check if it's raining
if (rainVoltage > 2.5) {
sendCommand("RAIN_DOWN");
} else {

// Check temperature
if (temperature > 60) {
sendCommand("TEMP_UP");
} else {
sendCommand("TEMP_DOWN");
}
}

delay(600000); // Delay for 10 minutes
}

void sendCommand(const char* command) {
rf_driver.send((uint8_t*)command, strlen(command));
rf_driver.waitPacketSent();
}

Motor Box Code:

#include <Wire.h>
#include <RH_ASK.h>
#include <Adafruit_MotorShield.h>

RH_ASK rf_driver;

// Define motor and encoder
Adafruit_DCMotor *motor = AFMS.getMotor(1); 
const int encoderPinA = 2; 

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 buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);

if (rf_driver.recv(buf, &buflen)) {
buf[buflen] = '\0'; // Null-terminate the received string
handleCommand((char*)buf);
}
}
}

void handleCommand(const char* command) {
Serial.println(command);

// Handle different commands and control motor accordingly
if (strcmp(command, "RAIN_DOWN") == 0) {
moveMotorDown();
} else if (strcmp(command, "TEMP_UP") == 0) {

moveMotorUp();
} else if (strcmp(command, "TEMP_DOWN") == 0) {
moveMotorDown();
}
}

void moveMotorUp() {
}

void moveMotorDown() {
}

One edit. I decided to simplify by just opening it if it is over 60 degrees

before everything put together you should test each piece alone.

#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
    }
  }
}

A few ideas.

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.

 

note: reply was meant for @shrektum

Hello shrektum

Welcome to the world's best Arduino forum ever.

For a long time I have been following the topic with the use of this type of wireless module and the associated software functions.

For wireless projects I recommend the use of the HC12 module.

What are the advantages:

  1. no external functions from a library are needed.
  2. the development of a communication protocol is in your hands
  3. the necessary development steps can be programmed and tested without using the wireless module
  4. the radio module can be easily configured for the project requirements
  5. both transmitter and receiver are contained in one radio module.

hth

Have a nice day and enjoy coding in C++.

yes. and it is no problem. if we want to give useful advise then it is recommended to set arduino to sleep for energy saving purpose.

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.

Specifications

  • Operating voltage: between 6 V and 24 V
  • Nominal voltage: 12 V
  • Free-run speed at 12 V: 126 RPM
  • Free-run current at 12 V: 46 mA
  • Stall current at 12 V: 1 A
  • Stall torque at 12 V: 4.2 kg·cm
  • Gear ratio: 1:34
  • Reductor size: 21 mm
  • Weight: 99 g

It looks like a fun project!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.