Been jigging with this for a while,
Basic system -
UNO Board,
Arduino spdt relay, SRD-05VDC-SL-C (set to normally open)
Arduino vibration sensor or accelerometer, WYPH SW-420
DFROBOT Inductive Prox 5V NPN normally open.
Relay drives a 6V, 3rpm geared motor on a seperate power supply.
Motor has an elliptical shaped rotor on the spindle.
Prox picks up the rotor twice per revolution, giving it two home positions or stop positions 180 degrees apart (at least that is what I'm trying to achieve)
Started out editing a basic LED blink code & it really only needed the Delays adjusted to 5000msec to give me 90 degrees of rotation in each step of the loop - 5 sec on, 5 sec off, 5 sec on again....
The Loop is initiated by the input from the Vibration sensor.
All good up until this point, but now trying to introduce the input from the prox as a point to end the loop is giving me some grief.
It is now just running a continuous loop of 5 on 5 off over & over. Even after I removed the last line of the loop - digitalwrite (relay, OFF)
As you will see in this, I'm using a "current condition v's previous condition" code that I borrowed & began tweaking but it has me stumped now.
Can any of you guys pls suggest a fix, or even a better way to code this device pls?
/*//==============================================================================//
* Vibration Sensor interfacing with Arduino
* Date: - 15-04-2019
* Author:- Sourav Gupta
* For:- circuitdigest.com
*/ //=============================================================================//
#include <Arduino.h>
#include <stdio.h>
#define ON 1
#define OFF 0
/*
* Pin Description
*/
int vibration_Sensor = A5;
int relay = 13;
int prox = A1;
/*
* Programme flow Description
*/
int present_condition = 1;
int previous_condition = 0;
/*
* Pin mode setup
*/
void setup() {
pinMode(vibration_Sensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(prox, INPUT);
}
/*
* relay run
*/
void (relay_run)();
/*
* main_loop
*/
void loop() {
previous_condition = present_condition;
present_condition = digitalRead(A5); // Reading input from the A5 Pin of the Arduino.
previous_condition = digitalRead(A1); // Reading input from proximity.
if (present_condition != previous_condition)
(relay_run)(); //if present condition is not equal to previous condition (receiving voltage from Both A1 & A5), Begin Loop.
if
(previous_condition)
digitalWrite(relay, OFF); //once back to previous condition (reading voltage from A1 only), End Loop.
}
void (relay_run) () {
digitalWrite(relay, ON);
delay(5000);
digitalWrite(relay, OFF);
delay(5000);
digitalWrite(relay, ON);
}
Thx, Mick.
