3

Been trying to figure this out for a couple hours, despite how ridiculously simple it should be. I have no idea why it's always returning 1, even when i'm pushing the button. Fritzing sketch: enter image description here

My code:

#include <Servo.h>
#define button 3

Servo myservo1;
Servo myservo2;
Servo myservo3;

char incomingByte = "";
int timer;
int timer2;
int interval;
int numservo = 3;
int press1 = 90;
int letup = 0;
int val = 0;
  
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);
  myservo2.attach(7);
  myservo2.write(0);
  myservo3.attach(5);
  myservo3.write(0);
  pinMode(button, INPUT_PULLUP);
  
}

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

  val = digitalRead(button);
  Serial.println(val);
  
  //checkerthing();

    while (Serial.available() > 0) {
      incomingByte = Serial.read();
      timer  = incomingByte - '0';
      Serial.println(timer);
      timer2 = timer * 60;
      interval = timer2/numservo;
    


     motor1_control(press1, letup, interval);
     checkerthing();
     motor2control(press1, letup, interval);
     checkerthing();
     motor3control(press1, letup, interval);
     checkerthing();
     
      
      
     timer = 0;
     timer2 = 0;
     interval = 0;
    }

   

  }
    

void checkerthing() {
  val = digitalRead(button);
  if (val == LOW){ 
    Serial.println("Stopped");
    timer = 0;
    timer2 = 0;
    interval = 0;
    
  }
  if (val == HIGH){
    Serial.println("Still going");
  }

}
void motor1_control(int angle, int originalangle, int lengthoftime){
    myservo1.write(angle);
    delay(1000);
    myservo1.write(originalangle);
    delay(lengthoftime * 1000);
    
}

void motor2control(int angle, int originalangle, int lengthoftime){
    myservo1.write(angle);
    delay(1000);
    myservo1.write(originalangle);
    delay(lengthoftime * 1000);
    
}

void motor3control(int angle, int originalangle, int lengthoftime){
    myservo1.write(angle);
    delay(1000);
    myservo1.write(originalangle);
    delay(lengthoftime * 1000);
    
}

Basically, the point of this is to have the servo motors go off at specific intervals as given by the user in the serial monitor. The button exists so that the user can restart the timer at any point in the code. But the problem is, I can't even get it to register the button being pushed. It always returns "HIGH", which leads me to believe there's something wrong with the circuits. I've tried using some different methods, but no luck. Can anyone tell me what the issue here is?

Edit: ...I hadn't pushed the pushbutton all the way into the breadboard. I'm gonna go cry in the corner for a bit. And then get back to coding I guess lol. Thanks for the answers.

New contributor
Crystal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
13
  • 2
    Is your breadboard one of the "split power rail" variety? – Majenko 2 days ago
  • 1
    @Majenko no, it's a full power one – Crystal 2 days ago
  • 3
    Hade you checked you're using the correct terminals on the push button and it's making proper contact with the breadboard ? – Bra1n 2 days ago
  • 2
    What Bra1n said. I have to check those 4-lead pushbuttons every... single... time... because they're like Micro USB connectors; takes three tries to get the orientation right. Check continuity to make sure you're using the correct leads. – Dave Newton 2 days ago
  • 2
    @DaveNewton To be honest, all USB connectors have 3 sides... – Sacha yesterday
2

Hadn't pushed the button all the way in lol

New contributor
Crystal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

Crystal is a new contributor. Be nice, and check out our Code of Conduct.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.