Pages: [1]   Go Down
Author Topic: Servo Motor timed reaction  (Read 465 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
millis();
What good is this?

Quote
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.
Logged

Online Online
Faraday Member
**
Karma: 80
Posts: 3786
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hitting EXACTLY 1000 may be a problem.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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--;

 }
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Logged

Pages: [1]   Go Up
Jump to: