Trouble getting tri color LED and servo working independantly

Hello… I’m a little bit new to programming with the arduino… and a bit rusty at programming in general…

i would like to have a tri color LED that slowly cycles through all the colors

and while this is happening i would like to control 1 or 2 servos at the same time, each independantly operated according to an algorithm.

making the LED go through all the colors was easy, but once i tried hooking up a servo and making it work with it, both the LED and the servo wouldn’t work right…

the LED would just blink on once, then go off completely, and the servo would twitch in place…

i changed the code a little bit and now its completely not working right… the servo works a little better now, but still not correctly… still very twitchy, and now the light just flashes all the colors seemingly randomly…

I would like all of this to be very smooth… gradual color changes, and very slow servo movement… if anyone can point me in the right direction, i would greatly appreciate it.

P.S. please bare with me, as i know my coding is probably horrible and really inefficient. I know this, and i’m currently seeking counseling for it. just kidding… but seriously, my code sucks. don’t hate me.

#include <Servo.h>
Servo myservo;
int pos = 0;

const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 6;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 17;
const int greenTime = 73;
const int blueTime = 127;
const int redTime = 167;

const int DISPLAY_TIME = 333;

void setup() {
  myservo.attach(6);
 }

void loop() {
  time = millis();
  boolean aaa = true;
  
  if ((time % servoTime ==0) && (aaa =true))
  {
   for(pos = 0; pos < 180; pos += 1)
  {
   myservo.write(pos);
   if (pos ==180)
   {
     aaa = false;
   }
   }
  if ((time % servoTime ==0) && (aaa = false)){
    
 for(pos = 180; pos>=1; pos -=1)
{
  myservo.write(pos);
  if (pos ==1)
  {
    aaa = true;
  }
} 
  }
  }
  
  if (time % greenTime ==0)
 { 
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    
  }
 }
 if (time % blueTime ==0)
 {
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    
  }
 }
 if (time % redTime ==0)
 {
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    
  }
 }
}

Servos use timers. Some number of servos can be driven using a single timer. Each timer that is used disables PWM on 2 pins.

Since you are using one servo and 3 PWM pins, you should still be able to accomplish your goal.

Your assignment is to figure out which PWM pins are affected when a servo instance is created, and to not use those PWM pins. If I remember correctly, the first PWM pins affected are 9 and 10.

You do not need to be using one of the PWM pins for the servo. Servos are not conrolled using PWM.

thanks for the advice… i changed my code a bit because i was tired of everything being all jittery… i commented out some parts of the script and it still didnt work… the only time it started to work right is when i commented out the “myservo.attach(7);” in the ‘void setup()’ function… once i got rid of that one line of code, my LED started working right…

but does this mean i can’t use a tri colored LED and a servo at the same time? what gives?

thanks for your help!

here is my updated code:

#include <Servo.h>
Servo myservo;
int pos = 0;

const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 100;
const int greenTime = 200;
const int blueTime = 300;
const int redTime = 400;

const int DISPLAY_TIME = 333;

void setup() {
  /* myservo.attach(7); */
 }

void loop() {
  /*time = millis();
  boolean aaa = true;
  
   for(pos = 0; pos < 180; pos += 1)
  {
   myservo.write(pos);
   
   }
  
    
 for(pos = 180; pos>=1; pos -=1)
{
  myservo.write(pos);
  
} 
  
  
  */ 
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    delay(DISPLAY_TIME);
  }
 
}

Yes, you can use the servo and the RGB LED at the same, just not using pins 9 and 10.

i changed the pins of the leds from 9 and 10 and moved them over to 5 and 6, and changed the code…

ALAS, the led now works perfectly… but now when i try to incorporate the servo movement… the servo is jerky still…

#include <Servo.h>
Servo myservo;
int pos = 0;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 100;
const int greenTime = 200;
const int blueTime = 300;
const int redTime = 400;

const int DISPLAY_TIME = 333;

void setup() {
   myservo.attach(7); 
 }

void loop() {
  time = millis();
  boolean aaa = true;
  
   for(pos = 0; pos < 180; pos += 1)
  {
   myservo.write(pos);
   
   }
  
    
 for(pos = 180; pos>=0; pos -=1)
{
  myservo.write(pos);
  
} 
  
  
  
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    delay(DISPLAY_TIME);
  }
 
}

i’m also interested in why the LEDS will work on pins 5,6,and 11… but not 9 and 10…
thanks again for your help!

