Little help here please

I have the arduino uno and i want to use a pushbutton to turn on a LED then turn it off so 1 push turns it on 2 push turns it off any idea how i might do that and also i am going to be using 4 pushbuttons. Thanks

you could use something to count the push button presses.

eg

int buttonCount = 0;

if (button == HIGH) // if its pressed
{
if(buttonCount == 0)
{
turn on
buttonCount++;
}
if(buttonCount == 1)
{
turn off
buttonCount = 0;
}
}

thanks

sorry it does not work not sure whats wrong i am kinda new at this

"...it does not work..." is not helpful.

We want to see your code, we want to see any error messages, we want to know your wiring, we want to know what you intended, and we want to know what actually happens. Most of all, we want you to read the sticky topics at the top of the forum.

Code and error messages should be pasted into your post using the "#" button and Edit | Paste (or ctrl-v).

As it is, all I can do is guess that your error is in line 42. My crystal ball told me that. :slight_smile:

Thanks!

The button needs to be debounced --- look in the playground section of this site.

Mark

holmes4:
The button needs to be debounced --- look in the playground section of this site.

Mark

Hey thanks didn't even know that it existed until i updated my software

what about a servo i want to do it the same way (pushed once servo moves from 0 to 180 second push it moves from 180 to 0) the debounced works just no sure how to add a servo. by the way this is for a Iron Man helmet. Thanks

I wager that the servo example in the IDE gives you enough to go on.

Instead of digitalWrite(LED,HIGH) use MyServo.write(180), and MyServo.write(0) at the opposite. Copy and paste the appropiate #include and MyServo.attach() and you're done.

ok i will give it a try

ok i got it to work but there is still a bug in the code, here’s the code for the LED and the servo, the LED does not always turn on/off every time i push the button could anybody tell me what it is that is wrong?

#include <Servo.h>
const int servo = 8;
const int button = 9;
const int button1 = 10;
const int led = 7;
int buttonState = 0;
int directionState = 0;
int pos = 0;
int ledState = HIGH;
int buttonState1;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
Servo myservo;

void setup() {
myservo.attach(8 );
pinMode(button, INPUT);
pinMode(button1, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led, ledState);
}

void loop(){
//led code
int reading = digitalRead(button1);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}}}
digitalWrite(led, ledState);
lastButtonState = reading;
//=======================================================
// servo code
buttonState = digitalRead(button);
if (directionState == 0){
if (buttonState == HIGH) {
directionState = 1;
for(pos = 0; pos < 180; pos=pos+1)
{
myservo.write(pos);
delay(15);
}
}

} else if (directionState == 1) {

if (buttonState == HIGH) {
directionState = 0;

for(pos = 180; pos>=1; pos=pos-1)
{
myservo.write(pos);
delay(15);
}}}}

could anybody help me?

Why do you read the button twice? the same button push does both things (toggle LED toggle servo position), right? Because of the long time to move the servo you program gets confused with the two readings (No I have not studied your code in detail, just skimmed it - I haven't counted "}" if they are te right place, This would be easier if you used the ctrl-T function to format the code and when posting use the "-button between the typewriter and speechbubble - the flow and structure are more evident.

Ok thanks for the tip and no one button moves the servo from 0-180 and 180-0 and second button turns on/off the leds

Msquare:
Why do you read the button twice?

Because i want it to be push it once the servo goes from 0 to 180 the second push it moves from 180 to 0, same with the LEDs push 1 they turn on push 2 they turn off

kwebersole:

Msquare:
Why do you read the button twice?

Because i want it to be push it once the servo goes from 0 to 180 the second push it moves from 180 to 0, same with the LEDs push 1 they turn on push 2 they turn off

But you are reading it only a few microseconds apart so it will still be the same value. It will not count the number of time it is read.

oh ok thanks