Flying ghost prop

Hi everyone. New to Arduino coding. I'm a big Halloween nut.

Trying to make a flying ghost prop.

The prop is activated by a PIR sensor, run a motor for 30 seconds, stop, reverse direction, and then (I want it to) stop when a hall effect sensor is triggered, I have most of the code working, I just can't figure out how to make the hall effect sensor stop the motor. Attached is my sketch. TIA

#include "BTS7960.h"

const uint8_t EN = 8;
const uint8_t L_PWM = 9;
const uint8_t R_PWM = 10;
int PIR = 4;
int hall = 6;
int LONG = 30000;
int RIGHT_TIME = 30000;
int RAMP = 250;

BTS7960 motorController(EN, L_PWM, R_PWM);

void setup() {
  Serial.begin(9600);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT);
}

void loop() {
  if (digitalRead(PIR) == LOW) {
    motorController.Enable();

    for (int speed = 0; speed < 255; speed += 10) {
      motorController.TurnRight(speed);
      delay(RAMP);
    }
    delay(RIGHT_TIME);
    for (int speed = 255; speed > 0; speed -= 10) {
      motorController.TurnRight(speed);
      delay(RAMP);
    }
    motorController.Stop();

    for (int speed = 0; speed < 255; speed += 10) {
      motorController.TurnLeft(speed);
      delay(RAMP);
    }
    delay(30000);
  }
  int sensorStatus = digitalRead(hall);
  if (sensorStatus == 1) {
    motorController.Stop();
    motorController.Disable();
  }
  delay(3000);
}

What Hall Effect sensor?

Does it output a high or low level signal when triggered? Is it a totem pole output or open collector?

The way your code is presently written, the Hall Effect input pin isn't looked until 30 seconds after the final for loop finishes. Is that what you intended?

Using a HiLetgo sensor https://www.amazon.com/dp/B01NBE2XIR?ref=ppx_yo2ov_dt_b_fed_asin_title. Sensor sends a high signal when triggered.

That is where my problem is, I need the motor to run CW until sensor is triggered.

Have you verified sensorStatus is HIGH when triggered? Use Serial.print(sensorStatus); to view the state of sensorStatus

I have not verified it with serial print. I did mock up a little test on a breadboard to test sensor and what pins on sensor are what. Made a LED turn on and off.

Show this sketch.

Please answer the above question.

int HallSensor = 2; // Hall sensor is connected to the D2 pin
int LED = 13; // onboard LED pin
void setup() {
  pinMode(HallSensor, INPUT); // Hall Effect Sensor pin INPUT
  pinMode(LED, OUTPUT); // LED Pin Output
}
void loop() {
  int sensorStatus = digitalRead(HallSensor); // Check the sensor status
  if (sensorStatus == 1) // Check if the pin high or not
  {
    // if the pin is high turn on the onboard Led
    digitalWrite(LED, HIGH); // LED on
  }
  else  {
    //else turn off the onboard LED
    digitalWrite(LED, LOW); // LED off
  }
}

I am sorry ... I failed to read the sketch... as mentioned in post #2, the problem is in the use of delay()...

OK. NP. That's my problem. I don't know how to make the motor run CW till sensor is triggered. Going CCW is fine with delay(); of a set time.

Your hall sensor gets about two milliseconds over two minutes of looping.

RIGHT TURN
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 
255 245 235 225 215 205 195 185 175 165 155 145 135 125 115 105 95 85 75 65 55 45 35 25 15 5 

LEFT TURN
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 
Delay 30...Delay finished.

(start millis=21521)
Read hall: 0
(stop millis= 21522)

Sorry @xfpd, I don't know what you mean.

"One millisecond" is the time you have allotted to read the hall sensor at the end of three "turn" loops, a 30 seconds delay, and another 3 seconds delay.

Would you, in a time-line, describe how you want your PIR, Hall, right turn, stop, left turn, stop to work?

On the outward journey, you speed the motor up from stationary to max speed, then slow it down and stop it. Then wait 30s. Then you speed the motor up from stationary to maximum speed in reverse. Then there is another 30s delay. At this time the motor is still at full speed in reverse. Will something be damaged?

I think you need to slow down the motor, but not to stationary, but a slow speed. Then start checking the hall sensor and stop the motor when the sensor is activated.

I want pass byers to trip PIR and send ghost across my yard and stop and a point (will be a time not distance) I want to make sure it doesn't travel to far and run into the end pulley or fence, reverse travel and stop (about 2 ft away) using the hall sensor before it crashes into the motor assembly. Waiting 30 seconds before it can be triggered again.

It doesn't wait 30 seconds, reverses travel as soon as it stops. I'm using a wiper motor, speed is not very fast. It takes about 30 seconds to travel 50 ft across my yard. The 30s delay after the reverse direction is to keep the motor moving till the hall sensor is triggered. That's what I'm having an issue with, keeping the motor running till I want it to stop with the hall sensor

@TK3D - Does this look close?

  • if PIR tripped
    • enable motor
    • increase motor speed
    • ?? where is half-way ??
    • decrease motor speed
    • ?? where is the end ??
    • change direction
    • increase motor speed
    • ?? where is half-way ??
    • decrease motor speed
    • ?? where is the end ??
  • if PIR NOT tripped
    • wait 30 seconds
  • restart

Apologies, you are correct. The 30s wait is while it is at full speed in forward direction. There is no wait before it reverses.

But as I said earlier, it does not slow down when moving in reverse and will hit the hall sensor at full speed. Is this intentional?

You already have code that will stop the motor when the hall sensor is activated. The only problem with that code that I can see is the final delay(300) which will cause the hall sensor to be checked only once every 3s, and so could cause the signal from the hall sensor to be missed, especially if the motor is at full speed. So I recommend removing that last delay().

Also I can see a danger that, while travelling in reverse and checking the hall sensor, if the PIR sensor is still activated, or gets activated again, it will go back into forward motion again. Because it never made back to the start position, it could overshoot at the other end of the track.

So I think you also want to change the code so that the PIR sensor is not checked again until the hall sensor has been triggered. Also you may want to check that the PIR sensor is no longer active before you allow the whole process to begin again.

Close...

  1. PIR trip
  2. increase motor speed
  3. run for time? TBD based on location of prop
  4. decrease speed after TBD time
  5. change direction
  6. increase motor speed
  7. stop with hall effect sensor input
  8. waiting 30 second before next trigger

NP. I'm limited on space for a slow down to stop function. The speed stopping abruptly won't hurt anything.

I understand the hall not being checked except for every 3 sec. The last delay is suppose to pause the whole code for 30s till the next PIR trigger. I'll play around with it tonight.