i’m also interested in why the LEDS will work on pins 5,6,and 11… but not 9 and 10

Re-read reply #1. I told you why.

   for(pos = 0; pos < 180; pos += 1)
  {
   myservo.write(pos);
   
   }

How long do you think that this for loop will take to execute? myservo.write() is not a blocking function. You are not giving the servo time to get to a position before telling it to get moving again.

Jerky servo motion is generally caused by three things - expecting instantaneous reaction from the servo, as you are doing here, driving the servo beyond its limits, and under-powering the servo.

How ARE you powering the servo?

PaulS: How long do you think that this for loop will take to execute? myservo.write() is not a blocking function. You are not giving the servo time to get to a position before telling it to get moving again.

Jerky servo motion is generally caused by three things - expecting instantaneous reaction from the servo, as you are doing here, driving the servo beyond its limits, and under-powering the servo.

How ARE you powering the servo?

Good point, i hadn't considered that. that seems to explain the jerkiness... not giving the servo time to move...

as far as power, i'm using a micro servo, its very small and came with an arduino starter kit. its powered by 5v current from arduino.

also, i re-read your previous posts and the servo timers and PWM pin thing totally makes sense...

In retrospect, i suppose i need to change the 'while' function...

i tried to change it to something that gives it more time to function, but i think i'm still off somehow...

boolean servoSwitch = true;
  
  if(servoSwitch == true)
  {
   myservo.write(0);
   delay(1000);
   servoSwitch = false;
 }
 else
 {
   myservo.write(180);
   delay(1000);
   servoSwitch = true;
 }

Code snippets don't cut it. We can't see where the variables are defined relative to where they are used.

heres the whole code

#include <Servo.h>
Servo myservo;
int pos = 0;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 100;
const int greenTime = 200;
const int blueTime = 300;
const int redTime = 400;

const int DISPLAY_TIME = 333;

void setup() {
   myservo.attach(7); 
 }

void loop() {
  boolean servoSwitch = true;
  
  if(servoSwitch == true)
  {
    myservo.write(0);
    delay(1000);
    servoSwitch = false;
  }
  else
  {
    myservo.write(180);
    delay(1000);
    servoSwitch = true;
  }
    
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    delay(DISPLAY_TIME);
  }
 
}

If you want the Arduino to control more than one thing at a time, you can't afford to just delay() the loop() while something happens. Look at the standard BlinkWithoutDelay example sketch to see how to time things without using delay().

PeterH:
If you want the Arduino to control more than one thing at a time, you can’t afford to just delay() the loop() while something happens. Look at the standard BlinkWithoutDelay example sketch to see how to time things without using delay().

I checked out that example and based on that i rewrote some of the code, but its still not working right…

#include <Servo.h>
Servo myservo;
int pos = 0;
long previousMillis = 0;
long interval = 1000;
long interval2 = 500;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 100;
const int greenTime = 200;
const int blueTime = 300;
const int redTime = 400;

const int DISPLAY_TIME = 333;

void setup() {
   myservo.attach(7); 
 }

void loop() {
  boolean servoSwitch = true;
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    if(servoSwitch = true)
    {
      myservo.write(0);
      servoSwitch = false;
    }
  }
  if(currentMillis - previousMillis > interval2)
  {
    if(servoSwitch = false)
    {
      myservo.write(180);
      servoSwitch=true;
    }
  }
  
  
  
  
  
     
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    delay(DISPLAY_TIME);
  }
 
}

after that, it still didnt work, so i went ahead and rewrote all the code so that there is no delay() in the loop().

#include <Servo.h>
Servo myservo;
int pos = 0;
long previousMillis = 0;
long interval = 1000;
long interval2 = 500;

long redMillis = 3;
long greenMillis = 5;
long blueMillis = 7;


const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

unsigned long time;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int servoTime = 100;
const int greenTime = 200;
const int blueTime = 300;
const int redTime = 400;

const int DISPLAY_TIME = 333;

void setup() {
   myservo.attach(7); 
 }

void loop() {
  boolean servoSwitch = true;
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval2)
  {
    myservo.write(0);
  }
    
  if((currentMillis - previousMillis > interval) && currentMillis >=500)
  {
      myservo.write(180);
      currentMillis = 0;
   
  }
  if(currentMillis % greenMillis == 0)
  {
    greenIntensity =+1;
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
  }
  if(currentMillis % blueMillis == 0)
  {
    blueIntensity +=1;
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
  }
  if(currentMillis % redMillis == 0)
  {
    redIntensity +=1;
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
  }
}
  
    
    
  
  
