Last active
April 23, 2016 22:07
-
-
Save hendrahc/9d85a9f6d06bc22c5ea9190e96c376e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var upmBuzzer = require("jsupm_buzzer"); | |
var mBuzzer = new upmBuzzer.Buzzer(3); //Buzzer for sound notification | |
var app = require('express')(); //Express Library | |
var server = require('http').Server(app); //Create HTTP instance | |
var io = require('socket.io')(server); //Socket.IO Library | |
var mraa = require('mraa'); | |
var LCD = require ('jsupm_i2clcd'); //LCD to display price & color notification | |
console.log('Current version of MRAA is', mraa.getVersion()); | |
var touch = new mraa.Aio(0); //touch sensor for input testing | |
var touchValue; | |
var lcdMessage=" "; | |
var myLCD = new LCD.Jhd1313m1(6, 0x3E, 0x62); | |
var myLED = new mraa.Gpio(2); | |
myLED.dir(mraa.DIR_OUT); | |
var price = "20.000"; //sample price | |
myLCD.setColor(0,0,255); | |
var success = true; | |
loop(); | |
function loop(){ | |
lcdMessage = price; | |
showDisplay(myLCD, "TOTAL",price); | |
var readTouch = touch.read(); | |
readTouch = Math.round(readTouch*.1); | |
mBuzzer.stopSound(); | |
if(touchValue != readTouch){ | |
touchValue = readTouch; | |
console.log(lcdMessage); | |
if(touchValue > 50){ | |
success = !success; //alternate between success and fail | |
if(success){ | |
successNotif(mBuzzer,myLED, myLCD); | |
}else{ | |
failNotif(mBuzzer,myLED, myLCD); | |
} | |
} | |
} | |
setTimeout(loop,100); | |
} | |
function showDisplay(LCD,msg1, msg2){ | |
//update text display on LCD | |
LCD.clear(); | |
LCD.setCursor(0,1); | |
LCD.write(msg1); | |
LCD.setCursor(1,15 - msg2.length); | |
LCD.write(msg2); | |
} | |
function successNotif(Buzzer, LED, LCD){ | |
//give notification when transaction is success | |
LED.write(1); | |
LCD.setColor(0,255,0); | |
showDisplay(LCD,"SUCCESS!!", "Thank You :)"); | |
Buzzer.playSound(1000, 100000); | |
Buzzer.playSound(2000, 200000); | |
Buzzer.stopSound(); | |
LCD.setColor(0,0,255); | |
LED.write(0); | |
} | |
function failNotif(Buzzer, LED, LCD){ | |
//give notification when transaction is failed | |
LCD.setColor(255,0,0); | |
showDisplay(LCD,"Transaction","ERROR :("); | |
Buzzer.playSound(20000, 600000); | |
Buzzer.stopSound(); | |
LCD.setColor(0,0,255); | |
LED.write(0); | |
} | |
//listener for price input | |
io.on('connection', function(socket){ | |
console.log("come here"); | |
socket.on('changePrice', function(data){ //on incoming websocket message... | |
price = data; //update blink interval | |
}); | |
}); | |
server.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment