Go Down

Topic: Need help with combining 2 sketches (Read 3091 times) previous topic - next topic

HALarduino

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

HALarduino

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

}


Groove

Quote
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.
Per Arduino ad Astra

HALarduino

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



Groove

Quote
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?
Per Arduino ad Astra

Arduinonoobs

#20
May 31, 2010, 09:35 am Last Edit: May 31, 2010, 09:52 am by NAB-unidesign Reason: 1
Here we go, The final code we used to make our shell change color and respond by vibrating when you put it up to your ear to listen to it. Credit for the code has to go to Groove, thanks again.  
Code: [Select]
const int LDRpin = 0; // analog pin 0 (connect LDR here)
const int LEDpin  = 13; //This is the LED

int threshold = 250;// 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);
 // read the value on analog input
 int LDR_val = analogRead(LDRpin);
 // 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);
 }
}

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)
}
Check out our blog at http://interactivearduino.wordpress.com/ to see the full project.

noor raihan

Hi,

I got problem to combine 2 sketches. The 1st sketches as below:

//void setup()
{
 // Setup serial
 Serial.begin(9600);
}void loop()
{
  Serial.println(analogRead(0));

  delay(200);  // wait 200 milliseconds
}


This is actually a soil moisture sensor. I want to make the output for this sensor as an analog input to blink three LED as stated below:

wet soil: 400-600 (GREEN LED)
in between: 200-400 (YELLOW LED)
dry soil: 0-200 (RED LED)

The 0-600 as stated above are the result that I get from the 1st sketches.I hope somebody can help me to get a right coding for my project because I'm very weak in this part :(

Thank You

Go Up