Servo Motor not working properly with led

I created this code for a metal detector

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

const byte npulse = 12; // number of pulses to charge the capacitor before each measurement
 
const byte pin_pulse = A0; // sends pulses to charge the capacitor (can be a digital pin)
const byte pin_cap  = A1; // measures the capacitor charge
const byte pin_LED = 12; // LED that turns on when metal is detected
 
void setup() {
  Serial.begin(9600);
  myServo.attach(servoPin);
  pinMode(pin_pulse, OUTPUT);
  digitalWrite(pin_pulse, LOW);
  pinMode(pin_cap, INPUT);
  pinMode(pin_LED, OUTPUT);
  digitalWrite(pin_LED, LOW);
}
 
const int nmeas = 256; //measurements to take
long int sumsum = 0; //running sum of 64 sums
long int skip = 0; //number of skipped sums
long int diff = 0;      //difference between sum and avgsum
long int flash_period = 0; //period (in ms)
long unsigned int prev_flash = 0; //time stamp of previous flash
 
void loop() {

  int minval = 2000;
  int maxval = 0;
 
  //perform measurement
  long unsigned int sum = 0;
  for (int imeas = 0; imeas < nmeas + 2; imeas++) {
    //reset the capacitor
    pinMode(pin_cap, OUTPUT);
    digitalWrite(pin_cap, LOW);
    delayMicroseconds(20);
    pinMode(pin_cap, INPUT);
    //apply pulses
    for (int ipulse = 0; ipulse < npulse; ipulse++) {
      digitalWrite(pin_pulse, HIGH); //takes 3.5 microseconds
      delayMicroseconds(3);
      digitalWrite(pin_pulse, LOW); //takes 3.5 microseconds
      delayMicroseconds(3);
    }
    //read the charge on the capacitor
    int val = analogRead(pin_cap); //takes 13x8=104 microseconds
    minval = min(val, minval);
    maxval = max(val, maxval);
    sum += val;
 
    //determine if LEDs should be on or off
    long unsigned int timestamp = millis();
    byte ledstat = 0;
    if (timestamp < prev_flash +12) {
      if (diff > 0)ledstat = 1;
      if (diff < 0)ledstat = 2;
    }
    if (timestamp > prev_flash + flash_period) {
      if (diff > 0)ledstat = 1;
      if (diff < 0)ledstat = 2;
      prev_flash = timestamp;
    }
    if (flash_period > 1000)ledstat = 0;
 
    //switch the LEDs to this setting
    if (ledstat == 0) {
      digitalWrite(pin_LED, LOW);
      Serial.println("No metal");
    }
    if (ledstat == 1) {
      digitalWrite(pin_LED, LOW);
      Serial.println("No metal");
    }
    if (ledstat == 2) {
      digitalWrite(pin_LED, HIGH);
      Serial.println("Metal detected");
    }
    if Serial.print=("Metal detected")
    myServo.write(servoPos);
    
  }
 
  //subtract minimum and maximum value to remove spikes
  sum -= minval; sum -= maxval;
 
  //process
  if (sumsum == 0) sumsum = sum << 6; //set sumsum to expected value
  long int avgsum = (sumsum + 32) >> 6;
  diff = sum - avgsum;
  if (abs(diff)<avgsum >> 10) {   //adjust for small changes
    sumsum = sumsum + sum - avgsum;
    skip = 0;
  } else {
    skip++;
  }
  if (skip > 64) {  // break off in case of prolonged skipping
    sumsum = sum << 6;
    skip = 0;
  }
 
  // one permille change = 2 ticks/s
  if (diff == 0) flash_period = 1000000;
  else flash_period = avgsum / (2 * abs(diff));
}

and changed a part to this

if (ledstat == 2) {
      digitalWrite(pin_LED, HIGH);
      Serial.println("Metal detected");
      myServo.write(90);
      delay(3000);
      myServo.write(0);```

It doesn't work and just stays on metal detected and the motor doesn't move back and the led stays on.

Why are you reposting your questions ?

Because my science project is due today and I ran into some last minute problems and no one is answering.

ask person who wrote this sketch. i am sure that is not you.

Youtube how am i supposed to ask i commented but like chya like he's gonna answer.

Also I am running into an error from this code saying "else without a previous 'if'"

    if (degrees=("Metal detected"));
    myServo.write(135);
      delay(30);
      myServo.write(90);
      else
      myServo.write (45);
      delay(3000);
      myServo.write(0);
    }

if statement has no action in this case
I am not sure if it even compare without error

I do not understand???

It's because that ; on the end of your "if", ends the "if", so the "else" is a sort of orphan.

Lose that ; and put the lines that are part of the test in a pair of {}. The else stuff needs to be {}'ed also.

edit... also the = in the test needs to be a ==.

edit edit... Also not sure if you can compare degrees which is presumably declared somewhere as a number of some type, can be compared with the string. But that you will find out once you get rid of the orphaned "else" which is what you asked about.

Like this???

if (degrees==("Metal detected"))
    {
    myServo.write(135);
      delay(30);
      myServo.write(90);
    }
      else
    {
      myServo.write (45);
      delay(3000);
      myServo.write(0);
    }

Yeah, but see my edit to my edit about comparing "Metal detected" to a number.

Sorry had to create a new acc to reply but I have set degrees to the input of the serial monitor by setting it as a variable then going like

degrees=Serial.parseInt();

sorry the name degrees may sound confusing as it may sound like a number

O....k. That's a funny one, but whatever.

If degrees is an integer like say 25 or 56 or whatever, how can that possibly be equal to a string "Metal detected"?

It IS a number, or at least it's a variable containing a number.

I do not understand because I set it as the input the serial monitor reads

You lost me there, and unfortunately I'm at work right now with a deadline for later today, and don't have time to have a proper look at the whole sketch and try to unravel what you're trying to do.

Since parseInt() returns an integer (number) from Serial then 'degrees' IS a number (0 if there are no numbers in the Serial message). What were you trying to do with that line?

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