# My code for is not working?

**URL:** <https://forum.arduino.cc/t/my-code-for-is-not-working/1074619>\
**Category:** General Guidance\
**Created:** [January 8, 2023, 3:06am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619 "2023-01-08T03:06:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 3:06am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/1 "2023-01-08T03:06:06Z")

</div>

The servo motor is not being activated when I press the 2nd and 3rd button what is the problem?

#include\<Servo.h\> //include the servo library  
int BUTTON1 = 5; //pin of the first button  
int BUTTON2 = 4; //pin of the second button  
int BUTTON3 = 3; //pin of the third button  
int BUTTON4 = 2; //pin of the fourth button  
int servoPin = 9; // Servo pin  
Servo servo; //create a servo object  
int pos = 0; //initial position of the servo  
void setup() {  
// put your setup code here, to run once:  
//pin used by the servo  
servo.attach(9);  
pinMode(BUTTON1, INPUT\_PULLUP); //define first button as input pullup  
pinMode(BUTTON2, INPUT\_PULLUP); //define second button as input pullup  
pinMode(BUTTON3, INPUT\_PULLUP); //define third button as input pullup  
pinMode(BUTTON4, INPUT\_PULLUP); //define fourth button as input pullup

}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:  
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed  
delay(5); //5 milliseconds of delay  
servo.write(pos); //servo goes to variable pos  
}  
if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:  
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed  
delay(5); //5 milliseconds of delay  
servo.write(pos); //servo goes to variable pos  
}  
}

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 3:10am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/2 "2023-01-08T03:10:47Z")

</div>

Hi, @jonathan678  
Welcome to the forum.

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

Please post your code in code tags in a new post.

What are each of the buttons supposed to do?  
What model Arduino are you using?  
How are you powering your servos?

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺  
PS, Before posting press [CTRL] [T] in the IDE to format your code to make it easier to read.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 3:16am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/3 "2023-01-08T03:16:55Z")

</div>

Hi,  
You need to put constraints on your "pos" value so it goes no lower than 0 and no higher than 180.

> **[constrain() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/math/constrain/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

```cpp
sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150

```

Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 3:36am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/4 "2023-01-08T03:36:37Z")

</div>

I'm using an arduino uno with the servo motor plugged into a breadboard and the uno.  
Are you sure that I have put constraints on the "pos" value can you elaborate more on that.

```cpp
#include<Servo.h> //include the servo library
int BUTTON1 = 5; //pin of the first button
int BUTTON2 = 4; //pin of the second button
int BUTTON3 = 3; //pin of the third button
int BUTTON4 = 2; //pin of the fourth button
int servoPin = 9; // Servo pin
Servo servo; //create a servo object
int pos = 0; //initial position of the servo
void setup() {
// put your setup code here, to run once:
//pin used by the servo
servo.attach(9);
pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup

}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 8, 2023, 3:43am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/5 "2023-01-08T03:43:46Z")

</div>

**pos** must be in the range of 0-180

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [January 8, 2023, 3:49am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/7 "2023-01-08T03:49:05Z")

</div>

Welcome to the forum.

The documentation is online. Read about Servo.write() and constrain().  
Did you click on that link by TomGeorge ? To read about the contrain() function ?

The Servo.write() is here : [Servo - write() - Arduino Reference](https://www.arduino.cc/reference/en/libraries/servo/write/)  
There you can read that the angle must be from 0-180.

Suppose I take your sketch, then do what the others wrote, then put it in Wokwi simulation, then you get this:

> **[to-servo-or-not-to-servo.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/353255619205199873)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

The real question is: **Why does your servo motor not work ?**  
Even a small servo motor can require a peak current of 500mA. Breadboards have bad contacts and jumper wires can be broken.  
Do you have a multimeter ?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 3:50am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/8 "2023-01-08T03:50:09Z")

</div>

Hi

> [@TomGeorge](#):
>
> `sensVal = constrain(sensVal, 10, 150);`

Was an example.

```arduino
pos= constrain(pos, 0, 180);

```

Place this after each of your

```arduino
pos++

```

and

```arduino
pos--

