I am using an ATTiny85 with a contactless water sensor and a buzzer.
The idea is that when NO water is detected, the buzzer will sound.
I am using the PinChangeInterrupt library.
The following code works great and the interrupt fires and switches the buzzer on.
However, I am using CHANGE with the interrupt and the problem is that a change must take place before the interrupt fires.
This creates a problem in that on power ON, the buzzer sounds even though water is detected.
Is it possible to have a FALLING or RISING event to fire the interrupt?
I have tried both and the interrupt does not fire when water is not detected by the sensor.
/*
* Sensor is active HIGH - when NO water is sensed
*/
#include "PinChangeInterrupt.h"
const int sensorPin = 3; // the pin that the pushbutton is attached to
const int buzzerPin = 1; // the pin that the LED is attached to
int sensorState = 1;
void setup() {
// set pin to input with a pullup, led to output
pinMode(sensorPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
// Attach the new PinChangeInterrupt and enable event function below
attachPCINT(digitalPinToPCINT(sensorPin), senseSensor, CHANGE);
}
void senseSensor(void) {
sensorState = digitalRead(sensorPin);
}
void loop() {
if (sensorState == 1){
digitalWrite(buzzerPin, HIGH);
}
if (sensorState == 0){
digitalWrite(buzzerPin, LOW);
}
}
/*
* Sensor is active HIGH - when NO water is sensed
*/
#include "PinChangeInterrupt.h"
const int sensorPin = 3; // the pin that the pushbutton is attached to
const int buzzerPin = 1; // the pin that the LED is attached to
volatile int sensorState = 1;
void setup() {
// set pin to input with a pullup, led to output
pinMode(sensorPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
// Attach the new PinChangeInterrupt and enable event function below
attachPCINT(digitalPinToPCINT(sensorPin), senseSensor, CHANGE);
sensorState = digitalRead(sensorPin); // Read the state
}
void senseSensor(void) {
sensorState = digitalRead(sensorPin);
}
void loop() {
if (sensorState == 1){
digitalWrite(buzzerPin, HIGH);
}
if (sensorState == 0){
digitalWrite(buzzerPin, LOW);
}
I don't know is the delay is in the correct position.
I only want to delay for 5 seconds if the sensorPin is HIGH.
I must now create a beeping sound, not a constant beeeeeeep.
The same supply that powers the ATtiny?
Maybe this sensor outputs a Highlevel while it is being turned on.
Try putting a 5 to 10 second delay in setup()
Then, make sure the glass is empty.
Apply power to the sensor and give it a few seconds to calibrate.
Fill the glass
Then start the Arduino program.
It should work but if you turn off power to the sensor then you need to start over with the empty glass.
Sorry, my mistake.
The device is fitted to a vehicle and monitors the water coolant level in the coolant bottle.
Every time the ignition is turned ON, the device powers up.
I am thinking of a way that I can "Fool" the device (ATTiny85) into thinking that it has started (Booted) up with the sensor in an empty state, then read the sensor and record either an empty or full state.
Possibly by using additional pins on the ATTiny85 as 1 output and 1 input and use this as a phantom empty state on Boot?
If the sensor is powered up with liquid in front of it, it will never work.
There is nothing you can do.
The sensor must be powered up ONLY when there is no liquid in front of it.