Beginner - Asynchronous function calls

Hello there. I am new to the forums, and the whole arduino scene. I’m not sure how to approach this properly, so I thought you guys could help me out.
This is my first small project handling both input and output, and I am working with a LDR to detect the amount of light in a room and triggering an alarm accordingly, which is basically 1 multicolored LED (RGB) and playing a beep sound through soundalarm().

I’m going to try and post some code here:

//start vars
int duration;
int pin;
int lightamount;

//lights
int blue = 13;
int green = 12;
int red = 11;

void setup(){
  //set output pins
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  
  //prepare to read analog signal
  Serial.begin(9600);
}

void loop(){
  lightamount = lightinput();
  
  if(lightamount > 210){
    soundalarm();
    cyclergb(500);
  }
  
}

void cyclergb(int duration){
  lightup(blue, duration); lightup(red, duration); lightup(green, duration);  
}

//outputs 1 to PIN for DURATION milliseconds
void lightup(int pin, int duration){
  digitalWrite(pin, HIGH);
  delay(duration);
  digitalWrite(pin, LOW);
}

//shuts down PIN
void lightdown(int pin){
  digitalWrite(pin, LOW);
}

//returns the amount of light going into the LDR
int lightinput(){
  int sensorvalue = analogRead(A0);
  Serial.println(lightamount);
  return sensorvalue;
}

Here’s the soundalarm() function. It’s rip from the arduino examples. I’ve only changed the digital pin and the notes it plays:

/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 10
 
 created 21 Jan 2010
 modified 14 Oct 2010
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h" (this is just a file with defines())

// notes in the melody:
int melody[] = {
  NOTE_F7, NOTE_F7, NOTE_F7};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void soundalarm() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(10, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.00;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(10);
  }
}

The good news is: it’s working. However, one of the functions waits for the other, so it basically plays the sound first and cycles the LED after the alarm.

Help appreciated :slight_smile:
Thanks in advance

Where's the code for soundalarm()?

Assuming it just toggles a pin then you could do something like this

if(lightamount > 210){
    digitalWrite(alarmPin, HIGH);
    cyclergb(500);
    digitalWrite(alarmPin, LOW);
  }

Rob

Graynomad:
Where's the code for soundalarm()?

Assuming it just toggles a pin then you could do something like this

if(lightamount > 210){

digitalWrite(alarmPin, HIGH);
    cyclergb(500);
    digitalWrite(alarmPin, LOW);
  }



______
Rob

Thanks for the input Graynomad. I've added the soundalarm() to my first post;

Ooo, it doesn't just toggle a pin does it?

I can see no simple way to integrate the two functions, and I'm guessing nobody else can either as they haven't come forward.

If this is a serious project it would be worth applying some deep thought, but if it's just for fun I'd probably move on.

I will have a closer look but don't hold your breath :slight_smile:


Rob

Two things come to mind, maybe 3. I'm beating my head against Perl (long time since I wrote much of that, and it wasn't a lot) so not much depth here.

I recall reading on the forums recently about using the timer functions to blink an LED. Not sure what capabilities are there in terms of duty cycle, but it might be worth looking into.

IIRC, there are calls you can use to set PWM to frequency and duty cycle other than the defaults.

Maybe the softPWM library? I haven't looked at it in detail, it would need to operate asynchronously in order to start the blinky, then you play your alarm, then stop the blinky.

Of course, you can always use hardware to blink your LEDs. Or, there are "blinker" LEDs you can buy.

Thank you both for all the input. I will give it a go with the blink, and post back the output.
It is not a very serious project, so there's nothing to worry about; anyways, the question arose, so, we could all benefit from it.

Thanks again, will report back soon :slight_smile: