Hello!
Im working on a micro controlled mosfet for my airsoft gun, the idea is to have different shooting modes like.. Single, burst and full auto.
My problem is i can't figure out what code i should use to switch between this modes... i have tried if-statements but doesnt work that good..
when im changing to burst mode and presses the trigger button the motor start spins like hell.. but single works just fine
so if any one could give me and example of working code to change between different modes i would appreciate it very much!
const int TriggerPin = 7; //Trigger switch attached to digital pin 7
const int ButtonPin = 4; // Change Modes
const int ledPin = 13; //onboard LED on pin 13
const int MotorPin = 3; //Motor drive pin (on MOSFET) connected to PWM pin at pin 3
const int MotorSpeed = 255; //PWM value for motor, max 255
const int SelectorPin = 8; //Selector Pin
const float CurveThresh = -4; //negative slope detection threshold
const int IRD = 110; //In Rush Delay in milliseconds to deal with in-rush motor startup current
const int Debounce = 50; //Standard Debounce delay
const float SpringSense = 4; //spring compression slope detection threshold
int switchPin = 2; // switch is connected to pin 2
int led1 = 12;
int led2 = 11;
int led3 = 10;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int PowerCurve[7]; //initialize the power curve array size 8
float CurvePoint; //CurvePoint will be compared to CurveThresh to determine if the spring is in free-fall
int fireTime = 0;
int TriggerState = 0; // Trigger button state
int ModeState = 0;
int FireMode = 0; //Firing mode of the gearbox. 0 is always safe, 1 is always singleshot
volatile int IMain = 0; // Direct ADC reading from current sensor
int BurstSetting = 3; //The number of rounds in a burst
void setup()
{
Serial.begin(115200); //High output serial for testing
pinMode(MotorPin, OUTPUT);
analogWrite(MotorPin, 0); //make sure the motorpin is off
pinMode(TriggerPin, INPUT);
pinMode(ButtonPin, INPUT);
digitalWrite(2, HIGH); //Turn on the internal pull-up on digital 2
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin);
delay(10);
val2 = digitalRead(switchPin);
if (val == val2) {
if (val != buttonState) {
if (val == LOW) {
if (FireMode == 0) {
FireMode = 1;
} else {
if (FireMode == 1) {
FireMode = 2;
} else {
if (FireMode == 2) {
FireMode = 3;
} else {
if (FireMode == 3)
{
FireMode = 0;
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the FireMode indicates
if (FireMode == 0)
{ // all-off
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
analogWrite(MotorPin, 0);
}
if (FireMode == 1)
{ // Single
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
TriggerState = digitalRead(TriggerPin);
if (TriggerState == HIGH)
{
SINGLESHOT();
}
else
{
analogWrite(MotorPin, 0);
}
}
if (FireMode == 2)
{ // Burst
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
TriggerState = digitalRead(TriggerPin);
if (TriggerState == HIGH)
{
BURST(BurstSetting);
}
else
{
analogWrite(MotorPin, 0);
}
}
if (FireMode == 3)
{ // full auto
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(100);
TriggerState = digitalRead(TriggerPin);
if (TriggerState == HIGH)
{
FULLAUTO();
}
else
{
analogWrite(MotorPin, 0);
}
}
}