```

It will stop your pos going out of range.  
Put some Serial.print statements in to help you debug your code with the IDE monitor.

Tom 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [January 8, 2023, 3:56am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/9 "2023-01-08T03:56:53Z")

</div>

Next time you post code, use the **auto-format** function in the IDE to indent and verify your code structure.

You’ll get more friends, and more help.  
Good work with the **code-tags**

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 4:44am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/10 "2023-01-08T04:44:54Z")

</div>

It still doesn't work the way I want it to even though I made changes in the code.

The view in tinkercad.

 ![Screenshot 2023-01-07 at 9.43.20 PM](https://europe1.discourse-cdn.com/arduino/original/4X/5/2/5/5258dd519ef9e78e8e921ffd168873b5dd9b04c9.png)

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 4:55am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/11 "2023-01-08T04:55:58Z")

</div>

> [@jonathan678](#):
>
> It still doesn't work the way I want it to even though I made changes in the code.

What way is it working.  
Can you post your new code in a new post please?

Can you please post some images of your project?

Thanks,, Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 5:07am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/12 "2023-01-08T05:07:30Z")

</div>

The project is exactly like the tinkercad that I posted. You check out the tinkercad version which also has the code.

> **[Login | Tinkercad](https://www.tinkercad.com/login?next=%2Fthings%2F08zjC6nrna2-project-9%2Feditel%3Fsharecode%3DmguT70zTPD_zjEm3_30VSstTchyRhXvpoc4JuyLkseg)**

```cpp
#include<Servo.h> //include the servo library
int BUTTON1 = 5; //pin of the first button
int BUTTON2 = 4; //pin of the second button
int BUTTON3 = 3; //pin of the third button
int BUTTON4 = 2; //pin of the fourth button
int servoPin = 9; // Servo pin
Servo servo; //create a servo object
int pos= constrain(pos, 0, 180); //initial position of the servo
void setup() {
  // put your setup code here, to run once:
  //pin used by the servo
  servo.attach(9);
  pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
  pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
  pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
  pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup
  Serial.print(pos);
}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
    pos++; 
    pos= constrain(pos, 0, 180); //increases the value of the "pos" variable each time the push button of the left is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
  if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
    pos--; 
    pos= constrain(pos, 0, 180); //decreases the value of the "pos" variable each time the push button of the right is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
}

```

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 5:25am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/13 "2023-01-08T05:25:24Z")

</div>

Hi,  
We need to see your project layout to check if your protoboard does not have these breaks in the side bus.  
 ![protobreak](https://europe1.discourse-cdn.com/arduino/original/4X/1/5/8/1586401306bd1af06bced184c63266db1175ce5d.jpeg)

Do you have a DMM?

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 5:36am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/14 "2023-01-08T05:36:53Z")

</div>

![image1](https://europe1.discourse-cdn.com/arduino/original/4X/b/b/b/bbb23b7a5905dbacce449be75a067f9404439c63.jpeg)

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [January 8, 2023, 6:27am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/15 "2023-01-08T06:27:33Z")

</div>

Thanks for the photo, I think that is okay.

Can you try to make two test sketches ?  
One that moves the servo motor with delays and another that reads the buttons and shows them on the Serial Monitor.

You really need a multimeter (some call it a DMM, but in my country it is a multimeter).

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 6:41am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/16 "2023-01-08T06:41:02Z")

</div>

> **[Login | Tinkercad](https://www.tinkercad.com/login?next=%2Fthings%2FjvPx2xmGz3T-project-9%2Feditel%3Fsharecode%3D9HyUFg1P3S5ZGgB2BMoYuWvq9ypxd2t-J9-fd4HPXdk)**

The link for the sketch is editable above, where the simulation with the diagram and code is also available for you to try. But I don't think adding delays or not would make a notable change.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [January 8, 2023, 10:06am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/17 "2023-01-08T10:06:16Z")

</div>

That is a "share" link. I think that everyone can now alter your Tinkercad project. Did you make a copy for yourself ?  
There might be a "make public" link, but I don't know how.

I already have your project working in the Wokwi simulator. Have you tried that link ?

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [January 8, 2023, 10:24am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/18 "2023-01-08T10:24:04Z")

</div>

> [@Koepel](#):
>
> I think that everyone can now alter your Tinkercad project

I can't.  
I don't have an account.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 8, 2023, 10:30am UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/19 "2023-01-08T10:30:09Z")

</div>

Hi,  
Do you have a DMM?  
The reason is, I don't think you have a good connection between the button legs and the protoboard sockets.

 ![buttons1](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/6/076221f77218da419623ffa88ea4c2e9543bbe4c.jpeg)

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 8, 2023, 4:06pm UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/20 "2023-01-08T16:06:36Z")

</div>

> [@jonathan678](#):
>
> ```arduino
> pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
> pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
> pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
> pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup
> 
> ```

You seem to be using the internal pull-up resistors. You are ALSO using external resistors. Are those pull-up resistors or pull-down resistors? It looks like they are pull-down resistors which are fighting with the internal pull-up resistors.

You should eliminate the external resistors and wire the buttons between the Arduino pins and Ground.

---

<div class="post-metadata">

**Author:** ![jonathan678](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jonathan678](https://forum.arduino.cc/u/jonathan678)\
**Post date:** [January 8, 2023, 7:19pm UTC](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619/21 "2023-01-08T19:19:57Z")

</div>

It still doesn't work. Here's a top view.

 ![image1-1](https://europe1.discourse-cdn.com/arduino/original/4X/2/a/f/2afc9262b71b75f9c11f0d01f53377f75b9ac045.jpeg)

[Next page](https://forum.arduino.cc/t/my-code-for-is-not-working/1074619.md?page=2)
