Help me with a problem please

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

Welcome to the forum

Thank you for trying to use code tags but you got it wrong. Please edit your post and paste the code where it currently says "type or paste code here"

To do what you want, you’ll need to use a ‘timer’, or millis() timing to allow the functions to cooperate.

delay() stops everything for the duration

It’s not hard, but requires a bit of thinking, planning and restructuring.

Thank You, But as I said I am very new. I looked at a millis example but have no idea where to start,

Do I check each pins state at the millis interval and then change it

If I write the program without checking the pin for state change and just tell it that if the pin is high then write the outputs low and high it works, but when add the 5 seconds pin status check it stops working.

It is difficult to find solutions if you know very little :sob:

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

1 Like

@krokkedil - your code does what ou wrote.

When the alarm condition appears, you make one beep and one flash.

I added to your loop just this one statement, just at the highest level so it gets execute every time through the loop:

  if (alarmActive) {
      digitalWrite(alarmPin, HIGH);
      digitalWrite(noiseBuzz, HIGH);
      digitalWrite(redLed, HIGH);
      delay(333);

      digitalWrite(alarmPin, LOW);
      digitalWrite(noiseBuzz, LOW);
      digitalWrite(redLed, LOW);
      delay(444);
  }

The delays will make the buttons seem sluggish - the alarm won't turn off immediately but will finich whatever blink/pause it is doing.

Here's an alarm function demonstration you might want to read today or one day. It does a beeping alarm you can silence and reset as separate events. There is no use for delay here, it is programmed using millis() instead and would allow all other activities coded in the same respectful manner to run at "the same time".. I wrote it for another alarming project, you might enjoy using the simulator.

HTH

a7

1 Like

You have 3 things your have to do each time through loop

  1. Check if your signal has been low for 5 seconds and if so, start your alarm
  2. check if your signal is high and if so, turn off your alarm
  3. if the alarm is active, see how long it has been since you toggled the buzzer

Item #3 just needs a couple more variables to keep track of things...

// 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

unsigned long alarmTime;
unsigned long alarmDelay;
const unsigned long alarmDelayON = 500;
const unsigned long alarmDelayOFF = 250;

// 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) {
    for (int i = 0; i < 3; ++i ) {
      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);
    }
  }
}

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

  // Step 1: check if we need to activate alarm

  // if the alarm is not active, check for alarm state
  if ( !alarmActive ) {
    // If the input state has been low for 5 seconds, activate the alarm
    if (currentInputState == LOW && millis() - changeTime >= 5000) {
      // Activate the alarm
      alarmActive = true;
      alarmTime = millis();
      digitalWrite(alarmPin, HIGH); // Turn the alarm on
      digitalWrite(noiseBuzz, HIGH);
      digitalWrite(redLed, HIGH);
      alarmDelay = alarmDelayON;
    }
  }

  // Step 2: check if we need to de-activate alarm

  // If the input is pulled high, turn off the alarm at any time
  if (currentInputState == HIGH) {
    digitalWrite(alarmPin, LOW);  // Turn the alarm off
    digitalWrite(noiseBuzz, LOW);
    digitalWrite(redLed, LOW);
    alarmActive = false;
  }

  // Step 3: check if we need to toggle buzzer,led during alarm

  if ( alarmActive && millis() - alarmTime >= alarmDelay ) {
    if ( digitalRead(redLed) == HIGH ) {
      // currently ON so toggle OFF
      digitalWrite(noiseBuzz, LOW);
      digitalWrite(redLed, LOW);
      alarmTime = millis();
      alarmDelay = alarmDelayOFF;
    }
    else {
      // current OFF, so toggle ON
      digitalWrite(noiseBuzz, HIGH);
      digitalWrite(redLed, HIGH);
      alarmTime = millis();
      alarmDelay = alarmDelayON;
    }
  }
}

Thank You for all the information. understanding the millis function is a bit more tricky but with playing with it i am sure I will get more confident.

I ended up adding a constant interval as per the doing the Multiple things at the same time example. It was a bit of learning curve to get it to work.

I will test it today and if it does not work they way i want I will follow the code given above.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.