Loading...
Pages: [1]   Go Down
Author Topic: LED's won't do what I tell them to do  (Read 123 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I'm trying to make a code that does this:
When a button is pushed:
move servo to 170, turn LED on and LED2 off

When a button is released:
move servo to 10, turn LED off and LED2 on

The servo works, but LED is on constantly and LED2 doesn't turn on at all
I know it's not the wiring because a simpler code turns the LED when the button is pushed (although the wiring for LED2 might be a little bit off, but I don't think so).

Here's the code:

#include <Servo.h>
 
Servo myservo;

const int BUTTON = 7;
const int LED = 13;
const int LED2 = 2;
int pos = 0;
int val = 0;

void setup()
{
  myservo.attach(9);
      pinMode(BUTTON, INPUT);
      pinMode(LED, OUTPUT);
      pinMode(LED2, OUTPUT);
}

void loop () {
  val = digitalRead(BUTTON);
  
  if (val == HIGH) {
    myservo.write (pos = 170);
    digitalWrite(LED, HIGH);
    digitalWrite(LED2, LOW);
  } else {
    myservo.write (pos = 10);
    digitalWrite(LED, LOW);
    digitalWrite(LED, HIGH);
  }
}

On a slightly separate note, if anyone could tell me how to stop the servo from spazzing out when it gets to 170, that would be great. Thanks!
« Last Edit: April 22, 2013, 01:17:10 am by twigs45 » Logged

East Anglia (UK)
Online Online
Edison Member
*
Karma: 47
Posts: 1406
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    myservo.write (pos = 170);
should be
Code:
    myservo.write(170);
and
Code:
    myservo.write (pos = 10);
should be
Code:
    myservo.write(10);

Code:
    digitalWrite(LED, LOW);
    digitalWrite(LED, HIGH);
one of these should be LED2

How are the buttons wired ?  Any pullup/down resistors, or are the input pins floating when the buttons are not pressed ?
Logged

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

Wow! Thanks so much! It was that missing 2 that broke things, and now everything is working! The (pos = 170) and (pos = 10) were working, but I like the simplicity of your version better. Thanks again!

And lastly, the little servo problem I posted at the end. When it gets to 170, it starts twitching and making clicking noises. It's as if it's trying to go farther but it can't. Know how I can solve this? Thanks in advance!
Logged

East Anglia (UK)
Online Online
Edison Member
*
Karma: 47
Posts: 1406
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried changing to 'my' way of doing myservo.write(), which is the correct way of doing it, by the way.
Logged

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

I did indeed. But the problem persists!
Logged

East Anglia (UK)
Online Online
Edison Member
*
Karma: 47
Posts: 1406
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this simple program
Code:
#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(9);
  myservo.write(170);
}

void loop () {}
Does the servo behave as expected or does it still jitter ?
Logged

Global Moderator
UK
Online Online
Brattain Member
*****
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe the servo doesn't go to 170.
Try 160 or 150, or use the writeMicroseconds method to test the servo limits.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline Offline
God Member
*****
Karma: 10
Posts: 871
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"LED's won't do what I tell them to do"

Have you tried another language other than English? Where were the LED's manufactured? They might be Chinese, Try Chinese smiley-grin
Logged

East Anglia (UK)
Online Online
Edison Member
*
Karma: 47
Posts: 1406
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The biter is bit  smiley
Quote
Have you tried another language other than English? Where were the LED's manufactured? They might be Chinese, Try Chinese

Quote
LED's
Wrong

Quote
They might be Chinese, Try Chinese
Wrong
Logged

Pages: [1]   Go Up
Print
 
Jump to: