Led mosfet 2 switches one works?

he guys so i have a mosfet attached to a led and then to my arduino nano, the first switch "D2" works fine the led its off then when the push switch is pushed on the led turns on now if i were take the switch out of D2 and put into D7 the led is on a bit dimmer and if i then press the switch it turns off? whats wrong with my code here???

const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 7;     // the number of the pushbutton pin

const int ledPin =  12;      // the number of the LED pin





// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;  
  
void setup() {
  
                   // Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

 // pinMode(ledPin, OUTPUT);

  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  pinMode(buttonPin2, INPUT);
  
}

void loop() {
 //Serial.println(buttonState); 
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
                    buttonState = digitalRead(buttonPin2);
if (buttonState2 == HIGH) {  
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    
  }

  
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    
 // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

  }

  

}

buttonState2 is never updated ???

its the same as "buttonstate"

what do i have to do? lol sorry

You have to make buttonState2 equal to something, right now you make buttonState2 = 0 when you created the variable but then never change it.

 // variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;

Maybe you should put this somewhere :wink:

 buttonState2 = digitalRead(buttonPin2);

And remember the above line needs to be executed/updated now and then.

ok so i have the buttonState = digitalRead(buttonPin);
under Void loop as i want to to update

i forgot to put buttonState2 = digitalRead(buttonPin2); in XD
but im still getting the same result, the led is still on and if i press the switch it flickers?

but the whole first "buttonState" example works fine

Show us your current sketch.

sorry about colors etc XD but here is how i have it set up currently i know im a paint pro XD lol

hmm this "img]Imgur: The magic of the Internet[/img" with brackets is on the forum but its not showing so here is the link:

const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 7;     // the number of the pushbutton pin

const int ledPin =  12;      // the number of the LED pin





// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
 
void setup() {
 
                  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

 // pinMode(ledPin, OUTPUT);

 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  pinMode(buttonPin2, INPUT);
 
}

void loop() {
 //Serial.println(buttonState);
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH) { 
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
   
  }

 
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
   
 // turn LED on:
 delay(1000);
    digitalWrite(ledPin, HIGH);
    
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

  }

 

}

ok so it turns out i needed to put the 3rd contact on the swtich to vin? and now it works but its dimmer that pin7?

OP's image:

What MOSFET are you using?

You do know Simple LEDs need a series dropping resistor.
What LEDs are you using?

Do you have two switches or one? :o

im using IRFB4110PBF N Channel Hexfet High Speed Power MOSFET

and its for a bigger project, basicly i want to do this:

http://airsofttech.dk/Guides.cshtml?Page=BurstModeMosfet

i have resisters etc,
atm is a UV Led and i did not know that no lol

but with 2 swtiches i followed the guide but could not get it to work lol

i do have 2 switches but im just moving the jumper over for now

IRFB4110 is a poor MOSFET for the Arduino to control directly.

You need a LOGIC LEVEL MOSFET

This schematic might be what's needed, Note the MOSFET is a LOGIC level transistor.

BTW
Use:

pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);

EDIT

const byte buttonPin1 = 2;     // the number of the pushbutton pin
const byte buttonPin2 = 7;     // the number of the pushbutton pin

const byte ledPin     =  12;   // the number of the LED pin

//variables will change:
byte lastButton1State  = 0;    // variable for reading the pushbutton status
byte lastButton2State  = 0;

//timing stuff
unsigned long scanMillis;

//***********************************************************************************
void setup()
{
  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

} //END of setup()


//***********************************************************************************
void loop()
{
  //**********************************
  //Time to scan the switches?
  if (millis() - scanMillis >= 50)
  {
    //restart the timer
    scanMillis - millis();
    scanSwitches();
  }

  //**********************************

} //END of loop()


//***********************************************************************************
void scanSwitches()
{
  byte state;

  //**********************************
  state = digitalRead(buttonPin1);
  if (lastButton1State != state)
  {
    lastButton1State = state;
    if (state == LOW)
    {
      //LED on
      digitalWrite(ledPin, HIGH);
    }

    else
    {
      digitalWrite(ledPin, LOW);
    }

  } //END of   if (lastButton1State != state)

  //**********************************
  state = digitalRead(buttonPin2);
  if (lastButton2State != state)
  {
    lastButton2State = state;
    if (state == LOW)
    {
      //LED on
      digitalWrite(ledPin, HIGH);
    }

    else
    {
      digitalWrite(ledPin, LOW);
    }

  } //END of  if (lastButton2State != state)

} //END of  scanSwitches()

//***********************************************************************************

bill thanks for your help so far! where would i go about installing a 11.1v power supply with a motor instead of a led now? i can do the power to the ardueo with resisters a step down thing,

but how would i attach a motor and the switches to that motor?

im guessing the 11.1v + to motor +, then the grn to?

perfect thank you!

soo i got all the parts and put them together and its still not working D: not getting anything from the motor now

Show us your wiring.

Hi,
Do you have a DMM?

Tom... :slight_smile: