Noor Hashem

@noorhashem

A Crypto-Powered Alarm You Can’t Snooze!

July 3rd 2019

If this looks like your typical alarm app screen, then you need some help waking up!

The maker community never ran out of alternative hardware solutions for our daily struggles that are fun, tweakable and of a challenging nature.

Some tried to wake up like this:

However, research in the dynamics of sleep and the science behind “how to wake up to your alarm” can be dedicated to replicating the crypto-powered alarm clock that will force you to wake up or you will end up losing a fortune :D. It can also be a good intro to tinker with Ethereum and get your hands dirty with some hardware and blockchain concepts.

The alarm uses the Elk blockchain development board and a sound sensor. It’s sound activated, which means you can use it right away with any of your personal alarms, without too much hassle or a lot of tweaks. When the sound sensor detects the ringing of your alarm, it starts counting within a trial of time (That you set in your code), in this trial you have to get up and shut it down by a hardware action of your choice or simply pressing a button.

What happens is, initially an amount of Ether is locked in a smart contract every time the alarm fires. the smart contract is controlled only depending on the state of your hardware. Whether you’ve managed to fulfill the hardware action within the trial’s time frame or failed to; the board keeps counting down. If you did it before the trial ends, then you don’t lose the designated Ether you locked from your account and it’s withdrawn back to your account. If not, then you lose it entirely and it’s sent to any charity address of your choice.

The smart contract functions used are :

Link to code snippet: https://pastebin.com/R585FCsp

Typical Arduino C was used for the rest of the logic for the alarm on Platform.io IDE which supports the Elk SDK. The sound sensor is associated with an interrupt pin, and once it signals for sound detection (LOW signal from the sensor ), the interrupt occurs and starts the millis() counter for the trial and locks your money in the smart contract using

function lockMyMoney(address _alarmAddress) public payable{
userComittedMoney[msg.sender] = msg.value;
alarmAddress[msg.sender] = _alarmAddress;

If you push the button to silence the alarm, another interrupt occurs and sets the Win flag to true and withdraws your money back

function withdrawMoney(address _alarmAddress) public {
require(alarmAddress[msg.sender] == _alarmAddress && userComittedMoney[msg.sender]>0);
msg.sender.transfer(userComittedMoney[msg.sender]);

If you lose, the money goes to the predefined charity address

function spend(address _alarmAddress, uint _amount) public{
require(userComittedMoney[msg.sender] >= _amount && alarmAddress[msg.sender]== _alarmAddress);
charityAddress.transfer(_amount);
userComittedMoney[msg.sender]-=_amount;

The code where the actions are handled would be as follows.

ContractMethodParam param1("AlarmAddress");
ContractMethodParam param2(AMOUNT_OF_MONEY_IN_GWEI);
void loop(){
if(win) //winning condition
{
win = false;
Ethereum.sendContractMethodCallTransaction("m/44'/60'/0'/0/0",CONTRACT_ADDRESS,AMOUNT_OF_MONEY_IN_GWEI,50000,20,"withdrawMoney(address)",&param1);
display.clearDisplay();
display.fillScreen(BLACK);
display.setCursor(37,7);
display.println(“WINNER!”);
display.setCursor(20,30);
display.println(“You’ve Earned”);
display.setCursor(20,40);
display.println(“0.016 ETH”);
display.display();
for(int i = 0; i<=3 ; i++)
analogWrite(LedPin, 90);
delay(500);
analogWrite(LedPin, 0);
delay(500);
}
if (!win && millis()-timer >= trial ) //losing condition
{
//You LOSING money!
Ethereum.sendContractMethodCallTransaction("m/44'/60'/0'/0/0",CONTRACT_ADDRESS,AMOUNT_OF_MONEY_IN_GWEI,50000,20,"spend(address,uint)",&param1,&param2);
display.clearDisplay();
display.fillScreen(BLACK);
display.setCursor(35,7);
display.println(“SLEEPY HEAD!”);
display.setCursor(33,30);
display.println(“You’ve lost”);
display.setCursor(33,40);
display.println(“0.016 ETH”);
delay(3000);
display.clearDisplay();
display.fillScreen(BLACK);
display.display();
}

And finally, a simple and sleek enclosure was made on Fusion 360 to encompass all the components in a presentable form.

This alarm leaves you no option but to either wake up on time or be a generous person, and in both cases, you’ve done something good whether for yourself or for the world.

Building blockchain-connected devices is made simple with Elk. Elk will be on Kickstarter soon, to get your hands on one once it’s out, sign up here.

More Related Stories