2

I'm basically trying to combine my servo code and my LED code but whenever I add my servo code the LED refuses to turn on at all. I am super new to arduino sorry if this doesn't make any sense!


Servo myservo; 

const int pushbutton_PIN = 2; 
const int POT_PIN = A0;
const int LED_PIN = 3; 

int pos = 0;   
int buttonState = 0; 
int lastbuttonState = 0; 
int analogValue = 0; 
int lightState = false; 
int brightness = 0;

void setup() { 
  myservo.attach(9); 
  pinMode(pushbutton_PIN, INPUT); 
  pinMode(POT_PIN, INPUT); 
  pinMode(LED_PIN, OUTPUT); 
  Serial.begin(9600); 
}

void loop(){
  light();
  servo();
  }

void servo(){
        for (pos = 0; pos <= 180; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);             
    delay(15);                       
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);  
  }
}
 
void light() {
      buttonState = digitalRead(pushbutton_PIN);
  if (buttonState != lastbuttonState) { 
    if (buttonState == HIGH) {
      delay(300);
      lightState = true;
      light_on();
    }
  }
  lastbuttonState = buttonState;
}
  

void light_on() {
  while (lightState == true) {
    analogValue = analogRead(POT_PIN);
    brightness = map(analogValue, 0, 1023, 0, 255);
    analogWrite(LED_PIN, brightness);
    buttonState = digitalRead(pushbutton_PIN);
    if (buttonState != lastbuttonState) { 
      if (buttonState == HIGH) {
        lightState = false;
        delay(300);
        analogWrite(LED_PIN, LOW);
        loop();
      }
    }
    lastbuttonState = buttonState;
  }
}

7
  • 3
    I think your problem is the blocking code. While your servo code run, the LED isn't handled in any way. To do multiple this at the same time (like controlling a servo, checking a button and controlling an LED, you actually need to do these this sequentially in very fast succession, basically only doing something, when it is time to do so, otherwise going further to the next thing. No delay calls (thats just busy waiting). Please look at tutorials about millis()and the BlinkWithoutDelay example, that comes with the Arduino IDE. It is very important coding principle for microcontrollers. – chrisl Apr 24 at 10:35
  • Common issue with PWM and Servo when it doesn't work is that used PWM pin is on the same Timer taken over by Servo library. But it's usualy using Timer1 and pin 3 is on timer 2, so it might not be issue in this case – KIIV Apr 24 at 10:48
  • Each call to your servo() function takes well over 5 seconds (5.4s, for the delay() calls alone!), during which nothing else - testing the button, f/ex - is being done. Seconding @chrisl 's suggestion to review the BlinkWithoutDelay example. Also search Arduino StackExchange for "non-blocking". – JRobert Apr 24 at 11:19
  • 1
    To add on to Chris's comment there is a really good sticky post on the arduino forum in the project guidance section which deals with this exact issue for newbies. I think it's called: doing several things at the same time – Zhelyazko Grudov Apr 24 at 11:49
  • is there any reason why you are moving the servo in small steps? – jsotola Apr 24 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.