Combining Codes

I’m trying to combine one code that flashes an RGB LED and one that controls a regular LED via a photocell.

Not sure if it’s possible, I’m very much a newbie when it comes to this, I tried a few things but none of them worked, these are the codes:

int sensorPin = 0;    			// select the input pin for the photocell
int sensorValue = 0;  			// variable to store the value coming from the photocell
int LEDpin = 8;					//LED Pin is on the Digital i/o pin number 8

void setup() {
  Serial.begin(9600);      //Set baud rate to 9600 on the Arduino
  pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8
}

void loop() {

  sensorValue = analogRead(sensorPin);  //get the value from input pin
  Serial.println(sensorValue);  //print the value to Serial monitor
  delay(2000);

      if (sensorValue < 300) //if there is darkness then turn led on

      {
        digitalwrite(LEDpin,HIGH);
      }
      else
      {
       digitalwrite(LEDpin, LOW);  //else, keep the led off
      }

}
int redpin=9;      //Pin 9
int greenpin=10;    //Pin 10
int bluepin=11;      //Pin 11
int var=0;
int var1=0;


void setup()
{
}

void loop()
{
  for(var=250;var<255;var++)
  {
    analogWrite(redpin,var);  //RED
    analogWrite(greenpin,0);
    delay(500);
    analogWrite(redpin,0);    //GREEN
    analogWrite(greenpin,var);
    delay(500);
    analogWrite(greenpin,0);   //BLUE
    analogWrite(bluepin,var);
    delay(500);
    analogWrite(bluepin,0);
    delay(500);
  }
  
  for(var1=250;var1<255;var1++)
  {
    analogWrite(redpin,var1);   //YELLOW
    analogWrite(greenpin,var1);
    delay(500);
    analogWrite(redpin,0);
    delay(500);
    analogWrite(greenpin,var1);  //CYAN
    analogWrite(bluepin,var1);
    delay(500);
    analogWrite(greenpin,0);
    delay(500);
    analogWrite(bluepin,var1);    //MAGENTA
    analogWrite(redpin,var1);
    delay(500);
    analogWrite(bluepin,0);
    delay(500);
    analogWrite(bluepin,var1);
    analogWrite(redpin,var1);
    analogWrite(greenpin,var1);
  }
  
}

what do you want the code to do? and what is happening with your code rite now so that we could help you.

It would be helpful to see what you have tried. Can you please post your combined code ? You will almost certainly need to stop using delay() because it holds up the program and will not let anything else happen until the delay finishes. Have a look at this http://arduino.cc/en/Tutorial/BlinkWithoutDelay for some ideas.

I want the code to go through the RGB LED colors as it does in the second code, but I only want it to do it when the photocell senses no light, as in the first code.

I don’t have any code right now because I tried so many things it got all messed up.

I did try something like this:

int LEDpin = 9;					//LED Pin is on the Digital i/o pin number 8
int LEDpin2 = 10;
int LEDpin3 = 11;
void setup() {
  Serial.begin(9600);      //Set baud rate to 9600 on the Arduino
  pinMode(LEDpin, OUTPUT);
  pinMode(LEDpin2, OUTPUT); //set the LED pin as an output on digital i/o pin 8
  pinMode(LEDpin3, OUTPUT);
}

Does this work?

int sensorPin = 0;    			// select the input pin for the photocell
int sensorValue = 0;  			// variable to store the value coming from the photocell
int LEDpin = 8;	
int redpin=9;      //Pin 9
int greenpin=10;    //Pin 10
int bluepin=11;      //Pin 11
int var=0;
int var1=0;				//LED Pin is on the Digital i/o pin number 8

void setup() {
  Serial.begin(9600);      //Set baud rate to 9600 on the Arduino
  pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8
}

void loop() {

  sensorValue = analogRead(sensorPin);  //get the value from input pin
  Serial.println(sensorValue);  //print the value to Serial monitor
  delay(2000);

      if (sensorValue < 300) //if there is darkness then turn led on

      {
        digitalwrite(LEDpin,HIGH);
        
        for(var=250;var<255;var++)
  {
    analogWrite(redpin,var);  //RED
    analogWrite(greenpin,0);
    delay(500);
    analogWrite(redpin,0);    //GREEN
    analogWrite(greenpin,var);
    delay(500);
    analogWrite(greenpin,0);   //BLUE
    analogWrite(bluepin,var);
    delay(500);
    analogWrite(bluepin,0);
    delay(500);
  }
  
  for(var1=250;var1<255;var1++)
  {
    analogWrite(redpin,var1);   //YELLOW
    analogWrite(greenpin,var1);
    delay(500);
    analogWrite(redpin,0);
    delay(500);
    analogWrite(greenpin,var1);  //CYAN
    analogWrite(bluepin,var1);
    delay(500);
    analogWrite(greenpin,0);
    delay(500);
    analogWrite(bluepin,var1);    //MAGENTA
    analogWrite(redpin,var1);
    delay(500);
    analogWrite(bluepin,0);
    delay(500);
    analogWrite(bluepin,var1);
    analogWrite(redpin,var1);
    analogWrite(greenpin,var1);
  }      
      }
      else
      {
       digitalwrite(LEDpin, LOW);  //else, keep the led off
      }
}

It does not.

There isn't anything on Digital i/o pin number 8.

The photocell is on Analog 1, and the RGB LED is on 9,10,11.

I was also wondering, is there some way to control the ground to the RGB LED instead of trying to do this via software?

So in other words the photocell could somehow control the ground, while the RGB setup remains the same as far as hardware and software, so all that changes is that when it’s dark the photocell would trigger the ground which would activate the RGB LED circuit.

