Cyclic Positional Trigger Mechanism

Pretty much correct.
Sorry, that was my fault, I made the mistake of referring to the crank as a flywheel, for some reason I thought it would be simpler to suggest a simple rotation, my bad.
Yes, sorry again for the confusion.

Anyway, here's the code at the moment, it seems to work decently well except while Trigger is LOW and PosiSwitch is HIGH (i.e. when the motor should be off) it seems to constantly twitch.

/*********************  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)
 *  -10K Resistor                                (x2)
 *  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 - ESC processor power (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 through resistor
int motorState;

//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);

//Debug
Serial.begin(9600);
}
 
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){
    motorState = LOW;
    digitalWrite(TIP120, motorState);
    }
  else {
    motorState = HIGH;
    digitalWrite(TIP120, motorState);
    }
  Fval = digitalRead(Flywheel);

  Serial.print("Trigger:");
  Serial.print(Tval);
  Serial.print("  ");
  Serial.print("Position:");
  Serial.print(Pval);
  Serial.print("  ");
  Serial.print("motor:");
  Serial. print(motorState);
  Serial.print("  |");
}
ESC.write(0);

}

You don't have a test for Trigger is LOW and PosiSwitch is HIGH.
That condition is NOT the ELSE for if (Pval == HIGH && Tval == LOW){

I think your logic would be much easier to follow if you use nested IFs rather than compound IFs. For example

if (Pval == HIGH) {
      if (val == LOW){
         motorState = LOW;
          digitalWrite(TIP120, motorState);
       }
  }
  else {
    motorState = HIGH;
    digitalWrite(TIP120, motorState);
  }

I realize this may not be what you want - but it should give the idea.

...R

By Jove that works! If I just put a delay in the Trigger "if" then I can make sure it runs until its out of the way of the PosiSwitch. Although that could be a bit problematic if the trigger is pulled shortly before it reaches the PosiSwitch... so maybe I should just leave it up to the user to hold the trigger for long enough.

Anyway, thanks!!! Long and convoluted as this has been (sorry), that's been a great help, I'll credit your help when I upload the code :slight_smile:

SJMaybury:
By Jove that works! If I just put a delay in the Trigger "if" then I can make sure it runs until its out of the way of the PosiSwitch. Although that could be a bit problematic if the trigger is pulled shortly before it reaches the PosiSwitch... so maybe I should just leave it up to the user to hold the trigger for long enough.

Anyway, thanks!!! Long and convoluted as this has been (sorry), that's been a great help, I'll credit your help when I upload the code :slight_smile:

You did not get the idea of using the delay() function from me.

...R