Use Simple Timer to turn a buzzer on and off

In my current project, I use code for a piezo beep sequence that might interest you. It's not perfect by any stretch of the imagination. But it does work. I found this on the web somewhere. Not sure where. But I made it work in my sketch pretty easily. Maybe it'll help you. Maybe it won't.

int beepState = 0;
int beepCount = 0;
const int speakerPin = 4;
const int shutOffSwitchPin = 6;
const long beepWait = 300;
unsigned long prevMillis = 0;

void setup() {
  pinMode(shutOffSwitchPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(shutOffSwitchPin) == HIGH) {
    beepState = 1;
  }
  if (beepState == 1) {
    makeNoise();
  }
}

void makeNoise() { //4 beeps and then stops
  unsigned long curMillis = millis();
  if (beepCount == 0 || beepCount == 2 || beepCount == 4 || beepCount == 6) {
    if (curMillis - prevMillis >= beepWait) {
      prevMillis = curMillis;
      tone(speakerPin, 900, 100); //(pin,sound(hz?),duration) change sound number to your liking.
      beepCount++;
    }
  }
  else if (beepCount == 1 || beepCount == 3 || beepCount == 5) {
    if (curMillis - prevMillis >= beepWait) {
      prevMillis = curMillis;
      noTone(speakerPin);
      beepCount++;
    }
  } else {
    beepState = 0;
    beepCount = 0;
    noTone(speakerPin);
  }
}

Hi ,

Sorry I didnt replay these 2 days. It is because my Arduino Uno died and wont upload. I will have to wait for a new Arduino. :frowning: :frowning:

DangerToMyself:
In my current project, I use code for a piezo beep sequence that might interest you. It's not perfect by any stretch of the imagination. But it does work. I found this on the web somewhere. Not sure where. But I made it work in my sketch pretty easily. Maybe it'll help you. Maybe it won't.

int beepState = 0;

int beepCount = 0;
const int speakerPin = 4;
const int shutOffSwitchPin = 6;
const long beepWait = 300;
unsigned long prevMillis = 0;

void setup() {
  pinMode(shutOffSwitchPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(shutOffSwitchPin) == HIGH) {
    beepState = 1;
  }
  if (beepState == 1) {
    makeNoise();
  }
}

void makeNoise() { //4 beeps and then stops
  unsigned long curMillis = millis();
  if (beepCount == 0 || beepCount == 2 || beepCount == 4 || beepCount == 6) {
    if (curMillis - prevMillis >= beepWait) {
      prevMillis = curMillis;
      tone(speakerPin, 900, 100); //(pin,sound(hz?),duration) change sound number to your liking.
      beepCount++;
    }
  }
  else if (beepCount == 1 || beepCount == 3 || beepCount == 5) {
    if (curMillis - prevMillis >= beepWait) {
      prevMillis = curMillis;
      noTone(speakerPin);
      beepCount++;
    }
  } else {
    beepState = 0;
    beepCount = 0;
    noTone(speakerPin);
  }
}

I might consider that if PieterPs code doesnt work for me.

PieterP:
What relay?

Have you tried using the PushButton example to detect a rising or a falling edge?

Pieter

Sorry I meant buzzer. I was a little tired working on the project. :slight_smile: When I get my arduino by post I will try with the PushButton example.

Hi,
How did you damage your UNO.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

I am surprised you have a PCB made and yet have not finalised your code.

Thanks.. Tom.. :slight_smile:

