Beginner's thumb question

Hi all,

I'm struggling in the very beginning of my first project.

The problem starts with a simple thing:
I want to read in 2 buttons and want to set a value depending on the pressed button.
The variable 'Target' shall get an integer value.

No matter what I try, 'Target' doesn't get the value.
Also the serial monitor reports that both buttons are pressed.

All the buttons are working properly, tested step by step with the blink example.

What am I doing so entirely wrong?
See code below.

Thank you,
Gregor

boolean Moving = false;

int Position_0 = 7;
int Position_1_up = 8;
int Position_1_down = 6;
int Position_2 = 5;

int Order_0 = 3;
int Order_1 = 2;
int Order_2 = 4;

int Door = 9;

int Motor_up = 11;
int Motor_down = 12;

int Light = 13;

int Target = 0;


void setup() {

  Serial.begin(9600);

  pinMode(Position_0, INPUT);
  pinMode(Position_1_up, INPUT);
  pinMode(Position_1_down, INPUT);
  pinMode(Position_2, INPUT);

  pinMode(Order_0, INPUT);
  pinMode(Order_1, INPUT);
  pinMode(Order_2, INPUT);

  pinMode(Door, INPUT);

  pinMode(Motor_up, OUTPUT);
  pinMode(Motor_down, OUTPUT);

  pinMode(Light, OUTPUT);

  digitalWrite(Motor_up, HIGH);
  digitalWrite(Motor_down, HIGH);
  digitalWrite(Light, HIGH);

}

void loop() {

  {
    if (digitalRead(Order_0) == HIGH)
      Target == 156;
      Serial.println("0 pressed");
  }


  {
    if (digitalRead(Order_1) == HIGH)
      Target == 789;
            Serial.println("1 pressed");
  }

  Serial.println(Target);

}

How have you wired the buttons?

How have you wired the buttons?

The buttons are wired to a resistor as recommended in several places to avoid noise.
As said, I tested the wiring and the general function with the blink example.
Works exactly as expected.

Same for the outputs, they go to a relay board and act as expected.

It must be something very stupid I do in the code...

Gregor77:
It must be something very stupid I do in the code...

Well for starters you have your open brackets ("{") in the wrong place for each if statement. The code should look more like:

    if (digitalRead(Order_0) == HIGH)
  {
      Target == 156;
      Serial.println("0 pressed");
  }

I'm also worried about having a pin for "up" and "down" but that should get you closer.

Hope this helps,

Brad
KF7FER

The buttons are wired to a resistor as recommended in several places to avoid noise.

Where does the resistor go? Have you enabled input pullup resistors, so that you actually can read a "high" level?

Hi Brad,

OMG, how stupid was that...
OK, now I see.

And I see my next error: == is nonsense in this case, it must read =.

Thank you very much for giving me the right kick!

Gregor

void loop() {

  
    if (digitalRead(Order_0) == HIGH)
  {
      Target = 156;
      Serial.println("0 pressed");
  }


  
    if (digitalRead(Order_1) == HIGH)
  {
      Target = 789;
            Serial.println("1 pressed");
  }

  Serial.println(Target);

}

Get rid of any resistors.
Wire any buttons, switches, etc to connect pin to Gnd when button/switch is closed.

Change these to use INPUT_PULLUP:

pinMode(Position_0, INPUT);
pinMode(Position_1_up, INPUT);
pinMode(Position_1_down, INPUT);
pinMode(Position_2, INPUT);

pinMode(Order_0, INPUT);
pinMode(Order_1, INPUT);
pinMode(Order_2, INPUT);

pinMode(Door, INPUT);

and change your test accordingly:

  {
    if (digitalRead(Order_0) == LOW)
      Target == 156;                             << these only need 1 =
      Serial.println("0 pressed");
  }


  {
    if (digitalRead(Order_1) == LOW)
      Target == 789;                             << these only need 1 =
            Serial.println("1 pressed");
  }

jremington:

The buttons are wired to a resistor as recommended in several places to avoid noise.

Where does the resistor go? Have you enabled input pullup resistors, so that you actually can read a "high" level?

Yes, the resistor is from GND to the pin, and 5V go to the resistor at the pin side.
So I can read HIGH, and it works.

And my code now as well, Brad gave me the right kick.
All is good.

Thank you.

CrossRoads:
Get rid of any resistors.
Wire any buttons, switches, etc to connect pin to Gnd when button/switch is closed.

As I'm uninitiated, I'm glad for any tip.
Q: What is better with that wiring than with the resistors?

      Target == 156;                             << these only need 1 =

Yes, I found that too.

As said, it's working now as expected.

Thank you.