/*  
  
     
  for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1) {
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) {
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
    delay(DISPLAY_TIME);
  }
 
 
  for (redIntensity =0; redIntensity <= 255; redIntensity +=1) {
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
    delay(DISPLAY_TIME);
  }
 
}
  
  */

it seems to be working even less… the light is beginning to bug out again.

i’m really happy that you guys aren’t being really obvious and letting me draw my own conclusions and inferences…
…but i feel like there is an essential thing that i’m overlooking as far as my implimentation.

i truly appreciate you giving me the space to learn from my own mistakes and experiences… but i’m beginning to get a little discouraged by the seemingly daunting nature of what is probably alot simpler than i’m making it out to be.

i’d appreciate some instant karma here, or maybe a swift kick to the cerebellum to jump start my circuitry.

it seems to be working even less... the light is beginning to bug out again.

Highly technical terms like "bug out" put some people off. Perhaps you could explain in simpler terms what that means.

You might also explain the relationship between what the servo is doing and what the LED is doing, if there is one.

Using millis(), you are trying to implement a state machine. At any given time, you are in one state, and it may, or may not, be time to move to a different state. If it is, you need to change something, and reset the time that the last change occurred. It may be necessary to manage several states - one for the servo, one for the red light, one for the green light, and one for the blue light - rather than trying to rely on one state.

Highly technical terms like "bug out" put some people off. Perhaps you could explain in simpler terms what that means.

sorry about my choice of technical terms...

what i meant to say is the light seems to randomly blink every color possible, rather than slowly flowing through the colors...

also, there doesn't seem to be any relationship between the servo and the LED... if there is, its not apparent to me...

when i push the reset button the servo position goes up to 180 and stays there, sortof jittering...and the LED, rather than slowly cycling through the colors, begins quickly strobing the different color combinations.

It may be necessary to manage several states

I didnt know it was possible for multiple different states could run simultaniously. i thought states had to be run one at a time.

i'm looking at some examples of user defined states and i'm going to see if i can rewrite the code.

ALRIGHT! thanks to your help, i’ve made a bit of headway…

i updated the code to run the servo, and led pins in different states… Here is the code.

#include <Servo.h>
 
Servo myservo;
int pos = 0;
boolean servoSwitch = true;

long previousMillis = 0;
long interval = 1000;

int redMillis = 3;
int greenMillis = 5;
int blueMillis = 7;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void setup()
{
  myservo.attach(SERVO_PIN);
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis % 1000 == 0)
  {
    servo();
  }
  if(currentMillis % redMillis == 0)
  {
    redLed();
  }
  if(currentMillis % blueMillis == 0)
  {
    blueLed();
  }
  if(currentMillis % greenMillis == 0)
  {
    greenLed();
  }
}

void servo()
{
  if(servoSwitch == true)
  {
    myservo.write(180);
    servoSwitch = false;
  }
  else
  {
   myservo.write(0);
  servoSwitch = true;
  }
  
}

void redLed()
{
    redIntensity +=1;
    blueIntensity = 255-redIntensity;
    analogWrite(RED_LED_PIN, redIntensity);
    analogWrite(BLUE_LED_PIN, blueIntensity);
}

void blueLed()
{
    blueIntensity +=1;
    greenIntensity = 255-blueIntensity;
    analogWrite(BLUE_LED_PIN, blueIntensity);
    analogWrite(GREEN_LED_PIN, greenIntensity);
}

void greenLed()
{
    greenIntensity =+1;
    redIntensity = 255-greenIntensity;
    analogWrite(GREEN_LED_PIN, greenIntensity);
    analogWrite(RED_LED_PIN, redIntensity);
}

right now, the servo is working a little better… it is coded to move back and forth each second, but it is inconsistant in that it sometimes skips a couple seconds

(i’m thinking it might be because it takes a couple milliseconds for the modulus operation to be performed, so sometimes it gets overlooked when the code runs… just an idea)

I’m going to try to rewrite the code so that it checks to see if the millis value is above or below a certain constant, rather than it checking to see if the modulus of the current millis() call is equal to 0…

somehow i feel like i might be leaving something out still though…

also, the LED is now just smoothly and quickly pulsiing the red green and blue lights… its a little smoother than in the previous code, but now there is no color change at all… all three lights seem to be keeping pretty constant color values.

not really sure how to change that… i might try making the intervals that they are changed a bit longer…

if i make any more progress, i’ll post my work, but i would still appreciate any help available.

thank you so much for your time and consideration.

I decided to slow down the rate that the LEDs change…
it appears to be cycling through different colors, but they are very drastic changes from another, not the slow gradual changes i wanted…

it goes from red to blue, to green to yellow,to purple, to red… no seeming order to the color changes…

also,it seems that slowing down the rate the LEDS are updated is making the servo a little more accurate…

I’m thinking that i could make the servo more accurate by replacing the modulus calculation that my code is written to run every millisecond… and replacing it with something that just runs within a certain time frame… like a 10 millisecond slot of time, once every 1000 milliseconds…

i’m pretty certain that should make the servo more accurate…

but as for the LED’s, i changed the code a little bit, and its still not doing the gradual changes… here is the updated code that does what i just described above with the jerky eratic color changes.

#include <Servo.h>
 
Servo myservo;
int pos = 0;
boolean servoSwitch = true;

long previousMillis = 0;
long interval = 1000;

int redMillis = 333;
int greenMillis = 555;
int blueMillis = 777;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void setup()
{
  myservo.attach(SERVO_PIN);
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis % 1000 == 0)
  {
    servo();
  }
  if(currentMillis % redMillis == 0)
  {
    redLed();
  }
  if(currentMillis % blueMillis == 0)
  {
    blueLed();
  }
  if(currentMillis % greenMillis == 0)
  {
    greenLed();
  }
}

void servo()
{
  if(servoSwitch == true)
  {
    myservo.write(180);
    servoSwitch = false;
  }
  else
  {
   myservo.write(0);
  servoSwitch = true;
  }
  
}

void redLed()
{
    for (redIntensity =0; redIntensity <= 255; redIntensity +=1) 
    {
      blueIntensity = 255-redIntensity;
      analogWrite(RED_LED_PIN, redIntensity);
      analogWrite(BLUE_LED_PIN, blueIntensity);
    }
}

void blueLed()
{
    for (blueIntensity =0; blueIntensity <= 255; blueIntensity +=1) 
    {
      greenIntensity = 255-blueIntensity;
      analogWrite(BLUE_LED_PIN, blueIntensity);
      analogWrite(GREEN_LED_PIN, greenIntensity);
    }
}

void greenLed()
{
    for (greenIntensity =0; greenIntensity <= 255; greenIntensity +=1)
    {      
      redIntensity = 255-greenIntensity;
      analogWrite(GREEN_LED_PIN, greenIntensity);
      analogWrite(RED_LED_PIN, redIntensity);
    }
}

I did another rewrite on the code and i got the servo working perfectly… thanks for all the advice on that.

the led is still not working exactly how i want… it is still blinking different colors… but now i have the color values changing at least… but not smoothly.

could anyone take a look at the latest version of my code? i feel like i’m SO CLOSE…

#include <Servo.h>
 
Servo myservo;
int pos = 0;
boolean servoSwitch = true;

long previousMillis = 0;
long interval = 1000;

int redMillis = 123;
int greenMillis = 234;
int blueMillis = 345;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void setup()
{
  myservo.attach(SERVO_PIN);
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis % 500 == 0)
  {
    servo();
  }
  if(currentMillis % redMillis == 0)
  {
    redLed();
  }
  if(currentMillis % blueMillis == 0)
  {
    blueLed();
  }
  if(currentMillis % greenMillis == 0)
  {
    greenLed();
  }
}

void servo()
{
  if(servoSwitch == true)
  {
    int servoA = myservo.read();
    
    myservo.write(servoA +=2);
    if(myservo.read() == 180)
    {
      servoSwitch = false;
    }
  }
  else
  {
   int servoA = myservo.read();
   myservo.write(servoA -=2);
   if(myservo.read() == 40)
   {
   servoSwitch = true;
   }
  }
  
}

void redLed()
{
      redIntensity +=1;
      blueIntensity = 255-redIntensity;
      analogWrite(RED_LED_PIN, redIntensity);
      analogWrite(BLUE_LED_PIN, blueIntensity);
}

