While i was searching code for control my fan's speed with PWM i found this code. It is working partically but not working properly but i couldn't find what is wrong. I want that when i push any button that predefined fan speed will continously will work with that speed.
#define PIN_MEMBRAN_RIGHT 13
#define PIN_MEMBRAN_UP 12
#define PIN_MEMBRAN_DOWN 11
#define PIN_MEMBRAN_SELECT 10
#define PIN_MEMBRAN_LEFT 9
#define PIN_MEMBRAN_START 8
uint8_t membran_pin_list[] = {
PIN_MEMBRAN_UP,
PIN_MEMBRAN_DOWN,
PIN_MEMBRAN_RIGHT,
PIN_MEMBRAN_LEFT,
PIN_MEMBRAN_SELECT,
PIN_MEMBRAN_START
};
#define PIN_FAN 6
int PushButtonInput_1 = 13; // Push button - set PWM Fan speed %25
int PushButtonInput_2 = 12; // Push button - set PWM Fan speed %50
int PushButtonInput_3 = 11; // Push button - set PWM Fan speed %75
int PushButtonInput_4 = 9; // Push button - set PWM Fan speed %100
void setup() {
pinMode(PIN_FAN, OUTPUT);
digitalWrite(PIN_FAN, HIGH);
pinMode(PushButtonInput_1,INPUT_PULLUP);
pinMode(PushButtonInput_2,INPUT_PULLUP);
pinMode(PushButtonInput_3,INPUT_PULLUP);
pinMode(PushButtonInput_4,INPUT_PULLUP);
}
void loop() {
//-----------------25%----------------------
if(digitalRead(PushButtonInput_1)==HIGH){
analogWrite(PIN_FAN, 64);
//Serial.print(PushButtonInput_1);
}
//----------------50%---------------------
if(digitalRead(PushButtonInput_2)==HIGH){
analogWrite(PIN_FAN, 128);
}
//------------------75%-----------------------
if(digitalRead(PushButtonInput_3)==HIGH) {
analogWrite(PIN_FAN, 192);
}
//---------------------------------------------
//------------------100%-----------------------
if(digitalRead(PushButtonInput_4)==HIGH){
analogWrite(PIN_FAN, 255);
}
//---------------------------------------------
}