Muscle Sensor Noise Canceling Code Not Working

Hi. We created a code that is supposed to make the servo only move when the value is a significant distance away from the previous value, and when the value exceeds 100, the servo cannot move until the value drops below 45. However, while testing, the servo would fidget with the slightest of changes in value and also would move a lot when the value exceeds 100, despite the provisions stopping it from doing so.

Here is the code:

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

int potpin = 1;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int preval;
void setup() {
  // put your setup code here, to run once:
  myservo.attach(13);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  // read the input on analog pin 1:
  int sensorValue = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (180 / 1023.0);
   // scale it to use it with the servo (value between 0 and 180)
     if (preval<=100){
      if ((voltage-45) > preval || preval > (voltage+45)) {
            myservo.write(voltage);                  // sets the servo position according to the scaled value
     float preval = voltage;
     }
     }
     else {
      if (voltage<=45){
          myservo.write(voltage);
          float preval = voltage;
      }
     }
 // print out the value you read:
  Serial.println(voltage);
 
   delay(50);    
}

Any help is very much appreciated!

I can only answer this because it appears obvious; “<” in the 100 section needs to be “>”

Generally, however, we cannot provide coding support for projects (so many options!) - best of luck moving forward!

Okay thank you for the help.