Using two vibration sensor to trigger two separate RGB LED 5050 strips.

Hi, I’m fairly new to this arduino programming thing. So I have a question on how I could use two vibration sensor(KY-002) to trigger two separate RGB LED 5050 strips at the same time. By the way I using millis() to create fading effect for the RGB led strips. Since I’ve encountered how the delay() pauses the whole programme. Here’s the code. I appreciate all helps. Thank you. :smiley:

int shockPin = 10; // Use Pin 10 as our Input
int shockVal = HIGH; // This is where we record our shock measurement
boolean bAlarm = false;

int ledcolor = 0;
int Timing = 0;

static long timer;
static long timerb;

int fadeAmount = 5;
int brightness = 0;

//A variable to hold the previously assigned pseudo random non-consecutive integers.
int previousledcolor = 0;

#define REDPIN 2
#define GREENPIN 3
#define BLUEPIN 4

unsigned long lastShockTime; // Record the time that we measured a shock

int shockAlarmTime = 0; // Number of milli seconds to keep the shock alarm high

int ms_delay = 0; // milliseconds to delay for
unsigned long end_time;

int shockPin2 = 11; // Use Pin 10 as our Input
int shockVal2 = HIGH; // This is where we record our shock measurement
boolean bAlarm2 = false;

int ledcolob = 0;
int Timing2 = 0;

int fadeAmount2 = 5;
int brightness2 = 0;

//A variable to hold the previously assigned pseudo random non-consecutive integers.
int previousledcolob = 0;

#define REDPIN2 5
#define GREENPIN2 6
#define BLUEPIN2 7

unsigned long lastShockTime2; // Record the time that we measured a shock

int shockAlarmTime2 = 0; // Number of milli seconds to keep the shock alarm high

int ms_delay2 = 0; // milliseconds to delay for
unsigned long end_time2;

void setup ()
{
Serial.begin(9600);

pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode (shockPin, INPUT); // input from the KY-002
timer = millis();

pinMode(REDPIN2, OUTPUT);
pinMode(GREENPIN2, OUTPUT);
pinMode(BLUEPIN2, OUTPUT);
pinMode (shockPin2, INPUT); // input from the KY-002
timerb = millis();
}

void loop ()
{

analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 0);

analogWrite(REDPIN2, 0);
analogWrite(GREENPIN2, 0);
analogWrite(BLUEPIN2, 0);

shockVal = digitalRead (shockPin) ; // read the value from our sensor
int r = 0;

int ledcolor = random(0,7); //this randomly selects a number between 0 and 6
ms_delay = random(3000, 11000);

if (shockVal == LOW) // If we’re in an alarm state
{
lastShockTime = millis(); // record the time of the shock
// The following is so you don’t scroll on the output screen
if (!bAlarm){
Serial.println(“yes”);
bAlarm = true;

//Check if the random number repeats. reassign a new random number.
while (ledcolor == previousledcolor) {
ledcolor = random(0,7);
}

//Set previousRandomInt equal to the current randomInt.
previousledcolor = ledcolor;

Serial.print(ledcolor);
Serial.print("\n");
//Serial.print(Timing);

end_time = millis() + ms_delay;
Serial.print(ms_delay);
switch (ledcolor) {
case 0: //RED
brightness = 0;
while (millis() < end_time){
for (r = 0; r < 256; r++) {
if (millis() - timer > 50){
timer = millis();
if(brightness<=255){
Serial.print(brightness);
analogWrite(REDPIN, brightness);
brightness = brightness + fadeAmount;
}
}
}
}

for (int r = 255; r >= 0; r–){
Serial.print("\n");
Serial.print(r);
analogWrite(REDPIN, r);
brightness = brightness + fadeAmount;
fadeAmount = -fadeAmount;
}
break;

case 1: //BLUE
brightness = 0;
while (millis() < end_time){
for (r = 0; r < 256; r++) {
if (millis() - timer > 50){
timer = millis();
if(brightness<=255){
analogWrite(BLUEPIN, brightness);
brightness = brightness + fadeAmount;
}
}
}
}

for (int r = 255; r >= 0; r–){
Serial.print("\n");
Serial.print(r);
analogWrite(BLUEPIN, r);
brightness = brightness + fadeAmount;
fadeAmount = -fadeAmount;
}
break;
}
}
}
else
{
if((millis()-lastShockTime) > shockAlarmTime && bAlarm){
Serial.println("\t");
Serial.println(“no”);
bAlarm = false;
}
}

shockVal2 = digitalRead (shockPin2) ; // read the value from our sensor
int b = 0;

int ledcolob = random(0,7); //this randomly selects a number between 0 and 6
ms_delay2 = random(3000, 11000);

if (shockVal2 == LOW) // If we’re in an alarm state
{
lastShockTime2 = millis(); // record the time of the shock
// The following is so you don’t scroll on the output screen
if (!bAlarm2){
Serial.println(“Second”);
bAlarm2 = true;

//Check if the random number repeats. reassign a new random number.
while (ledcolob == previousledcolob) {
ledcolob = random(0,7);
}

//Set previousRandomInt equal to the current randomInt.
previousledcolob = ledcolob;

Serial.print(ledcolob);
Serial.print("\n");
//Serial.print(Timing);

end_time2 = millis() + ms_delay2;
Serial.print(ms_delay2);
switch (ledcolob) {

case 0: //RED
brightness2 = 0;
while (millis() < end_time2){
for (b = 0; b < 256; b++) {
if (millis() - timerb > 50){
timerb = millis();
if(brightness2<=255){
analogWrite(REDPIN2, brightness2);
brightness2 = brightness2 + fadeAmount2;
}
}
}
}

for (int b = 255; b >= 0; b–){
Serial.print("\n");
Serial.print(b);
analogWrite(REDPIN2, b);
brightness2 = brightness2 + fadeAmount2;
fadeAmount2 = -fadeAmount2;
}
break;
}
}
}
else
{
if((millis()-lastShockTime2) > shockAlarmTime2 && bAlarm2){
Serial.println("\t");
Serial.println(“Second No”);
bAlarm2 = false;
}
}
}

The code you posted improperly does something. You failed to mention what it actually does.

while (millis() < end_time){
       for (r = 0; r < 256; r++) {
         if (millis() - timer > 50){
           timer = millis();
           if(brightness<=255){
             Serial.print(brightness);
             analogWrite(REDPIN, brightness);
             brightness = brightness + fadeAmount;
           }
         }

Rolled your own delay(), eh.

That is NOT the approach to doing two (or more) things “at the same time”.