5

In recent project, I'm working with light dimmer.

Let me explain with example of fan. In switch board, we have a fan switch which can able to ON/OFF our fan & FAN REGULATOR which vary speed of our fan. Now, when our switch ON then our fan start and then with help of regulator we can vary speed of fan. And when we switch OFF then our fan stop and we can't do anything means not vary fan speed. Same things I want in my light dimmer. One push button pressed once then it's just start my light dimmer and vise versa operation. And When push button pressed again then it's just stop my light dimmer.

Here is my full code.

int kPinPWM = 9; // This pin provides PWM values

int kPinSW1 = 6; // PUSH BUTTON // This pin used for increase PWM values
int kPinSW2 = 7; // PUSH BUTTON // This pin used for decrease PWM values

int kPinSw = 10; // PUSH BUTTON // This pin used for ON/OFF PWM pin means ON/OFF pin 9

int i = 0;  

int temp = 0; // Flag

void setup()
{
  pinMode(kPinPWM, OUTPUT);

  pinMode(kPinSW1, INPUT);
  pinMode(kPinSW2, INPUT);

  digitalWrite(kPinSW1, HIGH);
  digitalWrite(kPinSW2, HIGH);

  pinMode(kPinSw, INPUT);

  digitalWrite(kPinSw, HIGH);
}

void loop()
{
  if(digitalRead(kPinSw) == LOW && temp == 0) //If I pressed PUSH BUTTOPN(PIN 10) and temp = 0
  {
    temp = 1; // Change value to 1 // temp = 1

    digitalWrite(kPinPWM, HIGH); // Turn ON PWM pin (PIN 9)

    if(digitalRead(kPinSW1) == LOW) // Pressed PUSH BUTTON to increase PWM value
    {
      i++;

      if(i > 4)
      {
        i = 4;
      }

      while(digitalRead(kPinSW1) == LOW); //Until PUSH BUTTON pressed
    }

    if(digitalRead(kPinSW2) == LOW) // Pressed PUSH BUTTON to decrease PWM value
    {
      i--;

      if(i < 0)
      {
        i = 0;
      }

      while(digitalRead(kPinSW2) == LOW); //Until PUSH BUTTON pressed
    }

    switch (i)
    {
      case 0:
              analogWrite(kPinPWM, 0); // When Button pressed not pressed send 0 value
      break;

      case 1:
              analogWrite(kPinPWM, 64); // When Button pressed and i = 1 send 64 value
      break;

      case 2:
              analogWrite(kPinPWM, 128); // When Button pressed and i = 2 send 128 value
      break;

      case 3:
             analogWrite(kPinPWM, 192); // When Button pressed and i = 3 send 192 value
      break;

      case 4:
             analogWrite(kPinPWM, 255); // When Button pressed and i = 4 send 255 value
      break;
    }
  }

  if(digitalRead(kPinSw) == LOW && temp == 1) //If I pressed PUSH BUTTOPN(PIN 10) and temp = 1
  {
    temp = 0; // Change value to 0 // temp = 0

    digitalWrite(kPinPWM, LOW); Turn OFF PWM pin (PIN 9)
  }
}
  • Do you mean you want pins 9 and 10 to BOTH be able to dim and switch on/off a light? If so you just need to wrap you code in a function and take the pin number as a parameter. – Code Gorilla Jan 19 '17 at 13:01
  • Do you mean you want to use pin 10 as an input to control the PWM output, or do you want to use pin 10 as an output to control if the PWM signal gets to the light or not? – Majenko Jan 19 '17 at 13:12
  • 1
    Please explain what you want the three buttons to do. Also explain what the code is doing. What the code is not doing. Or what the code is doing wrong. Also, the first if statement inside the loop function seems very odd. It looks like you need to keep the button pressed for any of the other buttons or code to work at all. – Gerben Jan 19 '17 at 14:22
  • 1
    @matt Ok, let me explain some things. – Hasan Jan 19 '17 at 14:25
  • 1
    @Gerben Ok, understand your things. Let me clear the things. – Hasan Jan 19 '17 at 14:25
