New to arduino need some help running a RGB LED and a servo at the same time

I edited a program (not very great, but it works) the only thing is only thing is i cant get The light and the servo to work in sync ( i made 2 functions "showSpectrum" and "servoTest") the code is below can anyone assist me, its for a school project i was assigned to.

const int RED_PIN = 6;
const int GREEN_PIN = 5;
const int BLUE_PIN = 3;

int DISPLAY_TIME = 100;
#include <Servo.h> // servo library

Servo servo1; // servo control object

void setup()
{

servo1.attach(9);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

}

void loop()
{
showSpectrum();
servoTest();

}

void showSpectrum()
{
int x;
for (x = 0; x < 768; x++)
{
showRGB(x); // Call RGBspectrum() with our new x
delay(10); // Delay for 10 ms (1/100th of a second)
}
}

// The "base" numbers are:
// 0 = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;

if (color <= 255) // zone 1
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color; // green goes from off to on
blueIntensity = 0; // blue is always off
}
else if (color <= 511) // zone 2
{
redIntensity = 0; // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256); // blue off to on
}
else // color >= 512 // zone 3
{
redIntensity = (color - 512); // red off to on
greenIntensity = 0; // green is always off
blueIntensity = 255 - (color - 512); // blue on to off
}

analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}

void servoTest()
{
int position;

for(position = 0; position < 180; position += 2)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}

// Tell servo to go to 0 degrees, stepping by one degree

for(position = 180; position >= 0; position -= 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}

You have 768 iterations in one function, and 270 iterations in the other.
What do you want to do?

Please remember to use code tags when posting code.

void loop()
{
  showSpectrum(); 
  servoTest();
}

have you learned yet how to do either of these functions without blocking for loops and delays?

you will need to for this to work the way you want.

I edited a program (not very great, but it works) the only thing is only thing is i cant get The light and the servo to work in sync ( i made 2 functions "showSpectrum" and "servoTest") the code is below can anyone assist me, its for a school project i was assigned to.

const int RED_PIN = 6;
const int GREEN_PIN = 5;
const int BLUE_PIN = 3;

int DISPLAY_TIME = 100;  
#include <Servo.h>  // servo library


Servo servo1;  // servo control object



void setup()
{

 servo1.attach(9);
 pinMode(RED_PIN, OUTPUT);
 pinMode(GREEN_PIN, OUTPUT);
 pinMode(BLUE_PIN, OUTPUT);
 
}


//==========================
// This is where i need help VVV
//how can i get both of these things to happen at the same time, 
//what it does now is it will go through the spectrum of colours for the light then it will
//activate the servo, when the servo is done doing its rotations the rgb light changes colour again

void loop()
{
 showSpectrum(); 
 servoTest();



//^^^^^^^^^^^^^^^^^^^^^^^^^

 
}

void showSpectrum()
{
 int x; 
  for (x = 0; x < 768; x++)
 {
   showRGB(x);  // Call RGBspectrum() with our new x
   delay(10);   // Delay for 10 ms (1/100th of a second)
 }
}

// showRGB()
// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.

// The "base" numbers are:
// 0   = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

// Numbers between the above colors will create blends. For
// example, 640 is midway between 512 (pure blue) and 767
// (pure red). It will give you a 50/50 mix of blue and red,
// resulting in purple.

// If you count up from 0 to 767 and pass that number to this
// function, the LED will smoothly fade between all the colors.
// (Because it starts and ends on pure red, you can start over
// at 0 without any break in the spectrum).

void showRGB(int color)
{
 int redIntensity;
 int greenIntensity;
 int blueIntensity;

// Here we'll use an "if / else" statement to determine which
  // of the three (R,G,B) zones x falls into. Each of these zones
  // spans 255 because analogWrite() wants a number from 0 to 255.

  // In each of these zones, we'll calculate the brightness
  // for each of the red, green, and blue LEDs within the RGB LED.



 if (color <= 255)          // zone 1
 {
   redIntensity = 255 - color;    // red goes from on to off
   greenIntensity = color;        // green goes from off to on
   blueIntensity = 0;             // blue is always off
 }
 else if (color <= 511)     // zone 2
 {
   redIntensity = 0;                     // red is always off
   greenIntensity = 255 - (color - 256); // green on to off
   blueIntensity = (color - 256);        // blue off to on
 }
 else // color >= 512       // zone 3
 {
   redIntensity = (color - 512);         // red off to on
   greenIntensity = 0;                   // green is always off
   blueIntensity = 255 - (color - 512);  // blue on to off
 }

 analogWrite(RED_PIN, redIntensity);
 analogWrite(BLUE_PIN, blueIntensity);
 analogWrite(GREEN_PIN, greenIntensity);
}