PM5K: I was also wondering, is there some way to control the ground to the RGB LED instead of trying to do this via software?

So in other words the photocell could somehow control the ground, while the RGB setup remains the same as far as hardware and software, so all that changes is that when it's dark the photocell would trigger the ground which would activate the RGB LED circuit.

Do it all in software. Much easier to make changes.

Please can you describe exactly what you are trying to do with the combined sketches and what is connected to which pin on the Arduino ? Do you want/need the LED from sketch 1 to turn on and off or just the RGB ones ?

you could make it as u want to but we could suggest rite ok heres the thing what you want is to make state

pseudo code

if photosensor reading higher/lower(depending of your sensor reading) then set point fade colour blue fade colour green fade colour re else turn off all led

I want to take the first code which powers a LED on PIN 8 depending on input from a photocell.

I want to use the photocell, but I want to use it to run the RGB LED in the second code, and not the LED that was on PIN 8 in the first code.

The RGB LED is on digital 9,10,11. The photocell is on analog 0.

Actually Nick your code does work.

I had the ground for the photocell wrong, and had to change digitalwrite to digitalWrite. I also had to remove the second set of colors, otherwise it would cycle to white and stay on regardless of the photocell.

I was going to suggest doing it this way. You might like to try it so that can appreciate why it works

OK
(1) Wire up the second example and get it working
(2) Save the sketch with a new name
(3) Copy the declaration of variables at the top of the second sketch into the new sketch so the Arduino will know where to find things.
(4) At very the end of the new sketch, after the last }, paste in all of the code between { and } in the loop() function of the first sketch.
(5) Put “void RGB()” (without the quotes) before what you just pasted in
(6) Put “{” (no quotes) after void RGB() on a new line
(7) Put “}” (no quotes) on a new line after what you pasted in

You have just created a function that will run the RGB sequence
Whenever you want to run the RGB sequence issue the command RGB();

So

(8) Replace

      if (sensorValue < 300) //if there is darkness then turn led on

      {
        digitalwrite(LEDpin,HIGH);
      }
      else
      {
       digitalwrite(LEDpin, LOW);  //else, keep the led off
      }

in your new sketch with

      if (sensorValue < 300) //if there is darkness run the RGB function
         RGB();

      }

What do you want to happen when the lights are on ?

Interesting. I want it to turn off when the lights are on.

heres my code if you wanna try

const uint8_t RedLed= 9;
const uint8_t GreenLed= 10;
const uint8_t BlueLed= 11;
const uint8_t PhotoSensor= A0;
const uint8_t SetValue=300;

void setup()
{
 pinMode(RedLed,OUTPUT);
 pinMode(GreenLed,OUTPUT);
 pinMode(BlueLed,OUTPUT);
}

void loop()
{
  if (analogRead(PhotoSensor)>SetValue)
  {
    for(int a=0;a<=255;a++)
    {
     for(int b=0;b<=255;b++)
     {
      for(int c=0;c<=255;c++)
      {
        analogWrite(RedLed,a);
        analogWrite(GreenLed,b);
        analogWrite(BlueLed,c);
      }
     }
    }
     for(int a=255;a>=0;a--)
    {
     for(int b=255;b>=0;b--)
     {
      for(int c=255;c>=0;c--)
      {
        analogWrite(RedLed,a);
        analogWrite(GreenLed,b);
        analogWrite(BlueLed,c);
      }
     }
    }
  }
  else
  {
   digitalWrite(RedLed,LOW);
   digitalWrite(GreenLed,LOW);
   digitalWrite(BlueLed,LOW);
  }
}

PM5K: Interesting. I want it to turn off when the lights are on.

Ash has provided a solution. My preference would be to create a new function to turn the LEDs off and call it when you need to. I prefer this because you can give the functions names that mean something, can use them several times in a program if you need to and changes can be made in one place rather than searching for where to make changes.

void LEDSoff()     //turn off the RGB LEDs
{
   digitalWrite(RedLed,LOW);
   digitalWrite(GreenLed,LOW);
   digitalWrite(BlueLed,LOW);
}

Another thing to think about. When the light comes on should the RGB sequence stop immediately or carry on until it is finished ?

i made the code as simple as i could. the only thing that he need to change is to the declaration on the top to make it work. other then that i think its the same as his spec i think

Ash - I am sure that your code works but I still prefer to use functions for the reasons that I gave even if a function is only called once. The problem is not writing the code but maintaining and changing it, particularly if that is not being done by the original author. That particularly relates to function names.

Seeing a function called RGB would indicate to me that it does something with the RGB LEDs whereas the in-line code says nothing about what is going on and it can be difficult to see where in the code a set of actions is complete. I should probably have called the function RGBsequence for even better readability. You can use comments to help understand what is going on but by using a function it can easily be tested on its own unlike the in-line code. Once you have a function that works it can be treated as a black box. That is, of course, how the intrinsic Arduino functions work.

There is a school of thought that says that each function, including loop(), should be no longer than one screen long in order to aid readability but that may be going too far !

ye UKHeliBob.... i truly believe in what you say, i just want to give a working code though i forget to add in comment so that the user can understand what it does. urm yes function would make it a better and easier to understand.

wow theres a flaw in my code

I noticed that. I'm looking now to see what it is.

It doesn't seem to stop when the photocell senses light, and it doesn't flash all of the colors.

I'm a total noob so I've just been playing around with the variables and just going over the code. To me what would be really interesting is if it would be possible to randomize the color output each time the photocell turns the RGB LED on. I'm sure that's possible, but I'd bet the code may be a lot more complex.

I do like how for whatever reason the RGB LED seems to fade with your code rather than just flash between colors.