1
\$\begingroup\$

(First of all: Sorry for the Title.. I really don't know how to describe my problem in one Sentence)

I'm an arduino beginner. I build a two button control for an electronic motor.

schematic

simulate this circuit – Schematic created using CircuitLab

I wrote a Code to set the Speed with the both buttons (SW4 and SW3) and to display the speed in the serial monitor on my laptop.

It's working perfectly with this code:

void setSpeedButton()
    {

     while(1){ 
      buttonLeftState = digitalRead(buttonLeft);
      buttonRightState = digitalRead(buttonRight);

      if(buttonLeftState == HIGH)
      {
        speed = speed - vChange;
        delay(200);
        if(speed < 0){
          speed = 0;
        }
      }
      if(buttonRightState == HIGH)
      {
        speed = speed + vChange;
        delay(200);
        if(speed > 255){
          speed = 255;
        }
      }
      analogWrite(motor, speed); 

    vDisplay();

  }
 }

vChange is an integer with the value 9, the rest should be clear.

vDisplay() is a void with this code:

void vDisplay()
{
  Serial.print("Speed: ");
  Serial.println(speed);
}

Okay.. So far so good..But now:

if I change the code (first block) and put vDisplay() in both if-query (to display the speed only if it was changed) its like my board is going crazy and randomly changing the speed every few milliseconds and displaying it on the serial monitor...

I really find no solution for this problem. what is causing this problem?

If I forgot to add some informations, please let me know in the comments below. Thanks everyone for helping or commenting on this!

\$\endgroup\$
  • 2
    \$\begingroup\$ R1 and R3 should be on the Arduino side of the switches, to pull the inputs high when the button is not pressed. Those resistors are useless where you show them. \$\endgroup\$ – Peter Bennett Feb 19 '14 at 19:07
1
\$\begingroup\$

With the current configuration, your inputs are floating when the buttons aren't pressed. Thus, if no button is pressed, the input could be anything. You have no control. A better solution would be as follows:

schematic

simulate this circuit – Schematic created using CircuitLab

Now, with no button pressed, the input is pulled up to 5V. With a button press, the input is pulled down to ground.

\$\endgroup\$
  • \$\begingroup\$ okay thank you.. i will try this out.. but if the inputs of the unpressed buttons are floating, then why doens't it affect the output on the serial monitor with the code as i had it befor? \$\endgroup\$ – progger Feb 19 '14 at 19:21

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.