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);
}
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?
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.
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
}
}
"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
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.
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.