From 0586775ffc93906c48b205807a471cbaa0026406 Mon Sep 17 00:00:00 2001 From: Abhishek Malik Date: Fri, 23 Oct 2015 11:35:11 -0700 Subject: [PATCH] Java: Adding example for Grove MQ9 Gas Sensor Signed-off-by: Abhishek Malik --- examples/java/GroveMQ9.java | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/java/GroveMQ9.java diff --git a/examples/java/GroveMQ9.java b/examples/java/GroveMQ9.java new file mode 100644 index 00000000..2c90b7d6 --- /dev/null +++ b/examples/java/GroveMQ9.java @@ -0,0 +1,58 @@ +import upm_gas.Gas; +import upm_gas.MQ2; +import upm_gas.MQ3; +import upm_gas.MQ4; +import upm_gas.MQ5; +import upm_gas.MQ6; +import upm_gas.MQ7; +import upm_gas.MQ8; +import upm_gas.MQ9; +import upm_gas.TP401; +import upm_gas.thresholdContext; + +public class GroveMQ9 { + + static { + try { + System.loadLibrary("javaupm_gas"); + System.loadLibrary("mraajava"); + } catch (UnsatisfiedLinkError e) { + System.err.println( + "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + + e); + System.exit(1); + } + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + //! [Interesting] + // initialize the sensor on A0 + MQ9 mq9 = new MQ9(0); + + short[] buffer = new short[128]; + + thresholdContext context = new thresholdContext(); + + context.setAverageReading(0); + context.setRunningAverage(0); + context.setAveragedOver(2); + + int len; + int thres; + while(true){ + len = mq9.getSampledWindow((long)2, buffer); + + if(len != 0){ + thres = mq9.findThreshold(context, 30, buffer); + mq9.printGraph(context, (short)5); + if(thres != 0){ + // do something + System.out.println("threshold is crossed"); + } + } + } + //! [Interesting] + } + +} \ No newline at end of file