Why is it not working? pressing pin 1 should start the program, but it doesn't :(

here is the program:

#include <Servo.h>
Servo myservo;
void setup() {
  pinMode(13, OUTPUT );
  pinMode(1, INPUT);
  pinMode(8, OUTPUT );
  myservo.attach(9);
  myservo.write(90);
  digitalWrite(8, LOW);
  digitalWrite(13, HIGH);
  delay(1000 * 0.5);
  digitalWrite(13, LOW);

  if (digitalRead (1) == HIGH) { 

    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);

    delay(1000 * 0.5);


    myservo.write(0);
    digitalWrite(8, HIGH); 
    delay(1000 * 25 );
    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    myservo.write(90);
    delay(1000 * 0.2);
    digitalWrite(8, LOW);
    delay(1000 * 0.5);

    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);
    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);
    

  }
}
void loop() {


}
  1. Thank you for posting properly formatted code.

  2. Unless you have a physical pull up or pulldown resistor on pin 1, then it should be declared as INPUT_PULLUP

  3. Your if statement runs exactly once. Unless you are superhumanly fast, you will never be able to get it to run. Move the if statement into the loop() function.

  4. You should check for pin 1 being LOW in your if statement.

4.There may be other problems after doing this, but that's what comes to mind in terms of obvious changes.

1 Like

uuu really how right you are I realize it now :upside_down_face:
thank you very much for your help :slight_smile:

here it should wait 25 seconds but it doesn't wait that long just a moment? !! ahh i'm pretty lame :face_with_monocle: :slight_smile:

pinMode(1, INPUT);

Also, avoid pin 1 as it is used for serial communication.

Never ever use delay( ) in your sketches unless you know why you shouldn’t.

Google State Machine and millis TIMER.

Show us your circuit schematic and a good image of your wiring.

Don't you mean, until you discover why you shouldn’t?

sorry i couldn't reply yesterday. it was because i made my profile that day and was restricted.
I was told I can't post any more replies, I have to wait 18 hours to reply.
(if what I write is a bit weird it's because I use a translator and it sometimes translates weirdly)
thanks for any help @LarryD

Schematics and images don't need to be translated. That's one really great thing about them.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.