Need help with combining 2 sketches

Hi There

We have got these 2 sketches working perfectly by themselves but cant seem to get them to work as one sketch.

* LDR and Arduino

*

* Reads an input from LDR, sends it to serial monitor and turns a LED off or on.

*

* Code by Joshua Lopez http://joshualopezarduino.blogspot.com/

*/



// variables

int LDR_pin = 0; // analog pin 0 (connect LDR here)

int LDR_val = 0; // variable use to read input data

int LEDpin = 13; //This is the LED



// a threshold to decide when the LED turns on

int threshold = 500;


void setup(){



// declaration of pin modes

pinMode(LDR_pin, INPUT);

pinMode(LEDpin, OUTPUT);


// begin sending over serial port

Serial.begin(9600);

}



void loop(){

// read the value on analog input

LDR_val = analogRead(LDR_pin);



// if value greater than threshold turn on LED

if (LDR_val < threshold) digitalWrite(LEDpin, HIGH); else digitalWrite(LEDpin, LOW);



// output 'LDR_val' value into the console

Serial.print("LDR = ");

Serial.print(LDR_val);



}

And

// RGB LED - Automatic Color Cycling
//
// Matthew L Beckler
// matthew at mbeckler dot org

int redPin = 11;
int bluePin = 10;
int greenPin = 9;

int redIn = 0;
int greenIn = 1;
int blueIn = 2;

int redVal;
int greenVal;
int blueVal;

void setup()
{
  redVal = 255;
  greenVal = 255;
  blueVal = 255;
  update();
}

// This function updates the LED outputs.
void update()
{
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}

// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int* value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      (*value)--;
    else
      (*value)++;

    update();
    delay(10);
  }
}

void loop()
{
  // start out at black (all off)
  color_morph(&redVal,   1); // transition to red
  color_morph(&greenVal, 1); // transition to yellow
  color_morph(&redVal,   0); // transition to green
  color_morph(&blueVal,  1); // transition to aqua
  color_morph(&redVal,   1); // transition to white
  color_morph(&greenVal, 0); // transition to violet
  color_morph(&redVal,   0); // transition to blue
  color_morph(&blueVal,  0); // transition to black (all off)
}

We would like the RGB Led to run the whole time independently from the LDR part.

If you could please help us that would be great.

Thank you

Even better if you could get it so that the red color of the RGB led comes on when the light sensor is darkened and stays on for a few seconds after light has returned, and RGB led should just cycles through the other colors otherwise.

pinMode(LDR_pin, INPUT);

This doesn't do what you think or want it to do, and could have unexpected effects - remove it.

Here is what we have tried

// variables
int LDR_pin = 0; // analog pin 0 (connect LDR here)

int LDR_val = 0; // variable use to read input data

int LEDpin = 13; //This is the LED

int threshold = 450;// a threshold to decide when the LED turns on

// RGB LED - Automatic Color Cycling
int redPin = 11;
int bluePin = 10;
int greenPin = 9;

int redIn = 0;
int greenIn = 1;
int blueIn = 2;

int redVal;
int greenVal;
int blueVal;

void setup(){
{
  redVal = 255;
  greenVal = 255;
  blueVal = 255;
  update();
}
{
pinMode(LDR_pin, INPUT);// declaration of pin modes

pinMode(LEDpin, OUTPUT);

Serial.begin(9600);// begin sending over serial port
}
}

// This function updates the LED outputs.
void update(){ 
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}
// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int* value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      (*value)--;
    else
      (*value)++;
      
    update();
    delay(10);
  }
}
void loop(){
  {
// read the value on analog input
LDR_val = analogRead(LDR_pin);
// if value greater than threshold turn on LED
if (LDR_val > threshold) digitalWrite(LEDpin, HIGH); else digitalWrite(LEDpin, LOW);
if (LDR_val < threshold) digitalWrite(LEDpin, LOW); else digitalWrite(LEDpin, HIGH);
// output 'LDR_val' value into the console
Serial.print("LDR = ");
Serial.print(LDR_val);
  }
  {
  // start out at black (all off)
  color_morph(&redVal,   1); // transition to red
  color_morph(&greenVal, 1); // transition to yellow
  color_morph(&redVal,   0); // transition to green
  color_morph(&blueVal,  1); // transition to aqua
  color_morph(&redVal,   1); // transition to white
  color_morph(&greenVal, 0); // transition to violet
  color_morph(&redVal,   0); // transition to blue
  color_morph(&blueVal,  0); // transition to black (all off)
}
}

