Cyclic Positional Trigger Mechanism

I'm trying to make a circuit whereby when a button is pressed OR held down a motor rotates a flywheel. The flywheel has a marker attached that triggers a lever microswitch every time it rotates past that point. I want it to: Spin flywheel when button is pressed Stop when the microswitch is triggered If button is being held down continue through the microswitch into another cycle.

What would be the best way to code this? A while statement for the button and a break for the position microswitch?

What would be the best way to code this?
A while statement for the button and a break for the position microswitch?

Yes.

It is the nature of flywheels that they do not stop instantly. Have you allowed for that?

The code should be straightforward - something like this pseudo code (assuming it is in loop() or in functions called from loop().

if button was OFF and is now ON
  start the motor

If the button is OFF
  if the microswitch is ON
     stop the motor

...R

Well its not really a flywheel… its more a crank system. Here’s my code;

/*********************  FALCONER MK II  *********************/
/*           www.thingiverse.com/AcrimoniousMirth           */
/*           www.youtube.com/user/AcrimoniousMirth          */
/************************************************************/
/*  Control code for the Falconer MK II Nerf-style gauntlet.
 *  The gauntlet electronics are as follows:
 *  -Arduino Nano                                (x1)
 *  -RCX Z R 1804 2400KV Micro BLDC              (x2)
 *  -SimonK 30A ESC                              (x2) 
 *  -Worm Gear Motor DC 370 JSX-370              (x1)
 *  -7.4V 1300mAh 35C LiPo                       (x1)
 *  -SPST Lever Microswitch                      (x1)
 *  -Simple Push Button                          (x2)
 *  -TIP120 Transistor                           (x1)
 *  -1N4004 Diode                                (x1)
 *  -1K Resistor                                 (x1)
 *  The two push buttons each control the flywheel and ramrod 
 *  part of the circuit. The flywheel uses the BLDC and the
 *  ramrod uses the geared motor. The BLDCs are controlled by
 *  the ESCs and the power to the geared motor is run through
 *  the TIP120 as a relay. The lever microswitch is attached 
 *  to the base in such a way that every time the crank 
 *  reaches the back spot its triggered, sending simple
 *  positional data to the Arduino.
 *  The code has been adapted from these 2 sources:
 *  https://dronesandrovs.wordpress.com/2012/11/24/how-to-control-a-brushless-motor-esc-with-arduino/
 *  http://www.instructables.com/id/Use-Arduino-with-TIP120-transistor-to-control-moto/
 *  The first helps calibrate ESCs,refer to it if you need to.
 *  Pins are as follows:
 *  3v3 - low power rail
 *  GND - ground
 *  VIN - 7.4V rail (acting as power input to Arduino)
 *                   DO NOT HAVE ANY OTHER POWER IN
 *   6 - Lever Microswitch, NO pin (if SPDT)
 *   7 - Flywheel switch
 *   8 - Trigger
 *  10 - ESC data pin
 *  12 - TIP120 Base
 */

#include <Servo.h>  //The BLDCs  can be treated as servos.

//Flywheel
Servo ESC;
int throttle = 179; //Speed value for the ESCs.
int ESCpin = 10;    //Both ESC data connected to same pin

//Ramrod
int TIP120 = 12; //Connected to Base pin

//Switches
int Trigger = 8;
int Flywheel = 7;
int PosiSwitch = 6;
 
void setup()
{
//Flywheel
ESC.attach(ESCpin);

//Ramrod
pinMode(TIP120, OUTPUT); 

//Switches
pinMode(Trigger, INPUT);
pinMode(Flywheel, INPUT);
pinMode(PosiSwitch, INPUT);
}
 
void loop()
{
int Fval = digitalRead(Flywheel);
while (Fval == HIGH){
  ESC.write(throttle);
  int Pval = digitalRead(PosiSwitch); //What's its position?
  
  //It's not at dead point, so rotate.
  if (Pval == LOW){analogWrite(TIP120, 255);}
  
  //It's at dead point, so check trigger.
  if (Pval == HIGH){
    int Tval = digitalRead(Trigger);
   
    //Trigger is pulled, so rotate.
    if (Tval == HIGH){analogWrite(TIP120, 255);}
   }
  else analogWrite(TIP120, 0);
}
ESC.write(0);

}

You only check Fval before the WHILE so the WHILE can never terminate. You must also have the line

Fval = digitalRead(Flywheel);

within the WHILE loop.

However, if you look at the suggestion I made in Reply #2 you will see that there it does not use WHILE.

Also, my suggestion approaches the problem from a different point of view - maybe I misunderstood what you want?

...R

Sorry, I've done a rather shoddy job of explaining what's actually going on, however its rather tricky without an understanding of the full project. Your point about the while check is a good one though, thanks!

SJMaybury: Sorry, I've done a rather shoddy job of explaining what's actually going on, however its rather tricky without an understanding of the full project.

So why not tell us about the full project? Then maybe we could give more useful advice.

...R

Well, I’m designing my second generation Nerf-style foam dart launching gauntlet. It uses 2 BLDC drone motors with ESC controllers (that can be addressed like servos) for flywheels to propel the darts and a geared torque motor for the ramrod system that pushes the darts. There are three buttons/switches.
“Trigger” is used to push the darts using the ramrod motor
“Flywheel” is used to rev the flywheels
“PosiSwitch” is a lever switch mounted so that the crank that’s rotated by the ramrod motor will push it once every 360 degrees, thus marking a revolution.

The flywheels are referred to as ESC as that’s the method of control and both are addressed on the same pin.
The ramrod motor is referred to as TIP120 as that’s the transistor used to control it (acting as a low power relay)

When the Trigger is PRESSED the ramrod motor rotates until the PosiSwitch reads HIGH.
When the Trigger is LOW and the PosiSwitch is HIGH it doesn’t rotate.
When the Trigger is LOW and the PosiSwitch is LOW it rotates until the PosiSwitch is HIGH.
When the Trigger is HELD DOWN it rotates regardless of the PosiSwitch until its released at which point it stops at the next occurrence of there PosiSwitch being HIGH.

All this is in a WHILE for the flywheel ESCs as a safety feature to stop the ramrod pushing while the flywheels are stationary.

Basically the PosiSwitch is there to ensure that whether the Trigger is pressed or held down the ramrod always stops at the same spot.

Here’s the code atm.

/*********************  FALCONER MK II  *********************/
/*           www.thingiverse.com/AcrimoniousMirth           */
/*           www.youtube.com/user/AcrimoniousMirth          */
/************************************************************/
/*  Control code for the Falconer MK II Nerf-style gauntlet.
 *  The gauntlet electronics are as follows:
 *  -Arduino Nano                                (x1)
 *  -RCX Z R 1804 2400KV Micro BLDC              (x2)
 *  -SimonK 30A ESC                              (x2) 
 *  -Worm Gear Motor DC 370 JSX-370              (x1)
 *  -7.4V 1300mAh 35C LiPo                       (x1)
 *  -SPST Lever Microswitch                      (x1)
 *  -Simple Push Button                          (x2)
 *  -TIP120 Transistor                           (x1)
 *  -1N4004 Diode                                (x1)
 *  -1K Resistor                                 (x1)
 *  The two push buttons each control the flywheel and ramrod 
 *  part of the circuit. The flywheel uses the BLDC and the
 *  ramrod uses the geared motor. The BLDCs are controlled by
 *  the ESCs and the power to the geared motor is run through
 *  the TIP120 as a relay. The lever microswitch is attached 
 *  to the base in such a way that every time the crank 
 *  reaches the back spot its triggered, sending simple
 *  positional data to the Arduino.
 *  The code has been adapted from these 2 sources:
 *  https://dronesandrovs.wordpress.com/2012/11/24/how-to-control-a-brushless-motor-esc-with-arduino/
 *  http://www.instructables.com/id/Use-Arduino-with-TIP120-transistor-to-control-moto/
 *  The first helps calibrate ESCs,refer to it if you need to.
 *  Pins are as follows:
 *  3v3 - low power rail
 *  GND - ground
 *  VIN - 7.4V rail (acting as power input to Arduino)
 *                   DO NOT HAVE ANY OTHER POWER IN
 *   6 - Lever Microswitch, NO pin (if SPDT)
 *   7 - Flywheel switch
 *   8 - Trigger
 *  10 - ESC data pin
 *  12 - TIP120 Base
 */

#include <Servo.h>  //The BLDCs  can be treated as servos.

//Flywheel
Servo ESC;
int throttle = 179; //Speed value for the ESCs.
int ESCpin = 10;    //Both ESC data connected to same pin

//Ramrod
int TIP120 = 12; //Connected to Base pin

//Switches
int Trigger = 8;
int Flywheel = 7;
int PosiSwitch = 6;
 
void setup()
{
//Flywheel
ESC.attach(ESCpin);

//Ramrod
pinMode(TIP120, OUTPUT); 

//Switches
pinMode(Trigger, INPUT);
pinMode(Flywheel, INPUT);
pinMode(PosiSwitch, INPUT);
}
 
void loop()
{
int Fval = digitalRead(Flywheel);
while (Fval == HIGH){
  ESC.write(throttle);
  int Pval = digitalRead(PosiSwitch); //What's its position?
  int Tval = digitalRead(Trigger);
  
  if (Pval == HIGH && Tval == LOW){digitalWrite(TIP120, 0);}
  else digitalWrite(TIP120,255);
  Fval = digitalRead(Flywheel);
}
ESC.write(0);

}

Am I correct in thinking that you want to start the flywheel spinning and then push a dart into contact so that the flywheel throws the dart.

And you don't want a dart pushed when the flywheel is not spinning.

Assuming that is correct, what is not clear to me is whether pushing the button to start the flywheel should also cause a dart to be pushed a little later. Or whether there is one switch to start the flywheel and another that is pressed whenever you want to launch a dart.

Also, I am not clear about the purpose holding down the switch - is that so that many darts are pushed?

...R

Yes.
Yes.
Two switches. One controls the flywheel and one controls the ramrod. Ramrod uses a crank system to transfer angular motion into linear motion.
Pressing the trigger acts as a single shot. Holding it down acts as an automatic.

Hi, It sounds like you have got your hardware assembled.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project in jpg, png?

Thanks Tom.... :) PS I am worried about.