I have an Arduino Nano that got stuck while uploading some code and now it won`t do anything. The onboard led is barely lit and when I upload code I get

         System wide configuration file is "C:\Users\clayt\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbasp
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : USBasp, http://www.fischl.de/usbasp/

avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "C:\Users\clayt\AppData\Local\Temp\arduino_build_967741/Blink.ino.hex"
avrdude: writing flash (928 bytes):

Writing | ################################################## | 100% 0.50s

avrdude: 928 bytes of flash written
avrdude: verifying flash memory against C:\Users\clayt\AppData\Local\Temp\arduino_build_967741/Blink.ino.hex:
avrdude: load data flash data from input file C:\Users\clayt\AppData\Local\Temp\arduino_build_967741/Blink.ino.hex:
avrdude: input file C:\Users\clayt\AppData\Local\Temp\arduino_build_967741/Blink.ino.hex contains 928 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.27s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0xff != 0x0c
avrdude: verification error; content mismatch

avrdude done.  Thank you.

the selected serial port 
 does not exist or your board is not connected

The Uno samething

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "D:\Users\clayt\Desktop\imbarazz\arduino-1.8.2-windows\arduino-1.8.2\portable\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf"

         Using Port                    : COM4
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
An error occurred while uploading the sketch
avrdude: ser_recv(): read error: The I/O operation has been aborted because of either a thread exit or an application request.


avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xd7
avrdude: ser_send(): write error: sorry no info avail
avrdude: ser_recv(): read error: The device does not recognize the command.



avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xd7
avrdude: ser_drain(): read error: The device does not recognize the command.



avrdude done.  Thank you.

The RX led flashes but the TX led doesn`t.

I don`t think the damage is from the circut. When I got my arduinos I used a spray that applied plastic to the boards. I tought it was to protect them but the plastic got in between the ic pins and now not all the Atmega328 pins contact the actual ic socket.

I included the picture of the final project. The Float sensor will be placed inside a bucket that needs to filled from tap water. When the bucket is not full the Arduino turns on the Solenoid Valve and after one hour of filling the solenoid will close and waits 1hr 30min to continue filling the bucket. When the bucket fills up the arduino closes the valve and the buzzer beeps 4 times to notify the user the bucket is filled. That is the whole concept of this project. The only part where I am stuck is the 4 beeps when the bucket is full.

#include <SimpleTimer.h>


const int valve = 13;
const int sensor = 4;
const int buzzer = 5;

int mode = 0;

unsigned long previousMillis = 0;
const long fill_time = 3600000; //1hr fill time
unsigned long previousMillis1 = 0;
const long pause = 5400000; //1hr 30 min wait time

SimpleTimer timer;
void setup()
{
  pinMode(valve, OUTPUT);
  pinMode(sensor, INPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(valve, LOW);
  //digitalWrite(buzzer, LOW);

}

void loop () {
  unsigned long currentMillis = millis();

  if ((mode == 0 || mode == 2) && (digitalRead (sensor) == 0)) {//Turn valve on for 1hr

    if (currentMillis - previousMillis <= fill_time) {
      mode = 1;


    }
  } else  { // else if bucket full turn valve off and beep 4 times
    mode = 2;

    previousMillis1 = currentMillis;
    previousMillis = currentMillis; //reset times

  }

  if (mode == 1 && currentMillis - previousMillis >= fill_time) {// if bucket is filling and 1hr has passed
    mode = 3;  //turn valve off
    previousMillis1 = currentMillis;// reset time
  }

  if ((mode == 3) && (currentMillis - previousMillis1 >= pause)) { // if 1hr 30min have passed since the valve was stopped
    mode = 0; //turn valve on
    previousMillis = currentMillis; // reset pause time
  }



  switch (mode) {
    case 1:
      digitalWrite(valve, HIGH); //turn valve on
      break;
    case 2:
      //buzzer sound
      digitalWrite(valve, LOW);// turn valve off
      break;
    case 3: digitalWrite(valve, LOW); // turn valve off but dont beep
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }
  timer.run();
}

Hi,
OPs diag


What is J1, J2, J3 ?
What about the nano gnd?

Tom... :slight_smile:

TomGeorge:
Hi,
OPs diag


What is J1, J2, J3 ?
What about the nano gnd?

Tom... :slight_smile:

,
They are all Terminal Block, J1 is Solenoid Valve output J2 Input for Float Sensor, J3 12V Power Input, I have the ground connected to the other side of the nano above reset.