Can the Arduino do two things at one time?

I have two programs that I want to make into one. But not working right.

First program is the "Vibration Switch" The circuit has one Piezo Disk Vibration Sensor,
the (+) side is in pin 7 of the Anolog. The (-) side is in pin GND. There is one resistor
form pin 7 Anolog to GND.

Second program is the "Time Delay Led On" The circuit has one pushbutton
pin 8 and one led pin 13 both on Digital. If pushbutton is held down for more
then 5sec. and then let go, the led will go on. If pushbutton is held down for
less then 5sec. led is off.

What I am shooting for = I want to put the Piezo Disk on my dryer. When the
dryer is on and running, the led is off, but as soon as the dryer is turned off
the led is on, if it meets the 5sec. threshold. But the Piezo is so sensitive,
So I need A delay or threshold.

Here are the two program's

Vibration Switch

int inPin = 5;             // analog 5
 int val = 0;                 // where to store info from analog 5
 int pin11 = 11;         // output of red led

 void setup() {
 
 Serial.begin(9600);
 }
   void loop() {
 
  val = analogRead(inPin);                    // reads in the values from analog 5 and
                                                                   //assigns them to val
  if(val >= 1){
   
    val = constrain(val, 1, 100);               // mess with these values                                      
    val = map(val, 1, 100, 1, 255);        // to change the response distance of the device
    analogWrite(pin11, val);                    // *note also messing with the resistor should change 
                                                                   // the sensitivity
   }else{                                                     // analogWrite(pin11, val); just tuns on the led with
                                                                  // the intensity of the variable val
    analogWrite(pin11, 0);                     // the else statement is just telling the microcontroller
                                                                 // to turn off the light if there is no EMF detected
   }
   Serial.println(val);                                // use output to aid in calibrating
   }

Time Delay Led On

unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000;   // 5 sec or your choice
byte DET;
const byte pirPin = 8;  // == button, etc
const byte ledPin = 13;
int lastButtonState = 0;

void setup ()

{
  pinMode (ledPin,OUTPUT);
  digitalWrite (ledPin,LOW);
   pinMode(pirPin, INPUT);
   Serial.begin(9600);
}

void loop ()

{
   Serial.println(trippoint);
   Serial.println(refTime);
   DET = digitalRead(pirPin);
  
 if (DET != lastButtonState) {
   if (DET == 1)  // inactive
  {
    refTime = millis();
    trippoint = refTime;
  }
  else   // Active Low
  {
   Serial.println("off");
  }
  lastButtonState = DET;
 trippoint = millis();
 
  }
 if ((trippoint-refTime) > threshold)
  {
    digitalWrite(ledPin,HIGH);
    delay(100);
    digitalWrite(ledPin,LOW);
    delay(100);

   }
 }

Here is what I tried to do, with both programs.

I am having hard time with the vibration input, it's not working like the hold down the
switch in the (Time Delay Led On) program.

unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000;   // 5 sec or your choice
byte DET;
const byte pirPin = 8;  // == button, etc
const byte ledPin = 13;
int lastButtonState = 0; 

int inPin = 5;             // analog 5
int val = 0;               // where to store info from analog 5
int pin11 = 11;    


void setup ()
{
  //pirPin is INPUT by default, use ExtPullup
  pinMode (ledPin,OUTPUT);
  digitalWrite (ledPin,LOW);
   pinMode(pirPin, INPUT);
   Serial.begin(9600);
}

void loop ()
{
   val = analogRead(inPin);                    // reads in the values from analog 5 and
                                                                   //assigns them to val
  if(val >= 1)
  {
   val = constrain(val, 1, 100);               // mess with these values                                      
   val = map(val, 1, 100, 1, 255);        // to change the response distance of the device
   analogWrite(pin11, val);
        DET  = (val < 200 == 1);
                                                 // *note also messing with the resistor should change 
  }                                                                  // the sensitivity
   
 else
 {                                                     // analogWrite(pin11, val); just tuns on the led with
                                                                  // the intensity of the variable val
 analogWrite(pin11, 0);                     // the else statement is just telling the microcontroller
                                                                 // to turn off the light if there is no EMF detected
  }
 
  Serial.println(DET);                                // use output to aid in calibrating
 // Serial.println(trippoint);
 // Serial.println(refTime);
   
 // DET = digitalRead(pirPin);
  
 if (DET != lastButtonState) {
   if (DET == 1)  // inactive
  {
    refTime = millis();
    trippoint = refTime;
  }
  else   // Active Low
  {
  // Serial.println("off");
  }
  
 lastButtonState = DET;
 trippoint = millis();
}
 if ((trippoint-refTime) > threshold)
  {
    digitalWrite(ledPin,HIGH);
    delay(100);
    digitalWrite(ledPin,LOW);
    delay(100);

   }
 }

