Pages: [1]   Go Down
Author Topic: Simple servo + button + led Question  (Read 274 times)
0 Members and 1 Guest are viewing this topic.
Quebec
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am still quite new to the arduino programming and yesterday I tried to integrate two simple project together. The first one is a piece of code that I wrote to make a continuous servo turn for approximately 1sec. The second one is to light a led with the push of a button. Basically what I want to do is to have my servo rotate for 1sec and the LED turn on. On the second press have simply the LED turn off.

Heres's my code :
Code:
int switchPin = 8;
int ledPin = 13;
int servoPin = 3;

boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean motorOn = false;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(servoPin, OUTPUT);
  Serial.begin(9600);
}

/*------------------------------------------------------------*/
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}
/*------------------------------------------------------------*/

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH && motorOn == false)
  {
    ledOn = !ledOn;
    motorOn = !motorOn;
  }
 
  lastButton = currentButton;
  digitalWrite(ledPin, ledOn);
 
  int count = 0;
  int count_0 = millis();
 
  if (motorOn == true) {
    while(count <= count_0 + 1000) {
      digitalWrite(servoPin,HIGH);
      delayMicroseconds(1600);
      digitalWrite(servoPin,LOW);
      delay(20);
      count = millis();
    }
    motorOn = false;
  }
  digitalWrite(servoPin,HIGH);
  delayMicroseconds(1500);
  digitalWrite(servoPin,LOW);
  delay(20);
}


Thank you in advance.

M.
Logged

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

Code:
  boolean current = digitalRead(switchPin);
digitalRead() returns an int, not a boolean. This debounce code sucks.

Code:
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH && motorOn == false)
It's really hard to tell, from these names, whether currentButton refers to a switch number, a pin number, or a switch state. Better variable names would be in order.

Code:
  pinMode(switchPin, INPUT);
Now, this is a good name. It's clear that it refers to a pin number, and that the pin has a switch attached to it. What isn't clear is how that switch is wired. Using the internal pullup resistor greatly simplifies the wiring. No external resistor(s) required; connect one leg to ground and the other leg to the pin.

How is your switch wired?

Code:
  int count_0 = millis();
After your sketch has been running for 32 seconds, what is going to happen? Look at the millis() documentation again. The function returns an unsigned long for a reason.

Code:
  if (motorOn == true) {
Since motorOn is a boolean, it can contain only two values - true and false. So, this comparison is true == true, which is true, or false == true, which is false. As you can see the result is always the value in motorOn, so the == true part is redundant.

Code:
if(motorOn)
{
is all you need.

If you want to know if the motor is off,
Code:
if(!motorOn)
{

Code:
    while(count <= count_0 + 1000) {
Addition involving time can fail.

Code:
while(count - count_0 <= 1000)
{
does the same thing, and is guaranteed to work. But, count is a lousy name, since you aren't counting anything.

Why are you not using the Servo library? It makes dealing with the servo so much easier.

Finally, you haven't said what the code IS doing, and how that differs from what you want it to do. Without knowing that, it's hard to offer more than general advice.
Logged

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

digitalRead() returns an int, not a boolean. This debounce code sucks.
I use a boolean, because I read in the doc that digitalRead() returns HIGH or LOW, so the boolean seemed fit.

Quote
It's really hard to tell, from these names, whether currentButton refers to a switch number, a pin number, or a switch state. Better variable names would be in order.
See the new code attach if it makes more sense.

Quote
How is your switch wired?
One leg to pin8 the other to a pull down resistor which is connected to ground.

Quote
After your sketch has been running for 32 seconds, what is going to happen? Look at the millis() documentation again. The function returns an unsigned long for a reason.
Thanks, for that hint went to check in the doc and it always return a unsigned long. Thinking back it may be one of the reason it failed.

Quote
does the same thing, and is guaranteed to work. But, count is a lousy name, since you aren't counting anything.
I changed my code to have the to variable subtract from each other. That also caused me some problem too. I also changed my variable name from count to time.

Quote
Why are you not using the Servo library? It makes dealing with the servo so much easier.
Because I like to program myself basic stuff in order to understand what is happening first. Once I understand what is going on I will either build my own libraries (for fun) or use the one provided.

With the few pointer you provided me the code is now working perfectly fine

Thank You
M.

Code:
nt switchPin = 8;
int ledPin = 13;
int servoPin = 3;

boolean lastButtonState = LOW;
boolean currentButtonState = LOW;
boolean ledOn = false;
boolean motorOn = false;

long time = 0;
long time_0 = 0;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(servoPin, OUTPUT);
  Serial.begin(9600);
}

/*------------------------------------------------------------*/
boolean debounce(boolean lastState)
{
  boolean currentState = digitalRead(switchPin);
  if (lastState != currentState)
  {
    delay(5);
    currentState = digitalRead(switchPin);
  }
  return currentState;
}
/*------------------------------------------------------------*/

void loop()
{
  currentButtonState = debounce(lastButtonState);
  if (lastButtonState == LOW && currentButtonState == HIGH && motorOn == false)
  {
    ledOn = !ledOn;
    if (ledOn) {
      motorOn = !motorOn;
    }
  }
 
  lastButtonState = currentButtonState;
  digitalWrite(ledPin, ledOn);
 
  time = 0;
  time_0 = millis();
 
  if (motorOn) {
    motorOn = false;
    while(time - time_0 <= 1000) {
      digitalWrite(servoPin,HIGH);
      delayMicroseconds(1600);
      digitalWrite(servoPin,LOW);
      delay(20);
      time = millis();
    }
  }
  digitalWrite(servoPin,HIGH);
  delayMicroseconds(1500);
  digitalWrite(servoPin,LOW);
  delay(20);
}

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How is your switch wired?
Quote
One leg to pin8 the other to a pull down resistor which is connected to ground.

Well unless you enable the internal pull-up resistor for pin 8 this is not proper as you will have a 'floating input pin condition' when the button is not being pressed. Also what is the size of your external pull-down resistor?

Lefty
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9636
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably the easiest way to setup a button pin, set the pin high and then check for it being low when the switch is closed.

Code:
//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground to move servo
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: