Blink without delay within a function

Hello All. I’ve been trying to make a speaker beep three times within a function but have been unsuccessful in different variations of coding. With this code:

void beep(){
  //Serial.println(detect_333);
  if(beep){
    Serial.print ("IN BEEP AND GO BEEP = ");
    Serial.println (go_beep);
    for(int i=0; i=beeps && go_beep; i++){
      tone(3, 1000, 500);
      Serial.println (i);
      Serial.print ("GO BEEP = ");
      Serial.println (go_beep);
      beep_pause = millis();
      if((millis() - beep_pause) > 500){
        Serial.println (millis());
        Serial.print (beep_pause);
        go_beep = true;
      }
      else{
        go_beep = false;
      }
    }
  }
}

This is what I get on the serial monitor every time the function is called:

IN BEEP AND GO BEEP = 1
1
GO BEEP = 1
IN BEEP AND GO BEEP = 0
IN BEEP AND GO BEEP = 0
IN BEEP AND GO BEEP = 0
IN BEEP AND GO BEEP = 0
IN BEEP AND GO BEEP = 0
…and so on
it beeps one time the first time the function is called after a reset then doesn’t beep again.

With this code:

void beep(){
  //Serial.println(detect_333);
  if(beep){
    Serial.print ("IN BEEP AND GO BEEP = ");
    Serial.println (go_beep);
    for(int i=0; i=beeps && go_beep; i++){
      tone(3, 1000, 500);
      Serial.println (i);
      Serial.print ("GO BEEP = ");
      Serial.println (go_beep);
      beep_pause = millis();
      go_beep = false;
      }
      if((millis() - beep_pause) > 500){
        Serial.println (millis());
        Serial.println (beep_pause);
        go_beep = true;
    }
  }
}

It beeps every other time the function is called and the serial monitor displays:

IN BEEP AND GO BEEP = 1
1
GO BEEP = 1
IN BEEP AND GO BEEP = 0
17197
7192
IN BEEP AND GO BEEP = 1
1
GO BEEP = 1
IN BEEP AND GO BEEP = 0
52717
28704
IN BEEP AND GO BEEP = 1
1
GO BEEP = 1
IN BEEP AND GO BEEP = 0
66729
59724
…and so on.

How can millis() and a for loop be used within a function? Thanks

The complete code is below:

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
/*
This is the corresponding sketch to the 'basicSend' sketch.
 the nrf24l01 will listen for numbers 0-255, and light the red LED
 whenever a number in the sequence is missed.  Otherwise,
 it lights the green LED
 */
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int red = 14;
int green = 15;
int yellow = 16;
byte yellow_relay = 19;
byte green_relay = 18;
byte red_relay = 17;
float time_111 = 0;
float time_222 = 0;
float time_333 = 0;
byte beep_pin = 3;
boolean detect_111 = false;
boolean detect_222 = false;
boolean detect_333 = false;
float new_111_detect_time = 0;
float old_111_detect_time = 0;
float new_222_detect_time = 0;
float old_222_detect_time = 0;
float new_333_detect_time = 0;
float old_333_detect_time = 0;
float max_time_111 = 0;
float max_time_222 = 0;
float max_time_333 = 0;
unsigned long old_update_time = 0;
unsigned long new_update_time = 0;
int update_interval = 2000;
int led_flash_on = 500;
boolean yellow_led_started;
unsigned long yellow_led_start;
byte beeps = 0;
boolean detect = false;
unsigned long relay_start;
unsigned long beep_pause;
boolean go_beep = true;

void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red_relay, OUTPUT);
  pinMode(green_relay, OUTPUT);
  pinMode(yellow_relay, OUTPUT);
  pinMode(3, OUTPUT);
}
void report(){
  Serial.print("     Max 111 Time = ");
  Serial. print(max_time_111);
  Serial.print("     Max 222 Time = ");
  Serial. print(max_time_222);
  Serial.print("     Max 333 Time = ");
  Serial. println(max_time_333);
}

void flash_red_led (unsigned long red_led_start = 0, boolean red_led_started = false){
  if(!red_led_started){
    red_led_start = millis();
    digitalWrite (red, HIGH);
    red_led_started = true;
  }
  if(millis() - red_led_start > led_flash_on){
    digitalWrite (red, LOW);
    red_led_started = false;
  }
  Serial.print(millis());
  Serial. print(red_led_start);
}

void flash_green_led (unsigned long green_led_start = 0, boolean green_led_started = false){
  if(!green_led_started){
    green_led_start = millis();
    digitalWrite (green, HIGH);
    green_led_started = true;
  }
  if(millis() - green_led_start > led_flash_on){
    digitalWrite (green, LOW);
    green_led_started = false;
  }
}
void flash_yellow_led(){
  if(detect_333 && !yellow_led_started){
    yellow_led_start = millis();
    digitalWrite (yellow, HIGH);
    yellow_led_started = true;
    beep();
  }
  if((millis() - yellow_led_start) > led_flash_on){
    digitalWrite (yellow, LOW);
    yellow_led_started = false;
  }
  detect_333 = false;
  beeps = 3;
}
void beep(){
  //Serial.println(detect_333);
  if(beep){
    Serial.print ("IN BEEP AND GO BEEP = ");
    Serial.println (go_beep);
    for(int i=0; i=beeps && go_beep; i++){
      tone(3, 1000, 500);
      Serial.println (i);
      Serial.print ("GO BEEP = ");
      Serial.println (go_beep);
      beep_pause = millis();
      go_beep = false;
      }
      if((millis() - beep_pause) > 500){
        Serial.println (millis());
        Serial.println (beep_pause);
        go_beep = true;
    }
  }
}

void energize_relay(){
  if (detect){
    digitalWrite(red_relay,HIGH);
    relay_start = millis();
  }
  if ((relay_start + 3000) >= millis());
  {
    digitalWrite(red_relay, LOW);
  }
}

void loop(void){
  if (radio.available()){
    bool done = false;  
    while (!done){
      done = radio.read(msg, 2);

      if (msg[0] == 111){
        //digitalWrite(red, HIGH);
        flash_red_led();
        detect = true;
        detect_111 = true;
        new_111_detect_time = millis();
        time_111 = (new_111_detect_time - old_111_detect_time)/1000.00;
        old_111_detect_time = new_111_detect_time;
        if (time_111 > max_time_111){
          max_time_111 = time_111;
        }
      }    
      else {
        digitalWrite(red,LOW);

      }
      if (msg[0] == 222){
        //digitalWrite(green, HIGH);
        flash_green_led();
        detect = true;
        detect_222 = true;
        new_222_detect_time = millis();
        time_222 = (new_222_detect_time - old_222_detect_time)/1000.00;
        old_222_detect_time = new_222_detect_time;
        if (time_222 > max_time_222){
          max_time_222 = time_222;
        }
      }    
      //else {
      //digitalWrite(green,LOW);

      //}
      if (msg[0] == 77){
        // digitalWrite(yellow,HIGH);
        //flash_yellow_led();
        detect = true;
        detect_333 = true;
        new_333_detect_time = millis();
        time_333 = (new_333_detect_time - old_333_detect_time)/1000.00;
        old_333_detect_time = new_333_detect_time;
        if (time_333 > max_time_333){
          max_time_333 = time_333;
        }
      }    
    }
  }
  if ((millis() - old_update_time) > update_interval){
    //report();

    old_update_time = millis();
  }

  //energize_relay();
  flash_yellow_led();
  //beep();

}
beep_pause = millis();
if((millis() - beep_pause) > 500){

is equivalent to

if ( 0 > 500 )

Which is obviously never true :stuck_out_tongue:

something like this?

compiled but not tested…

#define TONE_PIN 5
boolean beeping = false;

void setup()
{
  Serial.begin(9600);
  beeping = true;
}
void loop()
{
  beep(500);// duration
}

void beep(unsigned long duration)
{
  static unsigned long lastBeep;
  static int counter = 1;
  if (beeping)
  {
    if (millis() - lastBeep < duration)
    {
      tone(TONE_PIN, 1000);
    }
    else if (millis() - lastBeep < duration*2)
    {
      tone(TONE_PIN, 0);
    }
    else
    {
      Serial.println("Beep");
      counter++;
      if (counter > 3)
      {
        counter = 1;
        beeping = false;
      }
    }
  }
}

whenever you make beeping true, it will then beep 3 times…

try to figure out how to use a wifi shield and connect to the internet as a client when a button is pressed, without messing up the timing of the led blinker. If the client can't connect it freezes everything for 10 seconds.

Now that's a pain to figure out. ----------- Still haven't figured it out.

Thomas499:
try to figure out how to use a wifi shield and connect to the internet as a client when a button is pressed, without messing up the timing of the led blinker. If the client can't connect it freezes everything for 10 seconds.

Now that's a pain to figure out. ----------- Still haven't figured it out.

I don't understand how this is supposed to help @scottyjr ?

...R