we will try removing pinMode(LDR_pin, INPUT); shortly

You could get rid of unnecessary braces.

void setup()
{
  redVal = 255; // this could be done where "redVal" is declared.
  greenVal = 255; // this could be done where "greenVal" is declared.

  blueVal = 255; // this could be done..I'm sure you get the picture.
  update();

  pinMode(LEDpin, OUTPUT);

  Serial.begin(9600);// begin sending over serial port
}

Hey thanks, we tried that but still nothing. Any more ideas?

Any more ideas?

Yeah, plenty.
How about you tell us what the difference is between what your sketch is doing now, and what you want it to do?
You're the guys with the hardware.

Something from your code you may want to think about (reformatted for easier reading):

if (LDR_val > threshold) 
  digitalWrite(LEDpin, HIGH); 
else
  digitalWrite(LEDpin, LOW);

if (LDR_val < threshold) 
  digitalWrite(LEDpin, LOW); 
else 
  digitalWrite(LEDpin, HIGH);

When we combine the two, The RGB LED works consistently , but the light sensor stops working when it is combined with the RGB code.

We want the RGB LED to work consistently but also be able to use the light sensor as a switch.

What does this do?

const int LDRpin = 0; // analog pin 0 (connect LDR here)
const int LEDpin  = 13; //This is the LED

int threshold = 450;// a threshold to decide when the LED turns on

// RGB LED - Automatic Color Cycling
const int redPin = 11;
const int bluePin = 10;
const int greenPin = 9;

int redVal   = 255;
int greenVal = 255;
int blueVal  = 255;

void setup()
{
  update();
  pinMode(LEDpin, OUTPUT);
  Serial.begin(9600);// begin sending over serial port
}

// This function updates the LED outputs.
void update()
{
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}

// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int& value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      value--;
    else
      value++;

    update();
    delay(10);
  }
}
void loop()
{
  // read the value on analog input
  int LDRval = analogRead(LDRpin);
  // if value greater than threshold turn on LED
  if (LDRval > threshold) 
    digitalWrite(LEDpin, HIGH); 
  else 
    digitalWrite(LEDpin, LOW);

  Serial.print("LDR = ");
  Serial.println(LDRval);

  // start out at black (all off)
  color_morph(redVal,   1); // transition to red
  color_morph(greenVal, 1); // transition to yellow
  color_morph(redVal,   0); // transition to green
  color_morph(blueVal,  1); // transition to aqua
  color_morph(redVal,   1); // transition to white
  color_morph(greenVal, 0); // transition to violet
  color_morph(redVal,   0); // transition to blue
  color_morph(blueVal,  0); // transition to black (all off)
}

Hey still not working. Have a look at this blog. It should give you a better idea of what we are working on.

http://interactivearduino.wordpress.com/wp-admin/

I think it probably is working, just not as fast as you may want it.

have you thought about something like:

void color_morph(int& value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      value--;
    else
      value++;
      
    update();
    //    delay(10);
  // read the value on analog input
  int LDR_val = analogRead(LDR_pin);
  // if value greater than threshold turn on LED
  if (LDR_val > threshold) 
    digitalWrite(LEDpin, HIGH); 
  else 
    digitalWrite(LEDpin, LOW);

  // output 'LDR_val' value into the console
  Serial.print("LDR = ");
  Serial.print(LDR_val);
  }
}

I don't really recommend this exact approach, because different values of "LDR_val" will affect your morph speed, but it's a step in the right direction.

