NOOB HELP

hi, I’m pretty new here, so please, if this is in the wrong section please point this out, but I’m trying tio make an led turn on when I press a button and also at the same time I’m trying to move a servo. I’m having a problem though, I seem to nopt be able to turn the damn thing off, servo turns but ld just stays lit, any ideas where I’m going wrong?

#include <Servo.h>#

I have rewrite your code using an interrupt (more information about interrupts). I don’t understand your servo code, but I think it isn’t hard to add it yourself. I haven’t tested the code… but it should work

#include <Servo.h>

const int buttonPin = 8;
const int servoPin = 9;
const int ledPin = 13;
bool btnState = 0;
int previous = LOW;
int directionState=0;
Servo servoOne;

int pos = 0;


void setup()
{
  servoOne.attach(9);
  servoOne.write(directionState);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(buttonPin,btnPressed, RISING);


}

void loop()
{
  if (btnState) {
    btnState=LOW;
    
    if(previous) {
      previous=LOW;
      digitalWrite(ledPin, LOW);
      //YOUR SERVO CODE HERE
    }
    else {
      previous=HIGH;
      digitalWrite(ledPin, HIGH);
      //YOUR SERVO CODE HERE
    }

  }
}
void btnPressed() {
  btnState = HIGH;
}
const int buttonPin = 8;
const int servoPin = 9;
const int ledPin = 13;       
int buttonState = 0;
int directionState = 0;
int state = HIGH;     
int reading;         
int previous = LOW;    
Servo servoOne;

int pos = 0;


void setup() 
{
  servoOne.attach(9);
  servoOne.write(directionState);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

 
}

void loop() 
{
 buttonState = digitalRead(buttonPin);
 
 if(reading == HIGH);
    if (state == HIGH)
      state = LOW;
    if
(state = HIGH);
     state = LOW;
      digitalWrite(ledPin, state);
previous = reading;

  if(directionState == 0)
  {
    if(buttonState == HIGH)
    {
      directionState = 1;
      for(pos = 0; pos < 90; pos=pos+1)
      {
        servoOne.write(pos);
        delay(5);
      }
    }
  }

  else if(directionState == 1)
  {
    if(buttonState == HIGH)
    {
     directionState = 0;
     for(pos = 90; pos > 1; pos=pos-1)
     {
        servoOne.write(pos);
        delay(5);

}


}


     }
    }

FlorisVO:
I have rewrite your code using an interrupt

Almost certainly unnecessary. Polling for a button is adequate, and easily managed.

And why shouldn't you use an interrupt it you can?

Almost certainly unnecessary. Polling for a button is adequate, and easily managed.

When you use interrupt, you are SURE that it 'record' button changes.

If your polling is fast enough, then you won't miss it. Also, depending on the switch, then the bounce may cause some problems.

If you add a RC debounce circuit, then you will end up with a long duration signal, longer than necessary for the arduino but much shorter than a persons reaction time.

I do agree that there are many cases where using an interrupt may be a better idea.