3

Finally I find out solution. Here, I detect when switch becomes pressed instead of button is pressed. So, for that I used StateChangeDetection. So while loop just removed from code. And I made function to perform dimmer and vise versa operation. k So, now I'm able to ON/OFF the PWM pin. And also vary PWM pin. Thanks everyone for your effort.

Here, it's my full code.

const int buttonPin1 = 2;          
const int buttonPin2 = 3;
const int buttonPin3 = 4;

const int pwmPin = 9;

const int onOffPin = 10;

int buttonState1 = 0;         
int lastButtonState1 = 0;

int buttonState2 = 0;         
int lastButtonState2 = 0;

int buttonState3 = 0;         
int lastButtonState3 = 0;

int buttonPushCounter = 0;

int onOffCounter = 0;

void setup()
{
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin1, INPUT);

   pinMode(pwmPin, OUTPUT);

   pinMode(onOffPin, OUTPUT);

   Serial.begin(9600);
}

void loop() 
{
   buttonState3 = digitalRead(buttonPin3);

   if(buttonState3 != lastButtonState3)
   {
     if(buttonState3 == HIGH)
     {
       onOffCounter++;
       Serial.println("Button 3 on");
       Serial.print("number of button 3 pushes:  ");
       Serial.println(onOffCounter);
     }
     else 
     {
       Serial.println("Button 3 off");
     }
     delay(50);
   }

   lastButtonState3 = buttonState3;

   if(onOffCounter % 2 == 0)
   {
     Serial.println("Switch is OFF");

     analogWrite(pwmPin, 0);
   }
   else
   {
      PWM_fun();

      Serial.println("Switch is ON");
   }
}

void PWM_fun()
{
  buttonState1 = digitalRead(buttonPin1);

  if (buttonState1 != lastButtonState1)
  {
    if (buttonState1 == HIGH)
    {
      buttonPushCounter++;
      Serial.println("Button 1 on");
      Serial.print("number of button 1 pushes:  ");
      Serial.println(buttonPushCounter);

      if(buttonPushCounter > 4)
      {
         buttonPushCounter = 4;
      }
    }
    else 
    {
      Serial.println("Button 1 off");
    }
    delay(50);
  }

  lastButtonState1 = buttonState1;

  buttonState2 = digitalRead(buttonPin2);

  if(buttonState2 != lastButtonState2)
  {
    if(buttonState2 == HIGH)
    {
      buttonPushCounter--;
      Serial.println("Button 2 on");
      Serial.print("number of button 2 pushes:  ");
      Serial.println(buttonPushCounter);

      if(buttonPushCounter < 0)
      {
         buttonPushCounter = 0;
      }
    }
    else 
    {
      Serial.println("Button 2 off");
    }
    delay(50);
  }

  lastButtonState2 = buttonState2;

  switch (buttonPushCounter)
  {
      case 0:
              analogWrite(pwmPin, 0);
      break;

      case 1:
              analogWrite(pwmPin, 64);
      break;

      case 2:
              analogWrite(pwmPin, 128);
      break;

      case 3:
              analogWrite(pwmPin, 192);
      break;

      case 4:
              analogWrite(pwmPin, 255);
      break;
  }
}
0

Thank you for the clarification.

Here's some code which should do that:

int kPinPWM = 9; // This pin provides PWM values

int kPinSW1 = 6; // PUSH BUTTON // This pin used for increase PWM values
int kPinSW2 = 7; // PUSH BUTTON // This pin used for decrease PWM values

int kPinSw = 10; // PUSH BUTTON // This pin used for ON/OFF PWM pin means ON/OFF pin 9

// Startup settings:
int level = 0;          // Lowest level
boolean on = false;     // Off