void blueLed()
{ 
      blueIntensity +=1;
      greenIntensity = 255-blueIntensity;
      analogWrite(BLUE_LED_PIN, blueIntensity);
      analogWrite(GREEN_LED_PIN, greenIntensity);
}

void greenLed()
{ 
      greenIntensity += 1;
      redIntensity = 255-greenIntensity;
      analogWrite(GREEN_LED_PIN, greenIntensity);
      analogWrite(RED_LED_PIN, redIntensity);
}

EXCELLENT… major breakthrough… got everything working perfectly…

figured out that my code was structured in a way to where my led wasn’t being given enough time to finish a cycle, before being switched to the next one…

once i realized it, i figured out a way to fix it so that it cycles through, then goes to the next led cycle…

i had a feeling that since i wasn’t getting anymore responses to my post, that you guys had already pretty much told me what i would need to know to figure it out… and you had. Thanks for saying what needed to be said, and then giving me space to let me fumble around and figure it out… It was fun, and challenging, and i feel like i understand programming with the arduino a little better now.

THANK YOU.

here is the updated code.

#include <Servo.h>
 
Servo myservo;
int pos = 0;
boolean servoSwitch = true;
int ledSwitch = 0;


long previousMillis = 0;
long currentMillis2 = millis();
long interval = 1000;

int redMillis = 333;
int greenMillis = 555;
int blueMillis = 777;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void setup()
{
  myservo.attach(SERVO_PIN);
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis % 500 == 0)
  {
    servo();
  }
  
  
  /*ledGo();*/
  
  
  if(currentMillis % redMillis == 0 && ledSwitch == 2)
  {
    redLed();
  }
  if(currentMillis % blueMillis == 0 && ledSwitch == 1)
  {
    blueLed();
  }
  if(currentMillis % greenMillis == 0 && ledSwitch ==0)
  {
    greenLed();
  }
  
  
}

void servo()
{
  if(servoSwitch == true)
  {
    int servoA = myservo.read();
    
    myservo.write(servoA +=5);
    if(myservo.read() >= 160)
    {
      servoSwitch = false;
    }
  }
  else
  {
   int servoA = myservo.read();
   myservo.write(servoA -=5);
   if(myservo.read() <= 44)
   {
   servoSwitch = true;
   }
  }
  
}
/*
void ledGo()
{
  if(currentMillis2 % 300 == 0)
  {
    
    for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
        redIntensity = 255-greenIntensity;
        analogWrite(GREEN_LED_PIN, greenIntensity);
        analogWrite(RED_LED_PIN, redIntensity);
        if(greenIntensity == 255)
        {
          ledSwitch = 1;
        }
        } 
    for (blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5) {
        greenIntensity = 255-blueIntensity;
        analogWrite(BLUE_LED_PIN, blueIntensity);
        analogWrite(GREEN_LED_PIN, greenIntensity);
        if(blueIntensity ==255)
        {
        ledSwitch = 2;
        }
      }
    for (redIntensity = 0; redIntensity <= 255; redIntensity+=5) {
        blueIntensity = 255-redIntensity;
        analogWrite(RED_LED_PIN, redIntensity);
        analogWrite(BLUE_LED_PIN, blueIntensity);
        if(redIntensity == 255)
      {
        ledSwitch = 0;
      }
    }
  } 
}
*/


void redLed()
{
  if(currentMillis2 % 20 == 0)
  {
      redIntensity +=1;
      blueIntensity = 255-redIntensity;
      analogWrite(RED_LED_PIN, redIntensity);
      analogWrite(BLUE_LED_PIN, blueIntensity);
      if(greenIntensity == 255)
        {
          ledSwitch = 1;
        }
      
  }

}

void blueLed()
{ 
  if(currentMillis2 % 25 == 0)
  {
      blueIntensity +=1;
      greenIntensity = 255-blueIntensity;
      analogWrite(BLUE_LED_PIN, blueIntensity);
      analogWrite(GREEN_LED_PIN, greenIntensity);
       if(blueIntensity ==255)
        {
        ledSwitch = 2;
        }
  }
}

void greenLed()
{ 
  if(currentMillis2 % 50 == 0)
  {
      greenIntensity += 1;
      redIntensity = 255-greenIntensity;
      analogWrite(GREEN_LED_PIN, greenIntensity);
      analogWrite(RED_LED_PIN, redIntensity);
      if(redIntensity == 255)
      {
        ledSwitch = 0;
      }
      
  }
}