Your right the code above is working there's just around a 15 sec delay between when the LDR is covered and the LED comes on.

const int LDRpin = 0; // analog pin 0 (connect LDR here)
const int LEDpin  = 13; //This is the LED

int threshold = 450;// a threshold to decide when the LED turns on

// RGB LED - Automatic Color Cycling
const int redPin = 11;
const int bluePin = 10;
const int greenPin = 9;

int redVal   = 255;
int greenVal = 255;
int blueVal  = 255;

void setup()
{
  update();
  pinMode(LEDpin, OUTPUT);
  Serial.begin(9600);// begin sending over serial port
}

// This function updates the LED outputs.
void update()
{
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}

// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int& value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      value--;
    else
      value++;

    update();
    delay(10);
  }
}
void loop()
{
  // read the value on analog input
  int LDRval = analogRead(LDRpin);
  // if value greater than threshold turn on LED
  if (LDRval > threshold)
    digitalWrite(LEDpin, HIGH);
  else
    digitalWrite(LEDpin, LOW);

  Serial.print("LDR = ");
  Serial.println(LDRval);

  // start out at black (all off)
  color_morph(redVal,   1); // transition to red
  color_morph(greenVal, 1); // transition to yellow
  color_morph(redVal,   0); // transition to green
  color_morph(blueVal,  1); // transition to aqua
  color_morph(redVal,   1); // transition to white
  color_morph(greenVal, 0); // transition to violet
  color_morph(redVal,   0); // transition to blue
  color_morph(blueVal,  0); // transition to black (all off)
}

How would we go about speeding this up to the speed it was at when the codes weren't merged?

Also in the code you don't recommend

void color_morph(int& value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      value--;
    else
      value++;

    update();
    //    delay(10);
  // read the value on analog input
  int LDR_val = analogRead(LDR_pin);
  // if value greater than threshold turn on LED
  if (LDR_val > threshold)
    digitalWrite(LEDpin, HIGH);
  else
    digitalWrite(LEDpin, LOW);

  // output 'LDR_val' value into the console
  Serial.print("LDR = ");
  Serial.print(LDR_val);
  }
}

it says that LDR_pin wasn't declared in this scope.

it says that LDR_pin wasn't declared in this scope

Call it "LDRpin" then, or whatever it was called at the top of the sketch.

When I said I don't recommend it, I mean it will work, and it will be as quick as you want, but it isn't really a very pretty solution.
But sometimes, prettiness is over-rated.

If you're not interested in printing the value read from the LDR, then this method needs you to put the delay(10) back in, but then it will not change the morph rate as the value read from the LDR changes.

Hey we have a similar problem; we're wanting to combine 2 sketches but are having difficulty;
the two sketches are:

/*
 * Ardrumo sketch
 *
 * Use with the Ardrumo software here:
 * http://code.google.com/p/ardrumo/
 * This is designed to let an Arduino act as a drum machine
 * in GarageBand (sorry, Mac OS X only).
 */

#define LEDPIN     13     // status LED pin
#define PIEZOTHRESHOLD 5  // analog threshold for piezo sensing
#define PADNUM 6          // number of pads

int val;

void setup() {
  pinMode(LEDPIN, OUTPUT);
  Serial.begin(57600);   // set serial output rate
}

void loop() {

  // Loop through each piezo and send data
  // on the serial output if the force exceeds
  // the piezo threshold
  for(int i = 0; i < PADNUM; i++) {
    val = analogRead(i);
    if( val >= PIEZOTHRESHOLD ) {
      digitalWrite(LEDPIN,HIGH);  // indicate we're sending MIDI data
      Serial.print(i);
      Serial.print(",");
      Serial.print(val);
      Serial.println();
      digitalWrite(LEDPIN,LOW);
    }
  }
}

And

// Example 04: Fade an LED in and out like on 
// a sleeping Apple computer 
//
// Copy and paste this example into an empty Arduino sketch

