Mosfet Help with Airsoft Trigger Unit

So I was following a guide for an airsoft mosfet that I am making, and I finished building it but now the Arduino gets power but I guess it can't read the trigger. I know the trigger works, but for some reason my arduino can't read it. Can anyone help me? I am using this guide:

and here are some pictures of my project:



And here's the code I'm using.

// AirsoftTech.dk

//////// WIRE PIN AN COMPONENT LAYOUT /////////
int MOSFET_PIN = 5;         // The Digital pin that attaches to the MOSFET gate, to turn it on and off.
int TRIGGER_PIN = A0;       // The analog pin that attaches to the trigger pin

float R1 = 100; // 100Kohm  // The Voltage devider resistor R1 
float R2 = 10;  // 100Kohm  // The Voltage devider resistor R2

//////// CONFIG VALUES /////////
int Max_ON_Time = 200;      // The time in MS for a full burst cycle...

//////// INTERNAL VALUES /////////
int TrigerStatus = LOW;     // The state of the trigger LOW => Not pressed, HIGH => Pressed
int TriggerReadValue = 0;   // The value read from the analog trigger pin. => 0-1024
int CurrentSleepTime = 0;   // The ammount of time the mosfet has been on.

// The setup routine runs once when you press reset.
void setup() {                
  // Initialize the digital pin as an output.
  pinMode(13, OUTPUT);              // LED pin
  pinMode(MOSFET_PIN, OUTPUT);      // Set the Mosfet pin as an output so that we can send power to the mosfet.
  digitalWrite(MOSFET_PIN, LOW);    // Make sure we start with power OFF!
  digitalWrite(13, LOW);    // Make sure we start with power OFF!
}

// The loop routine runs over and over again forever.
void loop() {
  ReadTrigger();                                   // Update the trigger state
  if(TrigerStatus == HIGH)                         // If the trigger is pushed.
  {
    SetMosfet(HIGH);                               // First turn the Mosfet On
    while (TrigerStatus == HIGH) 
    {
      if(CurrentSleepTime < Max_ON_Time){          // If we have not completed a cycle
        CurrentSleepTime = CurrentSleepTime + 1;   // Leave the mosfet on, and increase time counter.
      } else {
        SetMosfet(LOW);                            // If a full cycle has gone, turn the Monfet Off.
      }
      delay(1);                                    // Sleep for one milisecond
      ReadTrigger();                               // Update the trigger status (We stay in the loop untill the trigger is released)
    }
    SetMosfet(LOW);                                // Make sure the Mosfet is off when the trigger is released
    CurrentSleepTime = 0;                          // Reset time counter when the trigger is released.
  }
}

// This function updates the trigger status when it's called.
void ReadTrigger()
{
  int TriggerReadValue = analogRead(TRIGGER_PIN);
    
  TrigerStatus = LOW;
  if(TriggerReadValue > 20) {
      TrigerStatus = HIGH;
  }
}

// This function set the Mosfet state 
void SetMosfet(int val)
{
  digitalWrite(13, val);            // turn the LED on / off to indicate what the mosfet should be dooing
  digitalWrite(MOSFET_PIN, val);    // turn the Monfet on / off
}

I'm kind of pulling my hair out at this point and would appreciate any help I can get. Thanks!

Where is the function:

SetMosfet()

?

It's defined at the very bottom and used around the middle. It just controls the onboard LED so I know if the trigger's being sensed or not.

OIC :blush:

I guess there's a reason for an analog trigger but I'm not into AirSoft so... :slightly_smiling_face:

Use the serial monitor for debugging.

See which value is being read by the analog pin with the trigger activated and without the trigger activated.

// AirsoftTech.dk

//////// WIRE PIN AN COMPONENT LAYOUT /////////
int MOSFET_PIN = 5;         // The Digital pin that attaches to the MOSFET gate, to turn it on and off.
int TRIGGER_PIN = A0;       // The analog pin that attaches to the trigger pin

float R1 = 100; // 100Kohm  // The Voltage devider resistor R1 
float R2 = 10;  // 100Kohm  // The Voltage devider resistor R2

//////// CONFIG VALUES /////////
int Max_ON_Time = 200;      // The time in MS for a full burst cycle...

//////// INTERNAL VALUES /////////
int TrigerStatus = LOW;     // The state of the trigger LOW => Not pressed, HIGH => Pressed
int TriggerReadValue = 0;   // The value read from the analog trigger pin. => 0-1024
int CurrentSleepTime = 0;   // The ammount of time the mosfet has been on.

// The setup routine runs once when you press reset.
void setup() {                
  // Initialize the digital pin as an output.
  Serial.begin(115200);
  pinMode(13, OUTPUT);              // LED pin
  pinMode(MOSFET_PIN, OUTPUT);      // Set the Mosfet pin as an output so that we can send power to the mosfet.
  digitalWrite(MOSFET_PIN, LOW);    // Make sure we start with power OFF!
  digitalWrite(13, LOW);    // Make sure we start with power OFF!
}

// The loop routine runs over and over again forever.
void loop() {
  ReadTrigger();                                   // Update the trigger state
  if(TrigerStatus == HIGH)                         // If the trigger is pushed.
  {
    SetMosfet(HIGH);                               // First turn the Mosfet On
    while (TrigerStatus == HIGH) 
    {
      if(CurrentSleepTime < Max_ON_Time){          // If we have not completed a cycle
        CurrentSleepTime = CurrentSleepTime + 1;   // Leave the mosfet on, and increase time counter.
      } else {
        SetMosfet(LOW);                            // If a full cycle has gone, turn the Monfet Off.
      }
      delay(1);                                    // Sleep for one milisecond
      ReadTrigger();                               // Update the trigger status (We stay in the loop untill the trigger is released)
    }
    SetMosfet(LOW);                                // Make sure the Mosfet is off when the trigger is released
    CurrentSleepTime = 0;                          // Reset time counter when the trigger is released.
  }
}

// This function updates the trigger status when it's called.
void ReadTrigger()
{
  int TriggerReadValue = analogRead(TRIGGER_PIN);
  Serial.println(TriggerReadValue);
    
  TrigerStatus = LOW;
  if(TriggerReadValue > 20) {
      TrigerStatus = HIGH;
  }
}

// This function set the Mosfet state 
void SetMosfet(int val)
{
  digitalWrite(13, val);            // turn the LED on / off to indicate what the mosfet should be dooing
  digitalWrite(MOSFET_PIN, val);    // turn the Monfet on / off
}

Getting a lot of zeros, how would I go about fixing this? I have a voltmeter btw so I can use that if I have to...
Edit: It looks like when using the voltmeter, I have 11.3 volts going through when the trigger isn't pulled, and 0 when it's not. However, after it gets past the 10k resistor and gets to the pin itself, it reads at 11.4 when not pulled and drops to 11.2 when pulled. I think it's an issue with the soldering/wiring but I have no idea what...
Here's what I was trying to make:


image

Using a DVM, check the voltage at the indicated location by activating the trigger and without activating the trigger.

The drawing on the left is different from the drawing on the right.
See the arrows in red.

It reads 0 volts, so somehow they're connected. I don't really get how that's possible though... I don't really know what I'm doing wrong

I think its from the grey point right below the second red arrow that's connected to d5 though, I think the diagram on the right is correct and the one on the left is just an overview

It might be best just to completely resolder and rewire the trigger to a normal 5v connection instead of using resistors and stuff, I might try that later unless you can find out what I did wrong

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.