void setup()
{
  pinMode(kPinPWM, OUTPUT);

  pinMode(kPinSW1, INPUT);
  pinMode(kPinSW2, INPUT);
  pinMode(kPinSw, INPUT);

  digitalWrite(kPinSW1, HIGH);
  digitalWrite(kPinSW2, HIGH);
}

void loop()
{
    // Read each button in turn
    if(digitalRead(kPinSW1) == LOW) // Pressed PUSH BUTTON to increase PWM value
    {
       level++;

       if(level > 4)
       {
         level = 4;
       }

       while(digitalRead(kPinSW1) == LOW); //Until PUSH BUTTON released
    }

    if(digitalRead(kPinSW2) == LOW) // Pressed PUSH BUTTON to decrease PWM value
    {
       level--;

       if(level < 0)
       {
         level = 0;
       }

       while(digitalRead(kPinSW2) == LOW); //Until PUSH BUTTON pressed
    }

    if(digitalRead(kPinSw) == LOW ) // Pressed PUSH BUTTON to switch on/off
    {
        // If we're already on, switch off.
        if (on) on=false;
        // If we're already off, switch on.
        else on=true;

        while(digitalRead(kPinSW1) == LOW); // Until button is released
    }

    // Now set the PWM output
    if (on)
    {
        // We're on, so set the level
        switch (level)
        {
            case 0:
                // Maybe you don't want this "off" level..?
                analogWrite(kPinPWM, 0);
            break;

            case 1:
                analogWrite(kPinPWM, 64);
            break;

            case 2:
                analogWrite(kPinPWM, 128);
            break;

            case 3:
                analogWrite(kPinPWM, 192);
            break;

            case 4:
                analogWrite(kPinPWM, 255);
            break;

            default:
                // This should never happen but we should
                // handle it "just in case"
                analogWrite(kPinPWM, 255);
        }
    }
    else
    {
        // We're off, so stop the PWM
        analogWrite(kPinPWM, 0);
    }
}

We hold two variables: the level, and on/off state.

Buttons 6 and 7 affect the level, and button 10 affects the on/off state.

As with a fan, this code lets us change the "level" even when it's switched off. It has no immediate effect, but when you switch back on, the level will have changed.


Some things to say about the question in general:

  1. Try to explain more clearly exactly what you want to achieve. For example, what exactly do you want button 10 to do? Switch the PWM on? On to what value? I've chosen mid-way, but you could possibly want the highest value, or the lowest value, or the value it was last time you pressed button 10.
  2. There's no need to comment code so literally. We know literally what if(temp==0) does. What would be helpful is to give things proper names, and/or explain their meaning. What are you trying to achieve with temp? (It wasn't clear to me at all, so I removed it.)
  • Ok, Let me explain more. Let me update my question more clearly with brief description. I hope that's may be clear your all doubt. – Hasan Jan 20 '17 at 5:44
  • Thanks for the edits, but again we can see what the code does. Please try to explain, clearly and without lots of unnecessary detail, what you want it to do - the behaviour you want. I think you should be able to explain it in 5 lines of text. – Mark Smith Jan 20 '17 at 8:51
  • Ok, get your point. Let me explain with example of fan. In switch board, we have a fan switch which can able to ON/OFF our fan & FAN REGULATOR which vary speed of our fan. Now, when our switch ON then our fan start and then with help of regulator we can vary speed of fan. And when we switch OFF then our fan stop and we can't do anything means not vary fan speed. Same things I want in my light dimmer. One push button pressed once then it's just start my light dimmer and vise versa operation. And When push button pressed again then it's just stop my light dimmer. I hope you understand. – Hasan Jan 20 '17 at 9:06
  • I tried out your code. But it's not working as I want. Thank you again for your effort. I find out the solution. I will upload code and explanation. – Hasan Jan 20 '17 at 12:37
  • Feel free to explain how it differs from what you want. – Mark Smith Jan 20 '17 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.