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);
}
}