servo won't stop

Hello everyone, I am working on a project with a servo motor and am having a hard time stopping it. I have only done one other arduino project before this one, so I am still really new to programing. I posted my code below. During low light levels, I want my code to turn a servo on, move it left then right, and then turn to the original starting position and stop. before it does this, I want a blue and green led to fade on. However, when I ran my code, the LEDs and servo turned on immediately, ran, and did not stop regardless of the light level. It seems as though it is ignoring the IF statements all together. If you could help me out I would greatly appreciate it.

#include <Servo.h>

Servo myservo;

int thresholdval = 300;
int pos = 80;
int uvled = 7;
int photo = A0;
int fireled1 = 3;
int fireled2 = 4;
int blue = 11;
int green = 6;
int red = 10;
int fadeblue =0;
int fadegreen=0;
int fadered = 0;
int activate = 0;

void setup(){
myservo.attach(9); //attaches servo to pin 9
pinMode(uvled, OUTPUT);
pinMode(fireled1, OUTPUT);
pinMode(fireled2, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);

{
myservo.write(pos);
delay(15);
} }
void loop(){
if(photo<thresholdval || activate == 1){
activate = 1;
digitalWrite(uvled,HIGH);
digitalWrite(fireled1,LOW);
digitalWrite(fireled2,LOW);
for(fadeblue = 0; fadeblue < 255; fadeblue += 1);
for(fadegreen = 0; fadegreen < 255; fadegreen += 1);
analogWrite(blue,fadeblue);
analogWrite(green,fadegreen);
delay(5);
if( fadeblue == 255 && fadegreen == 255){
{
myservo.write(160);
delay(2000);
} {
myservo.write(1);
delay(2000);
} {
myservo.write(pos);
delay(2000);
}
delay(5);
activate =0;
}
}
else if(photo>thresholdval){
digitalWrite(uvled,LOW);
digitalWrite(fireled1,HIGH);
digitalWrite(fireled2,HIGH);
for(fadeblue = 255; fadeblue>=1; fadeblue-=1);
for(fadegreen = 255; fadegreen>=1; fadegreen-=1);
analogWrite(blue,fadeblue);
analogWrite(green,fadegreen);
digitalWrite(fireled1, LOW);
delay(15);

}}

