How to code HC-SR501 PIR to work with 28BYJ-48 Unipolar Stepper

Hello everyone,

I’m a complete beginner who is at a wall with how to code. I’m working on a project that is an automatic toilet seat lid and seat the size of a child first toilet. The motors I’m using are 2 28BYJ-48 Unipolar Steppers with 2 ULN2003 drivers, one for each. The sensors I’m using are 3 HC-SR501 PIR Motion Detectors. I’m using a 5v power supply.

The idea is that the first sensor when triggered will lift the seat and lid together. If the person needs the seat down they do a small motion with their foot on a sensor facing down that triggers the seat motor. If they do decide to sit down the third sensor will be attached to the underside of the lid that will monitor them so when they get up the lid closes. If the seat is not dropped after the initial rise, there will be a timer set at 90 secs that will drop the seat and lid together.

The mess of code I have attached is mostly for the motors from a library that I’ve gotten to work testing the motors out. The other stuff is my attempt at trying to put in code for the sensors. Am I on the right track at all? Please treat me like I’m five. Thank you for your help.

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define Constants

int inputPin1 = 2;               // choose the input pin (for PIR sensor)
int pirState1 = LOW;             // we start, assuming no motion detected
int val1 = 0;

int inputPin2 = 3;               // choose the input pin (for PIR sensor)
int pirState2 = LOW;             // we start, assuming no motion detected
int val2 = 0;

int inputPin3 = 4;               // choose the input pin (for PIR sensor)
int pirState3 = LOW;             // we start, assuming no motion detected
int val3 = 0;

// Define step constants
#define FULLSTEP 4
#define FULLSTEP 4

// Define Motor Pins (2 Motors used)

#define motorPin1  10     // Blue   - 28BYJ48 pin 1
#define motorPin2  11     // Pink   - 28BYJ48 pin 2
#define motorPin3  12    // Yellow - 28BYJ48 pin 3
#define motorPin4  13    // Orange - 28BYJ48 pin 4
                        
                        
#define motorPin5  6     // Blue   - 28BYJ48 pin 1
#define motorPin6  7     // Pink   - 28BYJ48 pin 2
#define motorPin7  8     // Yellow - 28BYJ48 pin 3
#define motorPin8  9     // Orange - 28BYJ48 pin 4

// Define two motor objects
// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

void setup()
{
  // 1 revolution Motor 1 CW
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(500);
  stepper1.moveTo(1024);  
  
  // 1 revolution Motor 2 CCW
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(50.0);
  stepper2.setSpeed(500);
  stepper2.moveTo(1024);
  
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
  
  Serial.begin(9600);

}

void loop(){
  if (inputPin1 = (HIGH););
    stepper2.moveTo(1024));
    stepper1.moveTo(1024));
    
}

I would reply to a 5 year old by telling them to work with sample programs for each of those devices until you understood exactly how they work and why they work that way and what the sample program actually does to make them work.

Only then should the 5 year old think about the logic of putting together several of those devices into a program.

Paul

Paul_KD7HB:
I would reply to a 5 year old by telling them to work with sample programs for each of those devices until you understood exactly how they work and why they work that way and what the sample program actually does to make them work.

Only then should the 5 year old think about the logic of putting together several of those devices into a program.

Paul

Wow Paul, ever heard of a sense of humor? Thanks for stating the obvious though and telling me what I could have just read off of a getting started with Arduino page. Next time try giving some actual constructive criticism or maybe some advice that someone can actually use. You’re the owner and president of an electronics company, be better than this Paul.

-Dap122893