The general rule is to get each small piece of a project working, before you combine the pieces. If the vibration input isn't working as expected, use the simplest possible program to isolate and fix that problem first.

Going back to the question in the subject: "Can the Arduino do two things at one time?" the answer is NO.

BUT - by switching between two tasks often enough it will appear to do two (or three, four...) things at the same time.

This is why you see so many posts talk about the "Blink without delay" example, because neither of your two tasks may block - that is, sit in a loop doing something that takes time. They must do such tasks a small step at a time, remember how far they got, and when the loop() gets round to the again, take the next tiny step.

The trick of the "Blink without delay" technique is that the large "delay()" is actually just taken off in small bites by just executing millis() to look at the clock. See if Arduino Playground - AvoidDelay helps explaining this.

Now, back to the program. I am sorry I have not studied your code. I have just looked if it had the classical error - using delay(). Yep.

"delay()" and "two things at once" are mutually exclusive.

It is probably easier to write a new sketch rather than combine two existing sketches. Just use the existing sketches for ideas.

I you really want to try to merge the two sketches this general approach may help

// variables from sketchA
// variables from sketch B

void setup() {
   // setup stuff from sketch A
   // setup stuff from sketch B
}

void loop() {
   // functionA();
   // functionB();
}

void functionA() {
   // code from loop() part of sketch A
}

void functionB() {
   // code from loop() part of sketch B
}

...R

I want the (Vibration Switch program) to be or replace the switch in the (Time Delay Led On program).
So if the Piezo Disk, input can be the switch. On or off.

If you break the LedOn program into two functions - one which reads the switch and stores the value in a global variable (let's call it switchPos) and the other which switches the light depending on the value of switchPos then all you need to do is get your Vibration code to set the value of switchPos.

...R

When the (Vibration Switch) program is running it is giving me data that is going from 0 to 116.
I think that is what's not right. I need data that is steady data, like 1111111111111111111111 or 00000000000000.
Like A switch held down.
Not jumping around.

All I want is A 5 sec. delay. Because the ( Piezo Disk Vibration Sensor) is so sensitive, that if
you bump or hit the dryer, it would make the led go off. So I want some delay.
So the dryer has to run for 5 sec. or more. Then when the data goes to 0.
Then the led would go one and off, on and off.

(Piezo Disk Vibration Sensor) on dryer, dryer not running = 0 for data
(Piezo Disk Vibration Sensor) on dryer, dryer is running = 1 for data
(Piezo Disk Vibration Sensor) on dryer, dryer is running = 1 for data, has to be running for more then 5sec.
(Piezo Disk Vibration Sensor) on dryer, dryer is running = 1 for data, has to be running for more then 5sec. After 5 sec is up.
(Piezo Disk Vibration Sensor) on dryer, dryer is running = ((0)) for data, has to be running for more then 5sec. After 5 sec is up.
Then turn on led.

Dryer Data when running.

62
0
65
67
103
0
83
0
6
8
77
60
62
77
0
3
0
116
0
60
0
83
80
31
26
31
6
0
52

Please take a look at the following tutorial on smoothing analog inputs.

You can generate a time based or count based moving average of the piezo data. Do you see zero from the piezo sensor when the dryer is off? You should be able to determine a threshold value from your moving average to distinguish between the dryer on and off.

BenBenBen:
When the (Vibration Switch) program is running it is giving me data that is going from 0 to 116.

An analog input is not going to give you an exact result. If there is a clear distinction between on and off numbers that should be sufficient. For example, anything less than 50 might be off and anything over 80 might be on. (But you need to decide your own values depending on the results you get).

The Blink Without Delay technique will allow you to manage any timing that you may need.

...R