int TIP120 = 12; //Connected to Base pin

with no base resistor.

Here's the internals and a circuit diagram. Don't worry, there's a resistor and diode.

Circuit.png|1170x468

Here's the internals.

Internals.png|1269x696

SJMaybury:
Two switches. One controls the flywheel and one controls the ramrod. Ramrod uses a crank system to transfer angular motion into linear motion.
Pressing the trigger acts as a single shot. Holding it down acts as an automatic.

Which of the two switches is the trigger?

How often, and when, should darts be pushed in automatic mode?

…R

The switch/button called "Trigger" is the trigger. The frequency is controlled automatically by the fact that the motor outputs angular motion as is the nature of crank systems.

SJMaybury: The switch/button called "Trigger" is the trigger. The frequency is controlled automatically by the fact that the motor outputs angular motion as is the nature of crank systems.

You are losing me ...

What is the other button - you said there are two.

How is the angular motion detected? What angle causes the dart to be pushed?

Remember, you have the whole idea in your head and I only have the bits you tell me.

...R

The other button is for revving the flywheels, I think I mentioned that before plus in the code header. The angular motion is detected once every rotation by the PosiSwitch which tells the Arduino that the ramrod is completely retracted. Here, this might really help. Its the video on the original Falconer MK I. The inner workings are pretty similar, the control is improved though.

