invalid use of void expression when trying to use timer library

Hi there Arduino Forumites!

I’m trying to get my first Arduino project together, itś a defusable fake bomb to use on my local airsoft field, and naturaly I need some sorts of timers for a bunch of things, so Iḿ using the timer library by Simon Monk, but I can quite get it to work, so some helt would be much apreciated!

I have managed to get a led to blink when the bomb is armed, but I can’t figure out how to use after to set a countdown to detonation.

I have created a function called detonate that is supposed to be called after X (entered by user) time has passed, but Iḿ doing something wrong when Iḿ calling the function (I think) since I get the invalid use of void expression message when compiling for this line:

t.after(detonationMilli, detonation());

and this is the function:

void detonation(){

digitalWrite(buzzer,HIGH);
delay(10000);
digitalWrite(buzzer,LOW);

}

(I know I shouldn’t use delay in the same sketch as timer, but I was using pulse, and thought that might be the problem, so I changed it to a delay, but with the same results.

Sketch is attached below!

KeypadAndLCDTest.ino (4.86 KB)

Don’t try to use the value a void function doesn’t return.

Could you elaborate?

 t.after(detonationMilli, detonation());

This calls detonation() first, then tries to pass the value it returns (which is void, i.e. no value) to t.after().

This will pass the function to t.after():

 t.after(detonationMilli, detonation);

Thanks you for your reply!

I tried that before though and got the following message as a result:

KeypadAndLCDTest.ino: In function ‘void setup()’:
KeypadAndLCDTest.ino:127:36: error: no matching function for call to ‘Timer::after(long int&, void (&)())’
In file included from KeypadAndLCDTest.ino:2:0:
/home/pi/sketchbook/libraries/timer2_1/Timer.h:42:10: note: candidate: int8_t Timer::after(long unsigned int, void ()(void), void*)
int8_t after(unsigned long duration, void (callback)(void), void* context);
^
/home/pi/sketchbook/libraries/timer2_1/Timer.h:42:10: note: candidate expects 3 arguments, 2 provided

error: no matching function for call to ‘Timer::after(long int&, void (&)())’

That looks pretty clear.

If only I could see your code. {sigh}

Well, Iḿ a beginner at this and programming in general, so for me itś not clear at all, so I would be very greatful if you could explain it to me!

I attached the code in the original post but I’ll paste it here to:

#include <Event.h>
#include <Timer.h>

#include <Keypad.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
String password=("");
String number;
String detonationTime=("");
long detonationMilli;
Timer t;
long PERIOD1 = 100;
#define ledpin 13
int buzzer=2;

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
pinMode(ledpin,OUTPUT);
t.oscillate(ledpin, PERIOD1, HIGH);

digitalWrite(ledpin, HIGH);
Serial.begin(9600);
lcd.begin (16,2);

// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear (); // go home

// Menu

lcd.print(“Enter new code:”);

while (number.length() < 10)
{
lcd.setCursor (0,1);
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘*’:
digitalWrite(ledpin, LOW);
break;
case ‘#’:
digitalWrite(ledpin, HIGH);
break;
default:
number=number+key;
lcd.print(number);

}
}

}
lcd.clear();
lcd.print(“New code set:”);
lcd.setCursor (0,1);
lcd.print(number);
delay(5000);
lcd.clear();
lcd.print(“Detonation time:”);

bool myBool=true;

while (myBool)
{
lcd.setCursor (0,1);
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '’:
detonationMilli = detonationTime.toInt();
detonationMilli = detonationMilli
1000;
myBool=false;
break;
case ‘#’:
detonationMilli = detonationTime.toInt();
detonationMilli = detonationMilli*1000;
myBool=false;
break;
default:
detonationTime=detonationTime+key;
lcd.print(detonationTime);

}
}

}
t.after(detonationMilli, detonation);

}

void loop()
{
password=("");
lcd.clear();
lcd.setCursor (1, 0);
lcd.print(“Enter code to”);
lcd.setCursor (1, 1);
lcd.print(“detonate bomb”);

while (password != number)

{

char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘*’:
digitalWrite(ledpin, LOW);
break;
case ‘#’:
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
password=password+key;
lcd.clear();
lcd.print(password);

if (password.length() > 9 && password != number)
{
password=("");
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(“Incorrect code”);
delay(3000);
lcd.clear();
lcd.setCursor (1, 0);
lcd.print(“Enter code to”);
lcd.setCursor (1, 1);
lcd.print(“detonate bomb”);
continue;

}

}
}
}

if(password == number)
{

detonationTimer.update();
password=("");
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(“Detonation in:”);
lcd.setCursor(0, 1);
lcd.print(detonationMilli);

while (password != number)

{

t.update();
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘*’:
digitalWrite(ledpin, LOW);
break;
case ‘#’:
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
password=password+key;
lcd.clear();
lcd.print(password);

if (password.length() > 9 && password != number)
{
password=("");
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(“Incorrect code”);
delay(3000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(“Detonation in:”);
lcd.setCursor(0, 1);
lcd.print(detonationMilli);
continue;

}

}
}
}

if (password == number)
{

lcd.clear();
lcd.setCursor (4, 0);
lcd.print(“Bomb has”);
lcd.setCursor (2, 1);
lcd.print(“been defused”);
delay(10000);
}
}

}
void detonation(){

digitalWrite(buzzer,HIGH);
delay(10000);
digitalWrite(buzzer,LOW);

}

The library comes with examples, I assume.
Play "spot-the-difference" with examples' callbacks and yours.

And please, remember to use code tags when posting code.

I went trough all the examples backwards and forward and googled my ass off for 2 days before posting here, but I just managed to solve it by adding NULL when I call the function, and (void* context) to the function. Don't know why is works but it does.

Because this 42:10: note:  candidate expects 3 arguments, 2 provided