Why does the button work with delay?

The servo works smoothly with the data coming from the serial port, but when I press the button it does not work, but when it remains pressed it works and the servo returns to 0.

#include <Servo.h>


Servo motor;
#define button 4


long randNumber;


void setup() {
 Serial.begin(9600);
 motor.attach(5);
 pinMode(button,INPUT);

}

void loop() {

if (randNumber == 0 ){
if (digitalRead(button ) == LOW){
motor.write(0);
}}

if (randNumber == 1 ){
if (digitalRead(button ) == LOW){
motor.write(0);
}}


     
byte x = Serial.parseInt();
if(x==1){

   randNumber = random(2);

   if (randNumber == 0 ){
   motor.write(90);
   }
   
   if (randNumber == 1 ){
   motor.write(90);
   }


}

Your code spends most of it's time in Serial.parseInt(), waiting for a character; after a second, it times out and returns 0, does a pass around the loop(), and re-enters Serial.parseInt(), where it waits for another second.

1 Like

So what should I do?

Step back. I need you to describe what you want your servo to do, based on the two inputs. Try to make the description simple and complete.

1 Like

Hi @lion1983 welcome to this forum!

motor.write(90) in both if conditions?

My English is very bad.

When data comes from the serial port, it is 90 degrees. When I press the button, it does not return to 0, but when the button is pressed for a long time, it returns to 0.

no, because there will be 2 servos and it will work randomly

randNumber == 0
servo1

randNumber == 1
servo2

Okay, so you can't tell me what it is you want to do, and you will change the hardware to fit the requirement I can't read.
See ya later!

You can have a look at Serial.setTimeout() - Arduino Reference
[edit]
To determine if there is serial data is available
Serial.available() - Arduino Reference

Only call parseInt if data is available.
[/edit]

Or possibly the better way is to study Robin's updated Serial Input Basics so you can learn the basics of solid serial communication.

Hi @lion1983 ,

welcome to the arduino forum.
The arduino-forum can be of great help if you do it in a certain way.

Here is some very good help. Written in the languaga urdu
Use google translate to translate it into your native language.
Do me a favor and try google translate

click on the triangle to open the text

تو بس گوگل ٹرانسلیٹ کا استعمال کریں۔ اپنی مادری زبان میں لکھیں اور گوگل کو ترجمہ کرنے دیں۔

یہ ایک ابتدائی کے طور پر بہت عام ہے کہ آپ تمام افعال کے بارے میں تمام تفصیلات نہیں جانتے ہیں۔
آپ کے کوڈ میں جو کچھ ہوتا ہے وہ کسی ایسی چیز کی وجہ سے ہوتا ہے جسے آپ ابھی تک نہیں جانتے ہیں۔
یہی وجہ ہے کہ آپ کو عام الفاظ میں بیان کرنا چاہیے۔
تمام پروگرامنگ الفاظ سے گریز کرتے ہوئے جو آپ کوڈ سے کرنا چاہتے ہیں۔

اس تفصیل کے ساتھ صارفین کو ایک واضح تصویر ملے گی کہ آپ کا کوڈ کیا کرے گا۔
آپ کے ابھی تک غیر فعال کوڈ کو دیکھنے سے کہیں زیادہ واضح تصویر

best regards Stefan

You have attached one servo. You need two servos on PWM pins

Servo motor1;
Servo motor2;
.
.
  #define servoPin1 5
  #define servoPin2 6
.
.
  motor1.attach(servoPin1);
  motor2.attach(servoPin2);

[edit] Also, if x == 1, your sketch makes the servo motors to go to 0 and 90 when the random number is 0 and 1.

Herkese teşekkür ederim, sorunu çözdüm

What was the solution, please tell us what you did, so others looking for a solution can get help from this thread.

Tom.. :grinning: :+1: :coffee: :australia:

#include <Servo.h>

Servo motor;
#define button 4

long randNumber;

void setup() {
Serial.begin(9600);
motor.attach(5);
pinMode(button,INPUT);

}

void loop() {

if (randNumber == 0 ){
if (digitalRead(button ) == LOW){
motor.write(0);
}}

if (randNumber == 1 ){
if (digitalRead(button ) == LOW){
motor.write(0);
}}

if (Serial.available() > 0) {

byte x = Serial.parseInt();
if(x==1){

randNumber = random(2);

if (randNumber == 0 ){
motor.write(90);
}

if (randNumber == 1 ){
motor.write(90);
}

}
}

If you would be so kind, mark @sterretje 's post as the solution.

Happy it worked out!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.