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):
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?
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?
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.
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:
Here is my full code with leds turning HIGH one after another and printing “X” right after running the program:
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.
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.
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