Relay,sensor control

Hello
I would like to know if there is a kind soul who can help me make a sketch.

It should work like this: a spring is wound up via a dc motor(motor1) connected to relay with an arm. The motor stops when the arm hits a sensor (INPUT1).

Then, if another sensor (INPUT2) is activated, it turns the relay to the motor (motor1) on again, but it should only run for a few seconds so the spring gets released and the hammer goes off.

After that, another motor (motor2) should move forward toward a switch and stop, then move back again to another switch and stop there (probably requires 2 relays).

And the condition for motor (motor1) to run again and reach (INPUT1) and stop, that is when sensor (INPUT2) is no longer activated.

I tried to mess around with one based on the button example, but I can't quite get there.

Using an ESP8266 D1 Mini


int buttonPin1 = 4;  // the number of the pushbutton pin

int buttonPin2 = 12;

int ledPin = 5;    // the number of the LED pin

int motorPin = 5;





// variables will change:

int buttonState = 0;  // variable for reading the pushbutton status

int buttonState1 = 0;





void setup() {

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);

   pinMode(motorPin, OUTPUT);

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin1, INPUT);

  pinMode(buttonPin2, INPUT);

}




void loop() {

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin1);

  buttonState1 = digitalRead(buttonPin2);

  //# = digitalRead(buttonPin2);




  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  if (buttonState ==  HIGH) {

    // turn LED on:

    digitalWrite(ledPin, HIGH);

  } // else {

    // turn LED off:

   // digitalWrite(ledPin, LOW);

  //}






  if (buttonState ==  LOW) {

    // turn LED on:

    digitalWrite(ledPin, LOW);

  }





//   MUS i fælden

 if (buttonState ==  LOW,buttonState ==  HIGH)  {

    // turn LED on:

    digitalWrite(ledPin, LOW);

  } // else {

    // turn LED off:

   // digitalWrite(ledPin, LOW);

  //}




//

 if (buttonState ==  HIGH,buttonState ==  HIGH)  {

    // turn LED on:

    digitalWrite(ledPin, HIGH);

  } // else {

    // turn LED off:

   // digitalWrite(ledPin, LOW);

  //}







// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

//if (buttonState1 ==  HIGH) {

    // turn LED on:

// digitalWrite(motorPin, HIGH);

// }  else {

    // turn LED off:

// digitalWrite(motorPin, LOW);

//}








}

Your code has a logic bug: you’re using a comma inside the if condition. In C/C++, the comma operator evaluates the left expression but discards it, and only the right expression determines the f result. So those two “combined” conditions don’t do what you think—they effectively behave like a single simple check and make the logic unreliable.

There’s also a hardware/IO issue: you assigned the same GPIO pin to both the LED and the motor output, which will cause conflicts (both will toggle together). Each device needs its own dedicated output pin.

Finally, for switches/sensors on ESP8266 you should not leave inputs floating; use a defined pull-up/pull-down (typically INPUT_PULLUP) and wire accordingly, otherwise you’ll get random triggering.

Given your described sequence (wind → stop on INPUT1 → release for a timed pulse on INPUT2 → motor2 forward/back to limit switches → re-arm when INPUT2 is inactive), this should be implemented as a simple state machine with non-blocking timing (millis()), not a few independent if statements.

Your sketch needs to be Auto Formatted. It is in the IDE under Tools. Next get rid of all the extra blank lines.
There is a Paid Consultancy section if you want to go thst route but here we are volunteers and we expect you to build the project and write the code. Once you get stuck then show us a picture of a hand drawn wiring diagram and your code.

Explain what you are trying to do.
The purpose and a sketch would help.

I am trying to make a device where a motor with an arm pulls up a spring and stops when it reaches the top sensor,

so when the bottom sensor is activated, the motor runs for a few seconds, then the arm releases the spring,

but the motor must not run again to reach the top sensor until the bottom sensor is no longer active.

Re-read your sketch. This is an example of errors in the sketch.

[edit]

Would you make a hand-drawing of your idea?

When is the "bottom sensor" active? What activated it? Did the motor pulling up activate it? If the motor is pulling up, but can not move, how is the bottom sensor going to become inactive?

a rodent activated it :see_no_evil_monkey:

when the sping is loaded, and a rodent enters, the bottom sensor get high, and the motor1 is runing for 2 sec

but i cant reload the spring before the rodent i gone,

later in my project the bottom plate is moving to on side and the dead rodent falls into a bucket, the´n plate moves back and the spring reloads

Hand-draw pictures of how this will work. Top view and side view. Post those pictures here.