// To slow down the servo's motion, we'll use a for() loop
  // to give it a bunch of intermediate positions, with 20ms
  // delays between them. You can change the step size to make 
  // the servo slow down or speed up. Note that the servo can't
  // move faster than its full speed, and you won't be able
  // to update it any faster than every 20ms.

  // Tell servo to go to 180 degrees, stepping by two degrees




void servoTest()
{
 int position;
 

 for(position = 0; position < 180; position += 2)
 {
   servo1.write(position);  // Move to next position
   delay(20);               // Short pause to allow it to move
 }

 // Tell servo to go to 0 degrees, stepping by one degree

 for(position = 180; position >= 0; position -= 1)
 {                                
   servo1.write(position);  // Move to next position
   delay(20);               // Short pause to allow it to move
 }
}

BulldogLowell:

void loop()

{
  showSpectrum();
  servoTest();
}




have you learned yet how to do either of these functions without __*blocking*__ for loops and delays?

you will need to for this to work the way you want.

Nope, also I tried looking at the blink without delay post, would implementing this fix the issue!

swiggle:
Nope, also I tried looking at the blink without delay post, would implementing this fix the issue!

yes...

here is an example un-blocking the Servo only. Take a look at how millis() is used to move the servo in increments without a for loop and without delay():

#include <Servo.h>  // servo library

const int RED_PIN = 6;
const int GREEN_PIN = 5;
const int BLUE_PIN = 3;
int DISPLAY_TIME = 100;
Servo servo1;  // servo control object

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.println(F("Let's Go!"));
}

void loop()
{
  //showSpectrum();
  servoTest();
}

void servoTest()
{
  enum{
    EXTENDING,
    RETRACTING,
  }static state = EXTENDING;
  
  const uint16_t interval = 20;
  static uint32_t lastMillis = millis();
  static int position = 0;
  if (millis() - lastMillis > interval)
  {
    lastMillis = millis();
    switch (state)
    {
      case EXTENDING:
        position+=2;
        if (position > 180)
        {
          position = 180;
          state = RETRACTING;
          return;
        }
        Serial.println(position);
        servo1.write(position);
        break;
      case RETRACTING:
        position-=1;
        if (position < 0)
        {
          position = 0;
          state = EXTENDING;
          return;
        }
        Serial.println(position);
        servo1.write(position);
        break;
    }
  }
}

void showSpectrum()
{
  int x;
  for (x = 0; x < 768; x++)
  {
    showRGB(x);  // Call RGBspectrum() with our new x
    delay(10);   // Delay for 10 ms (1/100th of a second)
  }
}

// showRGB()
// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.

// The "base" numbers are:
// 0   = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

// Numbers between the above colors will create blends. For
// example, 640 is midway between 512 (pure blue) and 767
// (pure red). It will give you a 50/50 mix of blue and red,
// resulting in purple.

// If you count up from 0 to 767 and pass that number to this
// function, the LED will smoothly fade between all the colors.
// (Because it starts and ends on pure red, you can start over
// at 0 without any break in the spectrum).

void showRGB(int color)
{
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  // Here we'll use an "if / else" statement to determine which
  // of the three (R,G,B) zones x falls into. Each of these zones
  // spans 255 because analogWrite() wants a number from 0 to 255.

  // In each of these zones, we'll calculate the brightness
  // for each of the red, green, and blue LEDs within the RGB LED.



  if (color <= 255)          // zone 1
  {
    redIntensity = 255 - color;    // red goes from on to off
    greenIntensity = color;        // green goes from off to on
    blueIntensity = 0;             // blue is always off
  }
  else if (color <= 511)     // zone 2
  {
    redIntensity = 0;                     // red is always off
    greenIntensity = 255 - (color - 256); // green on to off
    blueIntensity = (color - 256);        // blue off to on
  }
  else // color >= 512       // zone 3
  {
    redIntensity = (color - 512);         // red off to on
    greenIntensity = 0;                   // green is always off
    blueIntensity = 255 - (color - 512);  // blue on to off
  }

  analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
  analogWrite(GREEN_PIN, greenIntensity);
}

BulldogLowell:
yes...

here is an example un-blocking the Servo only. Take a look at how millis() is used to move the servo in increments without a for loop and without delay():

#include <Servo.h>  // servo library

const int RED_PIN = 6;
const int GREEN_PIN = 5;
const int BLUE_PIN = 3;
int DISPLAY_TIME = 100;
Servo servo1;  // servo control object

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.println(F("Let's Go!"));
}

void loop()
{
  //showSpectrum();
  servoTest();
}