revision: I just watched closer on whats going on with the LED and it seems to go from red to green… but then at the end of that it just goes back to red…

never goes from green to blue, and blue to red…

can someone else maybe take a quick look at my code?

it is coded to move back and forth each second, but it is inconsistant in that it sometimes skips a couple seconds

Not too surprising.

  if(currentMillis % 500 == 0)

If, on one pass, millis() returns 28976546, the modulo result will not be 0, so no servo motion.

Suppose on the next pass, millis() returns 28976575, still no servo motion.
Suppose on the next pass, millis() returns 28976584, still no servo motion.
Suppose on the next pass, millis() returns 28976596, still no servo motion.
Suppose on the next pass, millis() returns 28976603, still no servo motion.

The trick is to record the last time some action occurred (lastServoTime = millis();), then see if now minus then is greater than or equal some interval:

if(millis() - lastServoTime >= servoInterval)
{
   servo();
   lastServoTime = millis();
}

With this approach, there may still be more than servoInterval milliseconds between function calls. That can be avoided by assigning the return from millis() to a variable at the start of loop, and the use that variable throughout loop:

void loop()
{
   unsigned long currMillis = millis();

   if(currMillis - lastServoTime >= servoInterval)
   {
      servo();
      lastServoTime = currMillis;
   }

   // Other stuff...
}

Of course, the issue with the servo affects the LED colors, too.

thanks paul… i just went over the code again and tried to impliment what you said…
it is still going from red to green, then snapping back to red, rather than cycling through the rest of the colors.
i’m putting my mind toward the situation right now, but here is my code so far.

#include <Servo.h>
 
Servo myservo;
int pos = 0;
boolean servoSwitch = true;
int ledSwitch = 0;
long lastServoTime = millis();
long lastLedTime = millis();

long servoInterval = 1000;
long previousMillis = 0;
long currentMillis2 = millis();
long interval = 1000;

int redMillis = 33;
int greenMillis = 55;
int blueMillis = 77;

const int RED_LED_PIN = 5;
const int GREEN_LED_PIN = 6;
const int BLUE_LED_PIN = 11;
const int SERVO_PIN = 7;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void setup()
{
  myservo.attach(SERVO_PIN);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  if(currentMillis -lastServoTime >= servoInterval)
  {
    servo();
    lastServoTime = currentMillis;
  }
  
  
  
  if((currentMillis - lastLedTime >= redMillis) && ledSwitch == 2)
  {
    redLed();
    lastLedTime = currentMillis;
  }
  else if((currentMillis - lastLedTime >= blueMillis) && ledSwitch == 1)
  {
    blueLed();
    lastLedTime = currentMillis;
  }
  else if((currentMillis - lastLedTime >= greenMillis) && ledSwitch ==0)
  {
    greenLed();
    lastLedTime = currentMillis;
  }
  
  
}

void servo()
{
  if(servoSwitch == true)
  {
    int servoA = myservo.read();
    
    myservo.write(servoA +=5);
    if(myservo.read() >= 160)
    {
      servoSwitch = false;
    }
  }
  else
  {
   int servoA = myservo.read();
   myservo.write(servoA -=5);
   if(myservo.read() <= 44)
   {
   servoSwitch = true;
   }
  }
  
}

void redLed()
{
  if(currentMillis2 % 20 == 0)
  {
      redIntensity +=1;
      blueIntensity = 255-redIntensity;
      analogWrite(RED_LED_PIN, redIntensity);
      analogWrite(BLUE_LED_PIN, blueIntensity);
      if(greenIntensity == 255)
        {
          ledSwitch = 1;
        }
      
  }

}

void blueLed()
{ 
  if(currentMillis2 % 25 == 0)
  {
      blueIntensity +=1;
      greenIntensity = 255-blueIntensity;
      analogWrite(BLUE_LED_PIN, blueIntensity);
      analogWrite(GREEN_LED_PIN, greenIntensity);
       if(blueIntensity ==255)
        {
        ledSwitch = 2;
        }
  }
}

void greenLed()
{ 
  if(currentMillis2 % 50 == 0)
  {
      greenIntensity += 1;
      redIntensity = 255-greenIntensity;
      analogWrite(GREEN_LED_PIN, greenIntensity);
      analogWrite(RED_LED_PIN, redIntensity);
      if(redIntensity == 255)
      {
        ledSwitch = 0;
      }
      
  }
}