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);
}
}
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
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);
}
}
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?