void servoTest()
{
  enum{
    EXTENDING,
    RETRACTING,
  }static state = EXTENDING;
 
  const uint16_t interval = 20;
  static uint32_t lastMillis = millis();
  static int position = 0;
  if (millis() - lastMillis > interval)
  {
    lastMillis = millis();
    switch (state)
    {
      case EXTENDING:
        position+=2;
        if (position > 180)
        {
          position = 180;
          state = RETRACTING;
          return;
        }
        Serial.println(position);
        servo1.write(position);
        break;
      case RETRACTING:
        position-=1;
        if (position < 0)
        {
          position = 0;
          state = EXTENDING;
          return;
        }
        Serial.println(position);
        servo1.write(position);
        break;
    }
  }
}

void showSpectrum()
{
  int x;
  for (x = 0; x < 768; x++)
  {
    showRGB(x);  // Call RGBspectrum() with our new x
    delay(10);  // Delay for 10 ms (1/100th of a second)
  }
}

// showRGB()
// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.

// The "base" numbers are:
// 0  = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

// Numbers between the above colors will create blends. For
// example, 640 is midway between 512 (pure blue) and 767
// (pure red). It will give you a 50/50 mix of blue and red,
// resulting in purple.

// If you count up from 0 to 767 and pass that number to this
// function, the LED will smoothly fade between all the colors.
// (Because it starts and ends on pure red, you can start over
// at 0 without any break in the spectrum).

void showRGB(int color)
{
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

// Here we'll use an "if / else" statement to determine which
  // of the three (R,G,B) zones x falls into. Each of these zones
  // spans 255 because analogWrite() wants a number from 0 to 255.

// In each of these zones, we'll calculate the brightness
  // for each of the red, green, and blue LEDs within the RGB LED.

if (color <= 255)          // zone 1
  {
    redIntensity = 255 - color;    // red goes from on to off
    greenIntensity = color;        // green goes from off to on
    blueIntensity = 0;            // blue is always off
  }
  else if (color <= 511)    // zone 2
  {
    redIntensity = 0;                    // red is always off
    greenIntensity = 255 - (color - 256); // green on to off
    blueIntensity = (color - 256);        // blue off to on
  }
  else // color >= 512      // zone 3
  {
    redIntensity = (color - 512);        // red off to on
    greenIntensity = 0;                  // green is always off
    blueIntensity = 255 - (color - 512);  // blue on to off
  }

analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
  analogWrite(GREEN_PIN, greenIntensity);
}

and how would you add "showSpectrum" into it? would i need to do the same thing you did with the servo?

swiggle:
would i need to do the same thing you did with the servo?

Yes!

try to generate some code using that technique (detailed here) to change the led colors at an interval... no for-loops, no delay().

BulldogLowell:
Yes!

try to generate some code using that technique (detailed here) to change the led colors at an interval... no for-loops, no delay().

Would you mind doing it for me, id do if myself eventually but i need to get this done and ive already spent a day and a half building and trying to figure this code out, if you wouldn't mind id appreciate it alot

its for a school project i was assigned to.

swiggle:
Would you mind doing it for me, id do if myself eventually but i need to get this done and ive already spent a day and a half building and trying to figure this code out, if you wouldn't mind id appreciate it alot

Well, I did half of it already, you should turn in your own work. I already have my degree :wink:

If at first you don't succeed, try, try again!

BulldogLowell:
Well, I did half of it already, you should turn in your own work. I already have my degree :wink:

If at first you don't succeed, try, try again!

AWOL:

Okay let me explain, it isn't for a grade it isn't for anything special, i don't have to do it,

its a christmas tree ornament competition at my tech school, im a first year robotics student(senior in hs),
and we had a plan for this ornament but the idea wont work if i cant get the servo to spin and the rgb light to work, my instructor got the arduino's but he hasn't had time to experiment with them so he said if we used them we would have to do it alone

and im not a coding expert i took 2 years in highschool, but i know enough to understand the basic stuff, in javascript,

swiggle:
Okay let me explain, it isn't for a grade it isn't for anything special, i don't have to do it,

try to put a little effort into it and post your code. I'm sure if you need help you will get it!

Fortunately this is much easier than JavaScript!

:confused:

PS what is the prize for winning?

BulldogLowell:
try to put a little effort into it and post your code. I'm sure if you need help you will get it!

Fortunately this is much easier than JavaScript!

:confused:

PS what is the prize for winning?

I have tried, before i made the post i tried implementing the using millis() for timing but i couldnt get it to work
so i got rid of it thinking it wouldnt work

and the prize is bragging rights for your class

swiggle:
I have tried, before i made the post i tried implementing the using millis() for timing but i couldnt get it to work
so i got rid of it thinking it wouldnt work

and the prize is bragging rights for your class

void showSpectrum()
{
  static uint32_t lastMillis = 0;
  const uint16_t ledIncrement = 10;
  static int16_t x = 0;
  if (millis() - lastMillis > ledIncrement)
  {
    lastMillis = millis();
    showRGB(x++);  // Call RGBspectrum() with our new x
    if (x >= 768)
      x = 0;
  }
}

Merry Christmas