#define LED   9 // the pin for the LED
int i = 0;      // We'll use this to count up and down

void setup() { 
  pinMode(LED, OUTPUT); // tell Arduino LED is an output 
} 

void loop(){ 

  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(LED, i);      // set the LED brightness
    
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
  }

  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
  }

}

Thanks!

Hey Groove,

Thanks heaps for your help. There are some videos on our blog of the final project.

Keep up the good work looking after us Noobs.

@HALarduino - so where's the combined code?

Hey Groove,

We don't know how to combine the Ardrumo Sketch and the fading LED sketch successfully. We were wondering if you knew?

We're trying it ourselves but any input would be helpful.

Thanks

I combined them and it kind of worked,

BUT we want the LED to fade in and out continuously independently of whether the piezo buzzers are hit.

This is the Code we've got but the LED fades in and out only when you hit the piezo- and you can't get a sound/any reaction from the piezo again until the light has faded back out.

/*
 * Ardrumo sketch
 *
 * Use with the Ardrumo software here:
 * http://code.google.com/p/ardrumo/
 * This is designed to let an Arduino act as a drum machine
 * in GarageBand (sorry, Mac OS X only).
 */

#define LEDPIN     13     // status LED pin
#define PIEZOTHRESHOLD 5  // analog threshold for piezo sensing
#define PADNUM 3          // number of pads
#define LEDAPPLE   9 // the pin for the LED


int val;

void setup() {
  pinMode(LEDPIN, OUTPUT);
  pinMode(LEDAPPLE, OUTPUT); // tell Arduino LED is an output 
  Serial.begin(57600);   // set serial output rate
}

void loop() {

  // Loop through each piezo and send data
  // on the serial output if the force exceeds
  // the piezo threshold
  for(int i = 0; i < PADNUM; i++) {
    val = analogRead(i);
    if( val >= PIEZOTHRESHOLD ) {
      digitalWrite(LEDPIN,HIGH);  // indicate we're sending MIDI data
      Serial.print(i);
      Serial.print(",");
      Serial.print(val);
      Serial.println();
      digitalWrite(LEDPIN,LOW);
      
        for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(LEDAPPLE, i);      // set the LED brightness
    
      delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
  }

  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(LEDAPPLE, i); // set the LED brightness
    delay(10);           // Wait 10ms
  }
    
    
    }
  }

}

I combined them and it kind of worked

You haven't said what "worked" means in this context, so until that is clear to all, it isn't really possible to help.

"Have a look at the "Blink without delay" tutorial" is usually what I say at this point.

Sorry!!

What I meant by it 'kind of works' last time was:
-the LED faded in and out but only when a piezo buzzer was hit, and we wanted it to go regardless of the piezo buzzers activity.

Ok, I went through the blink without delay tutorial.

The LED blinks continuously which is what we want- but it stops the Ardrumo sketch working.
So we have no sound coming out when the piezo sensors are hit.

This is the combined sketched I tried but no sound came out:

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
#define PIEZOTHRESHOLD 5  // analog threshold for piezo sensing
#define PADNUM 3          // number of pads


// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  
  /*
 * Ardrumo sketch
 *
 * Use with the Ardrumo software here:
 * http://code.google.com/p/ardrumo/
 * This is designed to let an Arduino act as a drum machine
 * in GarageBand (sorry, Mac OS X only).
 */
#define PIEZOTHRESHOLD 5  // analog threshold for piezo sensing
#define PADNUM 3          // number of pads



int val;


  // Loop through each piezo and send data
  // on the serial output if the force exceeds
  // the piezo threshold
  for(int i = 0; i < PADNUM; i++) {
    val = analogRead(i);
    if( val >= PIEZOTHRESHOLD ) {
      Serial.print(i);
      Serial.print(",");
      Serial.print(val);
      Serial.println();
    }
  }



  
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 

 
   if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

This is the combined sketched I tried but no sound came out

Could that be because there's nothing in that sketch that would actually cause anything to make a sound?