if(photo<thresholdval || activate == 1){

You need to read the analog pin to start.

if(analogRead(photo)<thresholdval || activate == 1){
void loop(){
  if(photo<thresholdval || activate == 1){

Get in the habit NOW of putting every { and every } on lines BY THEMSELVES. Get in the habit NOW of using Tools + Auto Format BEFORE posting code. Get in the habit of using spaces to make the code readable.

void loop()
{
  if(photo < thresholdval || activate == 1)
  {

Now, what values do photo and thresholdval have? A0 is certainly less than 300, for all current Arduinos.

Please use the “insert code” button when pasting code.

 {                                
    myservo.write(pos);            
    delay(15);                        
  }

That code shouldn’t be in setup. Also you’re not using brackets after your for loops in the if and the else if conditions.

This:

for(fadeblue = 0; fadeblue < 255; fadeblue += 1);
for(fadegreen = 0; fadegreen < 255; fadegreen += 1);
 analogWrite(blue,fadeblue);
   analogWrite(green,fadegreen);
   delay(5);

Should be this:

for(fadeblue = 0; fadeblue < 255; fadeblue += 1){
for(fadegreen = 0; fadegreen < 255; fadegreen += 1){
 analogWrite(blue,fadeblue);
   analogWrite(green,fadegreen);
   delay(5);
}}

Notice the {'s after the for loops and the }'s at the end… Also you don’t want to use a semicolon after the for loop.

Thanks for your help groundfungus and partsofme. I fixed my code in both of those places, but now it seems there's a new problem. it seems my code gets stuck in the for loops. the LED fades on turn off and repeats itself over and over again. Also, I will try to use the paste code function next time. I am new to the forum so I didn't know that function even existed. any ideas on this new problem?

I fixed my code

So, you need to post it again.

Post your new code, please. Use the auto format first, though. Control t in the IDE or tools, auto format.

I was guessing where the two trailing }}'s I put in my last quote would go.

Take a look at this:

if(analogRead(photo)<thresholdval || activate == 1){
activate = 1;

digitalWrite(uvled,HIGH);
digitalWrite(fireled1,LOW);
digitalWrite(fireled2,LOW);
for(fadeblue = 0; fadeblue <= 255; fadeblue += 1){
for(fadegreen = 0; fadegreen <= 255; fadegreen += 1){
 analogWrite(blue,fadeblue);
 analogWrite(green,fadegreen);
   delay(5);
 if( fadeblue == 255 && fadegreen == 255){                                
    myservo.write(160);            
    delay(2000);                        
                               
    myservo.write(1);            
    delay(2000);                        
                             
    myservo.write(pos);            
    delay(2000);                        

  delay(5);
 activate =0;
}
}}

I added some equal signs in your for loops, you need that to be there or your fadeblue/fadegreen will never reach 255 which is your if condition: if( fadeblue == 255 && fadegreen == 255)

I also removed some useless brackets and moved the two ending }}'s for the for loops.

here’s my new code. I am self taught (as if it weren’t obvious enough), so I really appreciate the help.

#include <Servo.h>

Servo myservo;  

int thresholdval = 300;
int pos = 80; 
int uvled = 7;
int photo = A0;
int fireled1 = 3;
int fireled2 = 4;
int blue = 11;
int green = 6;
int red = 10;
int fadeblue =0;
int fadegreen=0;
int fadered = 0;
int activate = 0;
int val=0;

void setup(){
  myservo.attach(9);    //attaches servo to pin 9
  pinMode(uvled, OUTPUT);
  pinMode(fireled1, OUTPUT);
  pinMode(fireled2, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);

}
void loop(){
  {                                
    myservo.write(pos);             
    delay(15);                        
  }
  val=analogRead(photo);
  if(photo<thresholdval || activate == 1){
    activate = 1;
    digitalWrite(uvled,HIGH);
    digitalWrite(fireled1,LOW);
    digitalWrite(fireled2,LOW);
    for(fadeblue = 0; fadeblue < 255; fadeblue += 1){
      for(fadegreen = 0; fadegreen < 255; fadegreen += 1){
        analogWrite(blue,fadeblue);
        analogWrite(green,fadegreen);
        delay(5);
      }
    }
    delay(5);
    if( fadeblue == 255 && fadegreen == 255){
      {                                
        myservo.write(160);             
        delay(2000);                        
      } 
      {                                
        myservo.write(1);             
        delay(2000);                        
      } 
      {                                
        myservo.write(pos);             
        delay(2000);                        
      }
      delay(5);
      activate =0;
    }
  }
  else if(photo>thresholdval){
    digitalWrite(uvled,LOW);
    digitalWrite(fireled1,HIGH);
    digitalWrite(fireled2,HIGH);
    for(fadeblue = 255; fadeblue>=1; fadeblue-=1){
      for(fadegreen = 255; fadegreen>=1; fadegreen-=1){
        analogWrite(blue,fadeblue);
        analogWrite(green,fadegreen);
        delay(1);
      }
    }
    digitalWrite(fireled1, LOW);
    delay(15);
  }
}

You don’t say what the code does or if it word as you want, but here is an apparent problem.

val=analogRead(photo);
  if(photo<thresholdval || activate == 1){
and
 else if(photo>thresholdval){

You read the value of the pin A0 and assign the value to the variable val, then never use val. Replace photo (the A0 pin) with val in the if and else.

Doesn’t the proper indent make the code easier to read and follow?
check this out for some tips on style and nooby pitfals to avoid.

I can’t help thinking that all those delay()s will cause problems and that you should be studying the use of millis() for timing as in the Blink Without Delay example and in the demo Several things at a time.

These techniques allow the fading and the moving to take place in parallel.

…R

  {                                
    myservo.write(pos);             
    delay(15);                        
  }

What are the curly braces for?

  if(photo<thresholdval || activate == 1){
    activate = 1;

As has been pointed out, the if statement is comparing the wrong values. And, activate is a dumb name. We can’t tell what is being activated, or why we should care.

Put all the { on new lines, and make them line up with the closing bracees.

    for(fadeblue = 0; fadeblue < 255; fadeblue += 1)
    {
      for(fadegreen = 0; fadegreen < 255; fadegreen += 1)
      {
        analogWrite(blue,fadeblue);
        analogWrite(green,fadegreen);
        delay(5);
      }
    }
      {                                
        myservo.write(160);             
        delay(2000);                        
      } 
      {                                
        myservo.write(1);             
        delay(2000);                        
      } 
      {                                
        myservo.write(pos);             
        delay(2000);                        
      }

More useless, distracting, curly braces.