Hi Guys
I was wondering if anyone could help me combine two sketches into one. I have gone through using FOR and While and Do etc but i can not seem to make them work. What i think the problem is, is that one code is never ending, as it is a random led blink, so the arduino can never get through the whole section in the loop for it to do the next task. Sketch 1 is using 2 RGB LED serial modules to random blink and Sketch 2 is using a MIC Module to pic up the noise in the room to change 3leds according to the voltage reading. Both Sketches work by themselves perfectly. Any help would be appreciated.
Here is each sketch separately and then how i tried to put it together
Sketch 1
int SDI = 2;
int CKI = 3;
int ledPin = 13;
#define STRIP_LENGTH 3
long strip_colors[STRIP_LENGTH];
void setup() {
pinMode(SDI, OUTPUT);
pinMode(CKI, OUTPUT);
pinMode(ledPin, OUTPUT);
//Clear out the array
for(int x = 0 ; x < STRIP_LENGTH ; x++)
strip_colors[x] = 0;
randomSeed(analogRead(0));
}
void loop() {
//Pre-fill the color array with known values
strip_colors[0] = 0xFF0000; //Bright Red
strip_colors[1] = 0x00FF00; //Bright Green
strip_colors[2] = 0x0000FF; //Bright Blue
strip_colors[3] = 0x010000; //Faint red
strip_colors[4] = 0x800000; //1/2 red (0x80 = 128 out of 256)
post_frame(); //Push the current color frame to the strip
delay(2000);
while(1){ //Do nothing
addRandom();
post_frame(); //Push the current color frame to the strip
digitalWrite(ledPin, HIGH); // set the LED on
delay(250); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(250); // wait for a second
}
}
//Throws random colors down the strip array
void addRandom(void) {
int x;
//First, shuffle all the current colors down one spot on the strip
for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
strip_colors[x] = strip_colors[x - 1];
//Now form a new RGB color
long new_color = 0;
for(x = 0 ; x < 3 ; x++){
new_color <<= 8;
new_color |= random(0xFF); //Give me a number from 0 to 0xFF
//new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
}
strip_colors[0] = new_color; //Add the new random color to the strip
}
//Takes the current strip color array and pushes it out
void post_frame (void) {
//Each LED requires 24 bits of data
//MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
//Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
//Pulling the clock low for 500us or more causes the IC to post the data.
for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
long this_led_color = strip_colors[LED_number]; //24 bits of color data
for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
//Feed color bit 23 first (red data MSB)
digitalWrite(CKI, LOW); //Only change data when clock is low
long mask = 1L << color_bit;
//The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
if(this_led_color & mask)
digitalWrite(SDI, HIGH);
else
digitalWrite(SDI, LOW);
digitalWrite(CKI, HIGH); //Data is latched when clock goes high
}
}
//Pull clock low to put strip into reset/post mode
digitalWrite(CKI, LOW);
delayMicroseconds(500); //Wait for 500us to go into reset
}
Sketch 2
const int analogOutPin = 3; //White LED
const int analogOutPin2 = 5; //Pink LED
const int analogOutPin3 = 6; //RGB LED
int brightness = 1024;
void setup() {
Serial.begin(38400);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
delay(200);
if (voltage > 2.48)
{
analogWrite(analogOutPin3, brightness);
digitalWrite(analogOutPin3, HIGH);
analogWrite(analogOutPin, brightness);
digitalWrite(analogOutPin, HIGH);
delay(1000);
digitalWrite(analogOutPin, LOW);
}
if (voltage < 2.47)
{
analogWrite(analogOutPin3, brightness);
digitalWrite(analogOutPin3, HIGH);
analogWrite(analogOutPin2, brightness);
digitalWrite(analogOutPin2, HIGH);
delay(1000);
digitalWrite(analogOutPin2, LOW);
}
}
Combining them together
int SDI = 8;
int CKI = 9;
int ledPin = 13;
const int analogOutPin = 3; //White LED
const int analogOutPin2 = 5; //Pink LED
const int analogOutPin3 = 6; //RGB LED
int brightness = 1024;
#define STRIP_LENGTH 2
long strip_colors[STRIP_LENGTH];
void setup() {
Serial.begin(38400);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(SDI, OUTPUT);
pinMode(CKI, OUTPUT);
pinMode(ledPin, OUTPUT);
//Clear out the array
for(int x = 0 ; x < STRIP_LENGTH ; x++)
strip_colors[x] = 0;
randomSeed(analogRead(0));
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
if (voltage > 2.48)
{
analogWrite(analogOutPin3, brightness);
digitalWrite(analogOutPin3, HIGH);
analogWrite(analogOutPin2, brightness);
digitalWrite(analogOutPin2, HIGH);
delay(1000);
digitalWrite(analogOutPin2, LOW);
}
if (voltage < 2.47)
{
analogWrite(analogOutPin3, brightness);
digitalWrite(analogOutPin3, HIGH);
analogWrite(analogOutPin, brightness);
digitalWrite(analogOutPin, HIGH);
delay(1000);
digitalWrite(analogOutPin, LOW);
}
//Pre-fill the color array with known values
strip_colors[0] = 0xFF0000; //Bright Red
strip_colors[1] = 0x00FF00; //Bright Green
strip_colors[2] = 0x0000FF; //Bright Blue
strip_colors[3] = 0x010000; //Faint red
strip_colors[4] = 0x800000; //1/2 red (0x80 = 128 out of 256)
post_frame(); //Push the current color frame to the strip
delay(2000);
while(1){ //Do nothing
addRandom();
post_frame(); //Push the current color frame to the strip
digitalWrite(ledPin, HIGH); // set the LED on
delay(250); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(250); // wait for a second
}
}
//Throws random colors down the strip array
void addRandom(void) {
int x;
//First, shuffle all the current colors down one spot on the strip
for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
strip_colors[x] = strip_colors[x - 1];
//Now form a new RGB color
long new_color = 0;
for(x = 0 ; x < 3 ; x++){
new_color <<= 8;
new_color |= random(0xFF); //Give me a number from 0 to 0xFF
//new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
}
strip_colors[0] = new_color; //Add the new random color to the strip
}
//Takes the current strip color array and pushes it out
void post_frame (void) {
//Each LED requires 24 bits of data
//MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
//Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
//Pulling the clock low for 500us or more causes the IC to post the data.
for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
long this_led_color = strip_colors[LED_number]; //24 bits of color data
for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
//Feed color bit 23 first (red data MSB)
digitalWrite(CKI, LOW); //Only change data when clock is low
long mask = 1L << color_bit;
//The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
if(this_led_color & mask)
digitalWrite(SDI, HIGH);
else
digitalWrite(SDI, LOW);
digitalWrite(CKI, HIGH); //Data is latched when clock goes high
}
}
//Pull clock low to put strip into reset/post mode
digitalWrite(CKI, LOW);
delayMicroseconds(500); //Wait for 500us to go into reset
}