Hi sorry this is probly really simple for more advanced people but im ripping my hair out here.
So i have 2 motors and to "sensors" that connect for a second every few rotations of the motor (the line will be closed for a second)
this will be recieving a signal from rc equipment from servo line
when power is on i want this to happen
signal in is high
M1 starts
S1 gets closed
M1 stops
if signal is still high
M2starts
S2 gets closed for second
M2stops
If signal still high
M1 "repeat again as long as still high"
i want to be able to flip swich to high then back down and motor one does its full cycle M start S gets hit M stops atm i flip it up then down it will stop and motor will just keep running if i fip fast.
the the sensors are 2 pieces of metal and when the motor spins there are notches that press metal pices together closing the line
johnbuddha:
So i have 2 motors and to "sensors" that connect for a second every few rotations of the motor (the line will be closed for a second)
this will be recieving a signal from rc equipment from servo line
when power is on i want this to happen
signal in is high
M1 starts
S1 gets closed
M1 stops
if signal is still high
M2starts
S2 gets closed for second
M2stops
If signal still high
M1 "repeat again as long as still high"
What I read from your description that you have a "Finite State Machine" (FSM) which can be operating in three states:
NoMotorRunning()
Motor1Running()
Motor2Running()
For a FSM program the loop function could look something like:
byte fsmState=0;
void loop()
{
if (fsmState==0) NoMotorRunning();
else if (fsmState==1) Motor1Running();
else if (fsmState==2) Motor2Running();
}
Your task would be to write those three state-functions for your FSM.
In example for the NoMotorRunning() function, which starts motor1 after the signal going HIGH.
void NoMotorRunning()
{
if (digitalRead(signalPin)==LOW)
{
digitalWrite(motor1pin,LOW);
digitalWrite(motor2pin,LOW);
}
else
{
digitalWrite(motor1pin,HIGH);
fsmState=1; // switch over to the state with motor1 running
}
}
Thank you Jurs that is much more cleaner than what i have this is first code XD, gona look more into what fsm is and i should have some results soon, many thanks
thank you again but after researching fsm im even more confused on this coding then i was before i have no idea how to right a state >.> or even if what i was writing was correct.
johnbuddha:
when power is on i want this to happen
signal in is high
M1 starts
S1 gets closed
M1 stops
if signal is still high
M2starts
S2 gets closed for second
M2stops
If signal still high
M1 "repeat again as long as still high"
Ok. LIke this:
enum State {
OFF = 0,
M1 = 1,
M2 = 2
} state = OFF;
void setup() {
}
void loop() {
switch (state) {
case OFF:
if (signalIsHigh()) {
startM1();
state = M1;
}
break;
case M1:
if (s1isClosed()) {
stopM1();
if (signalIsHigh()) {
startM2();
state = M2;
}
else {
state = OFF;
}
}
break;
case M2:
if (s2isClosed()) {
stopM2();
if (signalIsHigh()) {
startM1();
state = M1;
}
else {
state = OFF;
}
}
break;
}
}
// you will need to fill these stub functions in
boolean signalIsHigh() { return false; }
boolean s1isClosed() { return false; }
boolean s2isClosed() { return false; }
void startM1() {}
void stopM1() {}
void startM2() {}
void stopM2() {}
I'm not sure this will work, though. If we stop m1 as soon as s1 closes, won't s1 still be closed when we switch back to it once m2 is done? Or are you certain that there will always be enough momentum to carry the motor slightly beyond that point?