Can someone tell me if there's a "stay/don't change state" type function?

I'm trying to code my servo to move only when "safe" to do so.

My code is trying to say that when my warning light is on stay in the current position. So I put the position it should already be in (i.e. 0/90 degrees) except what I've realised is it basically results in you being able to move the servo still just backwards with the buttons... so button 1 might be move to 90 degrees normally but with my light on it now turns it to 0 which I don't want.

And I can't find anything on how to keep something in its current state and stay still and not move...

If anyone has any suggestions that would be fantastic!!!

Welcome. Please read this to learn how to post code in a readable format, and other things you should know:

int REDPin=3;
int REDState=0;

int GREENPin=2;
int GREENState=0;

int YELLOWPin=4;
int YELLOWState=0;

int button1Pin=6;
int button1ValNew;
int button1ValOld=1;

int button2Pin=8;
int button2ValNew;
int button2ValOld=1;

#include <Servo.h>
int servoPin=9;
int servoPos=0;

const int trig = 12;
const int echo = 13;
int distance = 0;
int duration = 0;

Servo myServo;

void setup () {
  Serial.begin(9600);

  digitalWrite(REDPin,HIGH);
 
  myServo.attach(servoPin);
  myServo.write(servoPos);
 
  pinMode(button1Pin, INPUT);
  digitalWrite(button1Pin,HIGH);
  pinMode(button2Pin, INPUT);
  digitalWrite(button2Pin,HIGH);
  pinMode(echo,INPUT);
 
  pinMode(REDPin, OUTPUT);
  pinMode(GREENPin, OUTPUT);
  pinMode(YELLOWPin, OUTPUT);
  pinMode(trig, OUTPUT);
}

void loop(){
  button1ValNew=digitalRead(button1Pin);
  button2ValNew=digitalRead(button2Pin);
  digitalWrite(trig, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration/2)/28.5;
  Serial.println(distance);

 if (distance <= 7){
    digitalWrite(YELLOWPin, HIGH);
    YELLOWState=1;

  }
  else {
    digitalWrite(YELLOWPin, LOW);
    YELLOWState=0;
  }  



   if (button1ValOld==1 && button1ValNew==0){
    if (YELLOWState==1){ 
      myServo.write(0);
    }

    if (YELLOWState==0){
      digitalWrite(REDPin, LOW);
      myServo.write(90);
      digitalWrite(GREENPin,HIGH);  
    }
  }

  if (button2ValOld==1 && button2ValNew==0){
    if (YELLOWState==1){
      myServo.write(90);
     }
     
    if (YELLOWState==0){
      digitalWrite(GREENPin,LOW);
      myServo.write(0);
      digitalWrite(REDPin,HIGH);
    }
  }
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

First, anything like this should be written like:

    if (YELLOWState==1){
      myServo.write(90);
     }
     
    else {

Thank you
I did think I got carried away with the ifs

  • regardless of what your buttons do, won't the YELLOWPIN be repeatedly set HIGH the next iteration of loop if distance remains <= 7? is some mechanism needed to prevent this?
  • where are button1/2ValOld set to the button1/2Val

So the task is to make landing gear with a safety feature, so my feature is if the ultrasonic sensor reads 7 then it's not safe to move the gear so pressing the buttons shouldn't do anything. So I want it to repeatedly be HIGH if its <=7

And I don't really understand your second point? My friend helped me with this bit so I'm not too sure what she did but it worked so I didn't question it too much as I couldn't get the other bits to work so focused on them

since button1/2ValOld are initialized to 1 and that variable is never changed, that test in the conditions will always be true

then the test for the button presses should be inside a condition that YELLOWState == 0 and those tests for YELLOWState == 1 within the button conditions seem needless

It might not be ideal but it kind of works so I don't really want to go changing too much given I don't have enough experience with it (this is the first and only project I've ever attempted so) and again I'm not too sure what you're saying...
I think it was her way of not having to press and hold the button because she couldn't get PULLUP to work maybe? I think she mentioned something to do with the buttons when I asked why isn't there a INPUT_PULLUP