Hi, Looking at your fritzy picture, and your code, your switches pull high to 5V when you push them. You will need 10k resistors from those arduino inputs to gnd to make sure the inputs go to gnd when the switch is open.

You cannot leave an input open circuit if you are using it, it must be 5V or gnd, not floating like you have when the switch is open.

Are you really running the current from your LiPo through the protoboard to the ESC? If so, don't, the protoboard will not be able to carry the currents used by the ESC and its motor.

Also make sure the two red and blue power lines down the side of your protoboard are continuous, some boards have a break half way down their length.

Can you please post a copy of your circuit, in CAD or a picture of a [u]hand drawn circuit[/u] in jpg, png?

Fritzy pictures are not circuit diagrams, sorry.

Thanks.. Tom.. :)

10K resistors, got it. The ESCs run straight from the 7.4V LiPo. I was worried current would be an issue, the other option would be to take power from the ESC's circuit to run the Arduino through the VIN. Will do.

SJMaybury: The other button is for revving the flywheels, I think I mentioned that before plus in the code header. The angular motion is detected once every rotation by the PosiSwitch which tells the Arduino that the ramrod is completely retracted.

We are still on different wavelengths.

My understanding of the system now is like this (am I correct)

  • The user presses button A to start the machine.
  • Whenever he wants to fire a dart he presses button B
  • If he holds button B down a continuous stream of darts will be fired.
  • A dart will be pushed (loaded) once per revolution of the flywheel whenever the flywheel trips the microswitch.
  • And for added clarity, the microswitch is switch C.

And if this correct it is significantly different from the description in your Original Post on which my Reply #2 was based.

...R