Pushbuttons are not recognised

hi there., i am just a beginner at arduino. i was trying to run a motor in two directions after reading two pushbutton status. my code below however returns only the IDLE string on the serial monitor. The wirings are correct and the pushbutton isn't faulty. when pressed the multimeter reads 0V (when wired to ground). but the arduino seems to be in some kind of loop. any idea mates??

void loop()
{
  int up=4;
  int down=5;
  pinMode(up, INPUT);
  pinMode(down, INPUT);
digitalRead(up); 
digitalRead(down);

if(up==LOW)
{motor(1);}

else if(down==LOW)
{motor(2);}

else(Serial.println(IDLE");)
}

oh and the motor() function is correct too.. i replaced it with a simple led and it still didnt work

Hi and welcome,

is this your complete code? Please post the entire code incl. variable declarations, setup() etc. -> not only the loop() code. And: pinMode is no command for being used in a loop ... (this belongs to setup() ).

And: let us know something about hardware: - how are your buttons wired to the Arduino (do you use the internal pullup resistors or external ones?)? - what kind of motor is it (stepper, DC, brushless DC, ...) ? - what driver are you using for driving the motor? - did you already have the motor run in a simple, proven test sketch? - what voltage do you use for the motor / motor driver?

Only then we can assist you and point you to the issues.

Ok.. so here's the detailed code.. I have made some corrections too...

const int motor_1 = 13;
const int motor_2 = 12;
const int up=4;
const int down=5;

void setup() {

  pinMode(motor_1, OUTPUT);
  pinMode(motor_2, OUTPUT);
  pinMode(up, INPUT);
  pinMode(down, INPUT);

  Serial.begin(9600);
}
void loop()
{
  digitalRead(up); ;
  digitalRead(down);
     if(up==LOW)
      {motor(1);}
     else if(down==LOW)
      {motor(2);}
     else 
      {Serial.println("IDLE"); delay(50);}
}

  
void motor(int x)
{ 
  if(x==1)
  {
      digitalWrite(motor_1,LOW);
      digitalWrite(motor_2,HIGH);
      delay(3000);
  }
  else
    {
       digitalWrite(motor_1,HIGH);
       digitalWrite(motor_2,LOW);
       delay(3000);
    }
  
}

I am using l293d motor driver....

You can't do this:

  digitalRead(down);
     if(down==LOW)

You have to do something like this:

directionDown = digitalRead(downPin) ;
if (directionDown == LOW){
. . . 
}

I would suggest to check the syntax and understand what the 2 digitalRead() do (or actually don't do) at the begining of the loop

As a beginner to avoid any possible confusion, I would suggest you rename things that are pins to have to word PinNumber in the variable name and variables that hold the state of a pin (HIGH or LOW) to have the State word in the variable name

For example if instead of

const int up = 4;

the name was

const int upButtonPinNumber = 4;

then when later on in the code you do

     if(up==LOW)

That would become obvious it is definitely not what you meant to write if you were coding

     if(upButtonPinNumber == LOW)

Because it's totally obvious the PIN number can't be low or high, it's just the PIN number...

And if you had a variable that was upButtonState

upButtonState = digitalRead(upButtonPinNumber);
If (upButtonState == HIGH) {...

Then it would be very obvious that this feels much better and logical.

Don't worry about long variable names that doesn't make your code bigger when compiled, but it does make your code WAY more readable and maintainable.

Makes sense?

read the button sketch from basic examples in IDE... you will get the idea.

There are some open issues:

  1. Are we talking about one or two motors (motor1, motor2)?
  2. Still open: what kind of motor (DC I suppose)?
  3. Wiring1: Arduino-Driver-Motor-Power supply
  4. Wiring2: Buttons-Arduino (resistors?)

Otherwise we will be guessing around.

The first pb is the code

The first pb is the code

->> ??

rpt007: ->> ??

the OP has fundamental misunderstanding on how to use digitalRead. until he gets his head around the proper way of driving and reading pins status the rest does not matter much.

Once the code is clean, if it's non functional then you can indeed assess if connexions are weird (the OP stated "The wirings are correct")

ok, thanks for your explanation; then let's wait until he understands the digitalRead properly.

6v6gt:
You can’t do this:

  digitalRead(down);

if(down==LOW)




You have to do something like this:



directionDown = digitalRead(downPin) ;
if (directionDown == LOW){
. . .
}

i tried this and it seems to have solved the issue… seems like i have to learn a lot more than i thought… thanks guys…

i tried this and it seems to have solved the issue.. seems like i have to learn a lot more than i thought.. thanks guys..

Congratulations!

Yes, Arduino is (almost) like a piece of cake; at least you get all ingredients for making all sorts of delicious cakes, when you buy it. But you have to know how much you need from everything, the order of putting the ingredients together and then bake them together in variables declaration, libraries, setup() and loop().

Having understood everything you will be in the stars club of cooks 8)