Controlling PWM using push buttons

hi friends, First of all i am new to electronics. i am trying to generate PWM signal to control my VFD.
I have made a tinker cad simulation using Arduino UNO. I got the PWM signal and also can control the signal using the push buttons(Increse and decrease). Then, i tried to add one more button to stop and start the loop.i wrote a code but it is not working properly. I got stuck in this step, i have attached the screen shot of the simulation and the code.

can someone help me?

Note: I have attached both the code 1.before adding stop button 2.After adding stop button

Thanks in adance!!

2 button pwm 2.txt (550 Bytes)

pwm + stop.txt (718 Bytes)

Please post your code using code tags (The </> button)

int button1=12;
int button2=11;
int button3=4;
int buttonval1;
int buttonval2;
int buttonval3;

int pwmout=3;
int bright=0;
int dt=100;

void setup()
{
  pinMode(button1,INPUT_PULLUP);
  pinMode(button2,INPUT_PULLUP);
  pinMode(button3,INPUT_PULLUP);
  pinMode(pwmout,OUTPUT);
}

void loop()
{
  buttonval1=digitalRead(button1);
  buttonval2=digitalRead(button2);
  buttonval3=digitalRead(button3);
  
  delay(dt);
  
  if(buttonval1==LOW)
  {
    bright=bright+25;
  }
  if(buttonval2==LOW)
  {
    bright=bright-25;
  }
  if(buttonval3==LOW)
  {
    bright=0;
  }
  if(bright>255)
  {
    bright=255;
  }
  if(bright<0)
  {
    bright=0;
  }
  
  
  
  analogWrite(pwmout,bright);
  }

So which button is the Stop button? What does it do now? What EXACTLY is it supposed to do?

Steve

hi steve,

The push button which is connected to pin 4 is used to stop the motor. This code is working well with the increment and decrement of the motor speed. i want to stop the motor suddenly with a stop button that's y i used

if(buttonval3==LOW)
  {
    bright=0;
  }

could u please check my code.

Thanks for your response

That looks like it should do something, if the button is wired correctly with a pull-up resistor.

Note that it doesn't in any sense "stop and start the loop" it just sets the PWM to 0 so in your case LED should go off. To "start" it again you need to press the + button. Isn't that what happens? Or isn't that what you wanted?

Steve

Yes, that's what i want. But when i press 'stop' button it is not turning off. should i want to change any thing in my code?

I see you've changed the button pinModes to INPUT_PULLUP. How are the buttons wired, particularly the stop button? Do the other two buttons still work to increase/decrease brightness?

Are you still doing this all in Tinkercad rather than in the real world? If so it could be anything. Tinkercad is far from perfect.

Steve

Hi steve, you are right. i am confused with the push button concept. i have changed the input to INPUT_PULL and also changed the digital read to HIGH.(It gives output when it reads '1'). Now, it is working fine. Your replies helped me a lot. Thank you, very much.

int button1=12;
int button2=11;
int button3=4;
int button4=2;
int buttonval1;
int buttonval2;
int buttonval3;
int buttonval4;

int pwmout=3;
int bright=0;
int dt=100;

void setup()
{
  Serial.begin(9600);
  pinMode(button1,INPUT_PULLUP);
  pinMode(button2,INPUT_PULLUP);
  pinMode(button3,INPUT_PULLUP);
  pinMode(button4,INPUT_PULLUP);
  pinMode(pwmout,OUTPUT);
}

void loop()
{
  buttonval1=digitalRead(button1);
  buttonval2=digitalRead(button2);
  buttonval3=digitalRead(button3);
  buttonval4=digitalRead(button4);
  
  delay(dt);
  Serial.println(buttonval1);
  if(buttonval1==1)
  {
    bright=bright+25;
  }
  if(buttonval2==1)
  {
    bright=bright-25;
  }
  if(bright>255)
  {
    bright=255;
  }
  if(bright<0)
  {
    bright=0;
  }
 if(buttonval3==1)
  {
    bright=0;
 }
   if(buttonval4==1)
  {
    bright=25;
 }
  analogWrite(pwmout,bright);
  }
int button1=12;
int button2=11;
int button3=4;
int button4=2;
int buttonval1;
int buttonval2;
int buttonval3;
int buttonval4;

Next up, arrays :wink: