Pretty much. maybe detect & execute?
I uploaded this the other night & it is still doing some crazy stuff.
Not stopping when it picks up the Prox, & pausing when it shouldn't.
This what I have in mind - Laymans code -
A5 INPUT, HIGH == Go (ignore A1 input)
A1 INPUT, HIGH == Stop (end of Loop)
Loop
digitalWrite, 13 HIGH;
delay, 5000
digitalWrite, 13 LOW; (this is when the paddle is virtical)
delay, 5000
digitalWrite, 13 HIGH;
How do we get that into a usable C++ code?
Mick.
If triggered, start the motor AND a timer long enough to move away from the prox switch, stop the motor when it next gets to the prox.
Pseudo code:
If (trigger)
start 2 second timer;
if(timer_running or proxSwitch == HIGH)
digitalWrite(Relay, ON);
else if(proxSwitch == LOW)
digitalWrite(Relay, OFF);
OR:
if (trigger)
start 2 second timer:
digitalWrite(Relay, timer_running or digitalRead(proxSwitch)==HIGH);
One option:
It's a sequential process. Break it down to its elemental 'steps'. The most basic unit is "wait for some condition(s) to be met then go the next state, do something, like command a motor to run, then wait for something else to happen. Lather, rinse, repeat.
Put it all in a state machine (numerous [url=http://[tutorial](State machines - Introductory Tutorials - Arduino Forum)s on this site) using the switch/case construct.
Put some serial printing in there to find out what is happening.
You may have floating inputs q.v. How is it all wired up? You might want to look at input_pullup.
Got a chance to get back to this today.
Houston, we have a problem!
The Prox I bought was listed Normally open but upon testing today, it is acting as a normally closed switch.
ie, voltage signal when there is nothing acting on it.
I can't seem to find a small, rectangular 5V Prox that is a true N/O switch.
The Plot Thickens.
I did up a Vid on it this morning as well so you can see the configuration.
Mick.
Shouldn't matter as long as it changes at the proximity point. You can just reverse it in the code.
The Prox I bought was listed Normally open but upon testing today, it is acting as a normally closed switch.
It does not matter what its normal state is. You can detect the state that it is in and determine whether it is currently open or closed and also when it becomes open or closed.
wildbill:
#define ON 1
#define OFF 0
const unsigned long IndicatorDelay=5000;
const unsigned long IndicatorHideTime=5000;
byte vibration_Sensor = A5;
byte relay = 13;
byte proximitySensor = A1;
int present_condition = 1;
int previous_condition = 0;
void setup()
{
pinMode(vibration_Sensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(proximitySensor, INPUT);
DoIndicatorCycle(); // Make sure Indicator is hidden
}
void loop()
{
if(digitalRead(vibration_Sensor)==HIGH)
{
DoIndicatorCycle();
}
}
void DoIndicatorCycle()
{
ShowIndicator();
delay(IndicatorDelay);
RunMotor(IndicatorHideTime);
}
void ShowIndicator()
{
digitalWrite(relay, ON);
while(digitalRead(proximitySensor)==LOW)
;
digitalWrite(relay, OFF);
}
void RunMotor(unsigned long Milliseconds)
{
digitalWrite(relay, ON);
delay(Milliseconds);
digitalWrite(relay, OFF);
}
Hi Bill.
A few things I can see in this:-
You have a time delay in the run motor sequence.
this part of the loop is not time dependant, it should simply run until the voltage drops off the A1 Prox.
The other thing which seems odd is there seems to be nothing triggering the relay off (as in the prox) in this section.
I had a crack at tweaking it but now I'm getting Errors.
Pls help.
#define ON 1
#define OFF 0
const unsigned long IndicatorStart=5000;
const unsigned long IndicatorDelay=5000;
byte vibration_Sensor = A5;
byte relay = 13;
byte proximitySensor = A1;
int present_condition = 1;
int previous_condition = 0;
void setup()
{
pinMode(vibration_Sensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(proximitySensor, INPUT);
DoIndicatorCycle(); // Make sure Indicator is hidden
}
void loop()
{
if(digitalRead(vibration_Sensor)==HIGH)
{
DoIndicatorCycle();
}
}
void DoIndicatorCycle()
{
ShowIndicator();
delay(IndicatorDelay);
RunMotor();
}
void ShowIndicator()
{
digitalWrite(relay, ON);
while(digitalRead(proximitySensor)==LOW);
delay(IndicatorStart);
digitalWrite(relay, OFF);
delay(IndicatorDelay);
}
void RunMotor ()
{
digitalWrite(relay, ON);
if(digitalRead(proximitySensor)==LOW);
digitalWrite (relay, OFF);
}
Never mind. ![]()
Ivé figured it out.
Instead of Using the prox to end the loop (as per the thread tittle) your code is using the prox signal to for the delay in the middle of the loop!
It is working........90 degrees out of whack! :o
I can easily reposition the prox to pick up the paddle in the vertical
position.
The trouble is, it will be drawing current (albeit small) whilst in the parked position.
Thx, Mick.
billybushcook:
The trouble is, it will be drawing current (albeit small) whilst in the parked position.
Rather than power it from the 5V rail, you could use an I/O pin and then you can provide power only when you're interested in what it is reading.
Surely there is a way to write this so it parks itself over the Prox though?
Iv'e got a few ideas I will try this evening at home.
Mick.
Mount four targets at ninety degrees to each other - one extended target for each paddle, to give a longer dwell time, and one regular target for each hidden position.
From any rest position start the motor and run it until the next target is seen and then stop. Adapt the long/short press feature from a keypad library to distinguish between the two targets and thereby know the rotor position.
From the hidden position the *hit sensor * jogs the motor to start it toward the presented position. In the presented position the *timer * jogs the motor to start to the hidden position.
Sounds like being over complicated though?
To me, the objective sounds simple:-
Vib sensor, HIGH = Relay, ON - 5 sec
Relay, OFF - 5 sec
Relay, ON
Prox, LOW = Relay, OFF
The trouble is I'm C++ Illiterate atm. ![]()
Mick.
What does the prox sensor do now?
I suspect that you could probably use the code I posted earlier, but you may need to look for LOW rather than HIGH as the sign that it triggered.
When I changed this LOW to a HIGH.
void ShowIndicator()
{
digitalWrite(relay, ON);
while(digitalRead(proximitySensor)==LOW)
;
digitalWrite(relay, OFF);
}
That is how I got it to run 90 degrees out of whack.
Prior to that, it had a delay (Relay, OFF) right off the start & paid no attention to the state of the prox at any time.
The other issue I see in that (as previously mentioned) is that you have the "runmotor" Class (sorry I called it a sequence before reading up on things this morning) set to run on time delays.
ie, there is nothing in there to trigger a (Relay, OFF) when the state of the prox goes from High to LOW.
Nailed it!!!! 8)
I took your principal of breaking the Loop into separate Classes & to simplify things for my own benifit, removed the set up delays, adding them directly into the "ShowIndcator" Class.
Then set the condition "while" on the runMotor Class so it would cut out as soon as that condition changed!!!!
#define ON 1
#define OFF 0
byte vibration_Sensor = A5;
byte relay = 13;
byte proximitySensor = A1;
int present_condition = 1;
int previous_condition = 0;
void setup()
{
pinMode(vibration_Sensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(proximitySensor, INPUT);
DoIndicatorCycle(); // Make sure Indicator is hidden
}
void loop()
{
if(digitalRead(vibration_Sensor)==HIGH)
{
DoIndicatorCycle();
}
}
void DoIndicatorCycle()
{
ShowIndicator();
RunMotor();
}
void ShowIndicator()
{
digitalWrite(relay, ON);
delay (5000);
digitalWrite(relay, OFF);
delay (5000);
}
void RunMotor()
{
digitalWrite(relay, ON);
while(digitalRead(proximitySensor)==HIGH);
digitalWrite(relay, OFF);
}
Video for you.
That's great, nice to see it working. Is it time to go shooting?
Miserable Weather here ATM, East Coast Australia.
Huge low pressure system, causing havoc.
I have a Long Range get together coming up in October so it is time to build a few more units now I have it sorted. ![]()
Thx for the help.
Next project - Variable Thumbstick control for a remote mounted spotlight turret I built a few yrs ago & have been running on analog.
Mick.