Hi there I am a noob when it comes to Arduino I am 53 and is busy teaching myself some programming. I have managed to do a couple of things but I need help.
I am using an ATTINY 85 to monitor an INPUT from a sensor at startup the outputs must give me 3 beeps to inform me it is working and the input is HIGH, WORKING no problem there.
Then it need to monitor this input, if the input goes low for longer than 5 seconds it needs to activate the output and give me an alarm that switches on and off until the input goes HIGH again.
Currently the output goes HIGH once and then switches off when the input goes low. Please tell me what am I missing.
My Code is below
// Define the input and output pins
const int inputPin = 0; // PB0 (pin 5 on ATtiny85)(Sensor is connected to this pin)
const int alarmPin = 1; // PB1 (pin 6 on ATtiny85)(Also the onboard LED)
int redLed = 3; // LED on PB3 of Attiny 85
int noiseBuzz = 2; // Buzzer on PB2 of Attiny 85
int relayOut = 4; // PB 4 is ouput for the cutoff relay.
// Variables for state tracking
int lastInputState = LOW; // Last state of the input
unsigned long changeTime = 0; // Time when the state changed
bool alarmActive = false; // Whether the alarm is active
// Set the state of the inputs and outputs before startup
int ledState = LOW;
int redLedState = HIGH;
int noiseBuzzState = LOW;
int coolantLevel = LOW;
void setup() {
// Set the input pin as input
pinMode(inputPin, INPUT); // sensor input on PB0
// Set the alarm pin as output
pinMode(alarmPin, OUTPUT); // This is also the onboard LED on PB1
// Start with the alarm off
digitalWrite(alarmPin, LOW);
// initialize digital pin PB 2 as output for the Buzzer
pinMode(noiseBuzz, OUTPUT);
// initialize digital pin PB3 as an output for the external LED
pinMode(redLed, OUTPUT);
// initialaise pin PB44 as output for the relay to switch the pump
pinMode(relayOut, OUTPUT);
// turns the external led on during boot on PB3
digitalWrite (redLed, HIGH); // turns the external led on during boot on PB3
// turns the relay off during boot pn PB4
digitalWrite(relayOut, LOW);
delay(2000);
coolantLevel = digitalRead(inputPin);
if (coolantLevel == HIGH) {
digitalWrite(noiseBuzz, HIGH); // turn the buzzer, LEd and onboard LED on, this will happen 3 times to indicate the circuit is working
digitalWrite(redLed, HIGH);
digitalWrite(alarmPin, HIGH);
delay(100);
digitalWrite(noiseBuzz, LOW); // turn the buzzer, LED and onboard LED OFF, this will happen 3 times to indicate the circuit is working
digitalWrite(redLed, LOW);
digitalWrite(alarmPin, LOW);
delay(75);
digitalWrite(noiseBuzz, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(alarmPin, HIGH);
delay(100);
digitalWrite(noiseBuzz, LOW);
digitalWrite(redLed, LOW);
digitalWrite(alarmPin, LOW);
delay(75);
digitalWrite(noiseBuzz, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(alarmPin, HIGH);
delay(100);
digitalWrite(noiseBuzz, LOW);
digitalWrite(redLed, LOW);
digitalWrite(alarmPin, LOW);
delay(75);
}
}
void loop() {
// Read the current state of the input
int currentInputState = digitalRead(inputPin);
// Check if the input state has changed
if (currentInputState != lastInputState) {
// If the state changed, record the time
changeTime = millis();
lastInputState = currentInputState;
}
// If the input state has been low for 5 seconds, activate the alarm
if (currentInputState == LOW && millis() - changeTime >= 5000) {
// Activate the alarm if it's not already active
if (!alarmActive) {
digitalWrite(alarmPin, HIGH); // Turn the alarm on
digitalWrite(noiseBuzz, HIGH);
digitalWrite(redLed, HIGH);
delay(500);
digitalWrite(noiseBuzz, LOW);
digitalWrite(redLed, LOW);
delay(250);
alarmActive = true;
}
}
// If the input is pulled high, turn off the alarm at any time
if (currentInputState == HIGH && alarmActive) {
digitalWrite(alarmPin, LOW); // Turn the alarm off
alarmActive = false;
}
}