Servo Motor timed reaction

hey guys if anyone would be kind enough to check my codes for a bit… just want someones advice on this

#include <Servo.h>

Servo servo1;
unsigned long time1;
unsigned long condition1;
const int button1 = 17;
int bstate1 = 0;
int laststate1 = 0;
int count1 = 100;

void setup(){
 pinMode (button1, INPUT);
 servo1.attach(9);
}

void loop(){
 millis();
 if(bstate1 != laststate1){
   if(bstate1 == HIGH){
     time1 = millis();
     servo1.write(100);
   }
 }
 laststate1 = bstate1;
 condition1 = millis() - time1;
 if (condition1 == 1000){
    servo1.write(0);
    count1--;

 }

}

the result i’m looking for is that
When the USER presses the button, even if the user holds the button for a long period, the “count1” variable will only decrement once.
For the servo motor, its degree position would be 100 after pressing the button and after 1 Second (1000 ms) the degree position would be set to 0 degrees.

i’m want to avoid using DELAY

Is the code right?

 millis();

What good is this?

When the USER presses the button, even if the user holds the button for a long period, the "count1" variable will only decrement once.

You are using the wrong approach. What you want is to look at the state change detection example. You want to know when the switch changes from pressed to released or from released to pressed, rather than IF the switch is pressed.

When the switch transitions one way, increment count. Ignore the other transition.

Is the code right?

I'm not being funny, but why don't you try it... then you'll know the answer. If it's not right, be it compile errors or "unexpected" behaviour, then you'll need to do some debugging. If it still gives problems, then ask specific questions here.

I tested the code. Used the serial monitor to check the value of the "condition1" variable.

if (condition1 == 1000){ servo1.write(0); Serial.print("condition = "); Serial.println(condition1); }

The serial monitor didn't display the value of "condition1" even after pressing the button. I checked that "button1" is HIGH while being pressed when i tested the code.

Hitting EXACTLY 1000 may be a problem.

Take a look at digitalRead. Your original code doesn't ever read anything to tell you a button has been pressed.

Sorry about that. I typed on the spot and didn’t really copy my code from notepad.

#include <Servo.h>

Servo servo1;
unsigned long time1;
unsigned long condition1;
const int button1 = 17;
int bstate1 = 0;
int laststate1 = 0;
int count1 = 100;

void setup(){
 pinMode (button1, INPUT);
 servo1.attach(9);
}

void loop(){
 millis();
 bstate1 = digitalRead(button1);
 if(bstate1 != laststate1){
   if(bstate1 == HIGH){
     time1 = millis();
     servo1.write(100);
   }
 }
 laststate1 = bstate1;
 condition1 = millis() - time1;
 if (condition1 == 1000){
    Serial.print("condition = ");
    Serial.println(condition1);
    servo1.write(0);
    count1--;

 }

That code won't verify or Auto Format. It may just be a copy/paste problem when you posted it or your program may actually be wrong. If you put each curly bracket on its own line, which is a good habit to get into, it is easy to see the problem because the left/right curly brackets don't match

void loop()
{
  millis();
  bstate1 = digitalRead(button1);
  if(bstate1 != laststate1)
  {
    if(bstate1 == HIGH)
    {
      time1 = millis();
      servo1.write(100);
    }
  }
  laststate1 = bstate1;
  condition1 = millis() - time1;
  if (condition1 == 1000)
  {
    Serial.print("condition = ");
    Serial.println(condition1);
    servo1.write(0);
    count1--;

  }

That is the end of the code that you posted. Is it the end of your program ?

PaulS:
Hitting EXACTLY 1000 may be a problem.

i change the if condition to if(condition1 > 800 && condition < 1500)
the servo changed position accordingly from 100, and for a moment before changing to 0.

UKHeliBob: That code won't verify or Auto Format. It may just be a copy/paste problem when you posted it or your program may actually be wrong. If you put each curly bracket on its own line, which is a good habit to get into, it is easy to see the problem because the left/right curly brackets don't match

void loop()
{
  millis();
  bstate1 = digitalRead(button1);
  if(bstate1 != laststate1)
  {
    if(bstate1 == HIGH)
    {
      time1 = millis();
      servo1.write(100);
    }
  }
  laststate1 = bstate1;
  condition1 = millis() - time1;
  if (condition1 == 1000)
  {
    Serial.print("condition = ");
    Serial.println(condition1);
    servo1.write(0);
    count1--;

  }



That is the end of the code that you posted. Is it the end of your program ?

Again sorry about that. didn't completely copied the whole code