Question on code for Servo and RGB LED

My team is working on a mechanical bloom like this here:

We 3d printed the parts and instead of a base, we have a large clear acrylic box to hold the board, servo and breadboard.

The wiring is a combination of this tutorial for the RGB LED straight to the arduino board:

and this wiring for the Servo:

Here is the code we’re working with ( I just combined the code from the RGB tutorial and the Servo tutorial) When I run it through verify in the arduino program, there are no errors.

The problem is that when I run it to the set up the servo moves back and forth once and then the LED just stays red. Its like it gets stuck on the red code and doesnt continue through the rest of the code.

// Full Color LED fader 
// Written by Dain Unicorn based on code by EgoPlast 


#include <Servo.h>


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


int pos = 0;    // variable to store the servo position


byte RedLED =10; 
byte BluLED = 3;
byte GrnLED = 6;  
int  RedFade;
int  BluFade;
int  GrnFade;




void setup () {
 pinMode (12, OUTPUT); 
 pinMode (RedLED, OUTPUT);
 pinMode (BluLED, OUTPUT);
 pinMode (GrnLED, OUTPUT);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}




 void loop () {
  digitalWrite(12,HIGH); // Enable the circuit
  analogWrite(RedLED, 255); //Turns off the RED Element
  analogWrite(GrnLED, 255); //Turns off the GREEN Element
  analogWrite(BluLED, 255); //Turns off the BLUE Element
  delay(20);


  for (pos = 10; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position
 
 }
  for (pos = 90; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position
  
// Red Element fade


  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  }
  
// Green Element Fade  
  
  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  }
  
// Blue Element Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  
// Red+Green Elements Fade



  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 


// Green+Blue Elements Fade


  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 



// Blue+Red Elements Fade

  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 


// All Elements Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue =100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  }  
 
}}

I attached an image of the wiring. We tested just the LED code and Just the Servo code and they both worked interdependently so I don’t think its a wiring issue… although I do see how there is a lot of points of failure so well probably change the way its wired eventually.

Thank you to anyone who has stayed with me through this post! This is my first project and I am still learning.

IMG_0098.JPG

  analogWrite(RedLED, 255); //Turns off the RED Element
  analogWrite(GrnLED, 255); //Turns off the GREEN Element
  analogWrite(BluLED, 255); //Turns off the BLUE Element

how is 255 turning things off?

for (pos = 90; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position

why is the comment 180° to 10 and the code form 90 to 10??

where is the closing bracket for this? (hint use ctrl-T to indent your code properly and you'll see that it's not where you think it is...)

I'm not sure why 255 would turn it off but it does. That was based off the code from this tutorial.

When I tested just this code, it worked. It would turn the light off between each color.

For the 2nd section of code, the comment is old, its left over from where I got the code from. The actual degree change needs to be smaller to get the arm to go slight up and down, just enough for the bloom.

I'm not sure where the bracket is. When I did verify, there wasnt any errors. I can try adding one in and see if that fixes things?

Thank you for looking over my question! I appreciate any help.

I'm not sure where the bracket is. When I did verify, there wasnt any errors. I can try adding one in and see if that fixes things?

well you did not verify this... you have the right amount of brackets but for this one it is at the very end of your code....

Also in your picture, I see only one resistor? what type of LED do you have?

Are you aware that on boards other than the Mega, use of the Servo library disables analogWrite() (PWM) functionality on pins 9 and 10 ?

255 works because the leds are common anode.

--Michael

Ahhh!!! I see, so we should move the bracket and test to see if that helps. Thank you!

Also, I didn’t know that about pins 9 and 10, we can move our red LED to a different pin and see if that works too.

Thank you everyone!

I think I fixed the wrong pin error and the bracket error. Heres the code I tried:

// Full Color LED fader 
// Written by Dain Unicorn based on code by EgoPlast 


#include <Servo.h>


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


int pos = 0;    // variable to store the servo position


byte RedLED =7; 
byte BluLED = 3;
byte GrnLED = 6;  
int  RedFade;
int  BluFade;
int  GrnFade;




void setup () {
 pinMode (12, OUTPUT); 
 pinMode (RedLED, OUTPUT);
 pinMode (BluLED, OUTPUT);
 pinMode (GrnLED, OUTPUT);
  
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object
}




 void loop () {
  digitalWrite(12,HIGH); // Enable the circuit
  analogWrite(RedLED, 255); //Turns off the RED Element
  analogWrite(GrnLED, 255); //Turns off the GREEN Element
  analogWrite(BluLED, 255); //Turns off the BLUE Element
  delay(20);


 
  for (pos = 75; pos <= 135; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position
 
 }
  for (pos = 135; pos >= 75; pos -= 1) { // 
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position
  }


// Red Element fade


  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  }
  
// Green Element Fade  
  
  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  }
  
// Blue Element Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  
// Red+Green Elements Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    delay(20);                            
  } 




// Green+Blue Elements Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 




// Blue+Red Elements Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 








// All Elements Fade




  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  } 
  
  for(int fadeValue =100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    analogWrite(GrnLED, fadeValue);         
    analogWrite(BluLED, fadeValue);         
    delay(20);                            
  }  }

It moved the servo arm back and forth once and then the lights went through all the iterations! But once the lights started the servo stopped. I think I’m getting some where with all your help, im not sure how to run the servo and the lights.

Should I put a servo loop inbetween each light section?

Yes with that program you are only working with one or the other at any given time.

For example:

  for(int fadeValue = 255 ; fadeValue >= 100; fadeValue -=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  } 
  for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=2) { 
    analogWrite(RedLED, fadeValue);         
    delay(20);                            
  }

These two for loops take about 3 seconds to complete. You don’s have any statements in there pertaining to the servo so I wouldn’t expect the servo to do anything during that time.

If you want these things to work at the same time instead of sequentially then you need to program them that way. Get rid of all the for loops and delay calls and learn to write non-blocking code. See the Blink Without Delay example for some inspiration on how to handle timing without delay. Take advantage of the fact that the loop function repeats. You don’t need a for loop that fades all the way up or down at once, you can just take one step of fade or one step with the servo per pass of loop and let loop repeat and take another single step. There is a thread Demonstration Code for Several Things at the Same Time that explains a little more about writing non-blocking code.