Moving a Servo and Lighting an LED when a button is pushed


I am very new to using Arduino, I’m not really sure how anything really works. I have done the basics and lots of research but just can’t seem to solve the problem.

I am trying to move a servo 90 degrees, and have an LED light up when a button is pressed. I have an LED mvoing when a button is pressed in one sketch (successfully) and an LED lighting up when a button is pressed in another sketch (also working successfully.) However, when I place the two sketches into one sketch they don’t seem to work. Either the button turns on and off, or the servo moves back and forth.

I have attached my code above.

I am trying to do it without delays!

Thank You

mannequinandlight.ino (1.23 KB)

Hello - you must simple optimise your sketch. The servo part and the LED part has the same execute condition, so they can be executed in one function.
Please see and test (and study) my solution (btw: you should give your sketch in code tags):

#include <Servo.h>

Servo mannequinservo;

const int button = 2;
const int roofLED = 13;

bool buttonreading;
bool previousbuttonstate = LOW;
int LEDstate = HIGH;

long ltime = 0;
long debounce = 200;
//unsigned long currentMillis = 0;        // Is not used!
//unsigned long previousbuttonMillis = 0;
//unsigned long previousLEDMillis = 0;
//unsigned long previousservoMillis = 0;

int pos = 90;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);       // Makes only sense with use of Serial Monitor
  pinMode (button, INPUT);
  pinMode(roofLED, OUTPUT);

  Serial.println("Start the test"); // A hint to the Serial Monitor
  ltime = millis();   //--- Init the debounce timer

void loop() {
  // put your main code here, to run repeatedly:
//  lights();

void execute() {
  buttonreading = digitalRead(button);
  if (buttonreading == HIGH && previousbuttonstate == LOW && millis() - ltime > debounce) {
    //---- Execute the Servo part ----
    if (pos == 90)
      pos = 0;
      pos = 90;
    //---- Execute the LEd part ---
    if  (LEDstate == HIGH)
      LEDstate = LOW;
      LEDstate = HIGH;
    digitalWrite (roofLED, LEDstate);
  previousbuttonstate = buttonreading;

Thank you so much. It worked perfectly.