Go Down

Topic: Why button is not connected? Arduino Uno R3 (Read 119 times) previous topic - next topic

aceimnors

Hi, it's my first post here so I am sorry if I miss some important information - I know very little about Arduino.

I've got a problem with connecting a button to my Arduino. When I click it in console should appear a message, but doesn't matter in how many ways I tried connecting it, it still doesn't work... 
I hope it's a stupid mistake somewhere, but I can't see it.

This is how it looks like (I use tinkercad online to make this circuit):

xxx
And the code is here:
Code: [Select]
void setup()
{
  pinMode (buttonLeft, INPUT);  //buttonLeft=2
  pinMode (buttonRight, INPUT); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
   stateLeft=digitalRead(buttonLeft);
   if (stateLeft==HIGH){
       Serial.print("X"); //why it doesn't print X?
   }
 
  digitalWrite(left, HIGH);
  delay(500); //delay 500ms
  digitalWrite(left, LOW);
  delay(500); //delay 500ms
  left--;
  if(left<8)
    left=constLeft;
}


Leds are HIGH one after another when I run the simulation, but I want to press the button to have control when to on/off this simulation. 
What is wrong and why I can't even detect pushing the button? 

Thanks for all help...

Paul__B

#1
Feb 23, 2021, 10:49 am Last Edit: Feb 23, 2021, 10:50 am by Paul__B
Connecting buttons to ground is correct.  You must set all button inputs in pinMode as INPUT_PULLUP.

aceimnors

Thank you! I also've added  "Serial.begin(9600);"   to my setup().  

Now in console it prints "X" periodically, even when I didn't push the button.
What should I do so it will print X in console only after I push for the first time the button?

Robin2

Those little momentary contact switches may need to be rotated 90°.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

#4
Feb 23, 2021, 01:08 pm Last Edit: Feb 23, 2021, 01:13 pm by TomGeorge
Hi,
Welcome to the forum.
Unfortunately your code is not complete, it will not compile.
Can you post it again in a new post, the missing part of your code is the bit before the void setup().

Do you have a DMM?
Can you post a picture of your project so we can see your component layout.

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
Can you try this code, the IDE monitor should tell you the states of you buttons as you press them.
Code: [Select]

int buttonLeftPin = 2;
int buttonRightPin = 3;
bool stateLeft;
bool stateRight;


void setup()
{
  Serial.begin(9600);
  pinMode (buttonLeftPin, INPUT_PULLUP);  //buttonLeft=2
  pinMode (buttonRightPin, INPUT_PULLUP); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}


void loop()
{
  stateLeft = digitalRead(buttonLeftPin);
  stateRight = digitalRead(buttonRightPin);
  Serial.print("Left Button = ");
  Serial.print(stateLeft);
  Serial.print("    Right Button = ");
  Serial.println(stateRight);
  delay(500);
}



Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

aceimnors

Hi Tom, thank you so much for your response!

I tried your code with my circuit but it seems to work quite the opposite way...
After I compile&run this circuit automatically Left and Right button is set to 1 and starts printing it in console the whole time, but when I push for example LeftButton, the RightButton is set to 0 and prints it. The same happens when I push RightButton, then left is set to 0, but then as you can see it's again set to 1.

I'm so sorry for being this messy about it, but I really have basic knowledge and I just wanted to make this simple circuit work...
This is the result I have now:
xd
Here is my full code with leds turning HIGH one after another and printing "X" right after running the program:
Code: [Select]

int constLeft=13;
const int buttonLeft=2;
const int buttonRight=3;
int stateLeft=0;
int stateRight=0;
int left=13;
int right=6;
void setup()
{
  pinMode(buttonLeft, INPUT_PULLUP);  //buttonLeft=2
  pinMode(buttonRight, INPUT_PULLUP); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);

}

void loop()
{
  
  digitalWrite(left, HIGH);
  delay(500); //delay 500ms
  digitalWrite(left, LOW);
  delay(500); //delay 500ms
  left--;
  if(left<8)
    left=constLeft;
    
   stateLeft=digitalRead(buttonLeft);
   if (stateLeft==HIGH){
       Serial.println("X");
   }
}


For now I'm not building phisically this circuit, I just wanted to start with this online version and then try real.  :)
Thanks again.

PerryBebbington

Those little momentary contact switches may need to be rotated 90°.

...R
Robin,
The OP has got them wired diagonally, so the orientations doesn't matter.

TomGeorge

#8
Feb 23, 2021, 03:01 pm Last Edit: Feb 23, 2021, 03:01 pm by TomGeorge
Hi,
The response in the monitor you are getting is correct.
When you press the button you switch the relative input to gnd, which is LOW or 0.
When the button is not pressed the internal pullup resistor pulls the relative input to 5V, HIGH or 1.

You can change the response in my code by changing these lines.
Code: [Select]
stateLeft = digitalRead(buttonLeftPin);
  stateRight = digitalRead(buttonRightPin);

to
Code: [Select]
stateLeft = !digitalRead(buttonLeftPin);
  stateRight = !digitalRead(buttonRightPin);

The ! character inverts the value of the detected digitalRead.

What do you want your code to do when the left or right buttons are pushed?

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

aceimnors

Of course... I didn't get that at first! Thank you sooo much, now everything works with those buttons.
I misunderstood which kind of signal is being sent when I press the button - I thought it would be HIGH... Now all is fine.

And what I wanted to achieve is controling direction of lighting up those leds - when I press once Right Button, then leds should shine from right to the left infinitely. Same thing when I press Left Button - just to control the direction of this signal :)

Thanks again.
Topic can be closed!

Go Up