Go Down

Topic: Combining Codes (Read 1 time) previous topic - next topic

PM5K

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:

Code: [Select]
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
      }

}


Code: [Select]
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);
  }
 
}

ash901226

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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PM5K

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:

Code: [Select]
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);
}

Nick_Pyner

Does this work?

Code: [Select]
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
      }
}

Go Up