byte buttonPin = x; // 2, for example
byte fanPin = y; // 3, for example
byte fanState = 0; // status of fan
void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
// button is pressed, continue
if ( fanState == 0){ // fan is off, button is pressed
digitalWrite (fanPan, HIGH); // turn MOSFET on
fanState = 1;
}
if (fanState == 1){ // fan is off, button is pressed
digitalWrite (fanPan, HIGH); // turn MOSFET on
fanState = 0;
}
delay (100); // give the button a chance to settle
}
byte fanPin = y; // 3, for example
byte fanState = 0; // status of fan
void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
// button is pressed, continue
if ( fanState == 0){ // fan is off, button is pressed
digitalWrite (fanPan, HIGH); // turn MOSFET on
fanState = 1;
}
if (fanState == 1){ // fan is off, button is pressed
digitalWrite (fanPan, HIGH); // turn MOSFET on
fanState = 0;
}
delay (100); // give the button a chance to settle
}
Didn't try compiling this
I tried this code (changes some typos ("fanPan") and added a last bracket) and the fan spinning but the button does nothing.
Here is my code:
byte buttonPin = 5; // 2, for example
byte fanPin = 4; // 3, for example
byte fanState = 0; // status of fan
void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
// button is pressed, continue
if ( fanState == 0){ // fan is off, button is pressed
digitalWrite (fanPin, HIGH); // turn MOSFET on
fanState = 1;
}
if (fanState == 1){ // fan is off, button is pressed
digitalWrite (fanPin, HIGH); // turn MOSFET on
fanState = 0;
}
delay (100); // give the button a chance to settle
}
}
To help the next person, maybe you should post your final code and a schematic.
Fwiw, here's how I coded it:
// https://forum.arduino.cc/index.php?topic=677648
/*
BASED ON State change detection (edge detection) changed for INPUT PULLUP
(button wired from pin to ground)
https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/
// this constant won't change:
const int button = 4;
// Variables will change:
bool buttonState; // current state of the button
bool lastButtonState; // previous state of the button
bool fanState = false;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println("https://forum.arduino.cc/index.php?topic=677648");
pinMode(button, INPUT_PULLUP);
//initialize button states
buttonState = digitalRead(button);
lastButtonState = buttonState;
//turn builtin led off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println(" ");
Serial.print("fanState is starting as "); Serial.println(fanState);
Serial.println("setup() done... press the button");
Serial.println(" ");
}//setup
void loop()
{
// read the button:
buttonState = digitalRead(button);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
{
if (buttonState == LOW) //... and if it's now low, that's a press
{
fanState = !fanState;
Serial.print("New press, fanState is now "); Serial.println(fanState);
}// change to low
// Delay a little bit to avoid bouncing
delay(50);
}//change
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
//proof:
digitalWrite(LED_BUILTIN, fanState);
} //loop
Gives this output:
fanState is starting as 0
setup() done... press the button
New press, fanState is now 1
New press, fanState is now 0
New press, fanState is now 1
New press, fanState is now 0
Of course. i can post my code later today. But i does not have any schematic. Is there any good online tools for fast creating of schematic for beginners?