Adding a delay or wait component in a offline connection

Hi,
I am trying to use a MetaWear c sensor to detect shocks applied to a body using accelerometer and turn a LED on for 4 seconds. And in this case, sensor should detect the shocks in offline mode. I developed a code with the help of freefall example and react component to detect and turn the led on.
But now I need to stop detecting any more shocks while the LED is on and resume detecting shocks when LED is off. I couldn't figure out how to achieve this.

Here is the code snippet I used to configure the MetaWear Sensor.

return accelerometer.acceleration().addRouteAsync(new RouteBuilder() {
@Override
public void configure(RouteComponent source) {
source.map(Function1.RMS).average((byte) 4).filter(ThresholdOutput.BINARY, 0.8)
.filter(Comparison.EQ, 1)
.react(new RouteComponent.Action() {
@Override
public void execute(DataToken token) {
led.editPattern(Led.Color.GREEN)
.riseTime((short) 0)
.pulseDuration((short) 1000)
.repeatCount((byte) 4)
.highTime((short) 500)
.highIntensity((byte) 16)
.lowIntensity((byte) 16)
.commit();
led.play();
}
});
}
});

Can anyone please help me regarding this ?
Thanks.

Comments

  • Wrap your code with Markdown code tags so it renders correctly

  • edited November 2018

    Hi Eric,
    Please find my code below. It would be great if you can help me on this matter.
    `board.connectAsync().onSuccessTask(new Continuation<Void, Task>() {
    @Override
    public Task then(Task task) {

                accelerometer = board.getModule(Accelerometer.class);
                accelerometer.configure()
                        .odr(60f)
                        .commit();
    
                led = board.getModule(Led.class);
    
                return accelerometer.acceleration().addRouteAsync(new RouteBuilder() {
                    @Override
                    public void configure(RouteComponent source) {
                        source.map(Function1.RMS).average((byte) 4).filter(ThresholdOutput.BINARY, 0.8)
                                .filter(Comparison.EQ, 1)
                                .react(new RouteComponent.Action() {
                                    @Override
                                    public void execute(DataToken token) {
                                        led.editPattern(Led.Color.GREEN)
                                                .riseTime((short) 0)
                                                .pulseDuration((short) 1000)
                                                .repeatCount((byte) 4)
                                                .highTime((short) 500)
                                                .highIntensity((byte) 16)
                                                .lowIntensity((byte) 16)
                                                .commit();
                                        led.play();
                                    }
                         });
                    }
                });
            }
        }).continueWith(new Continuation<Route, Void>() {
            @Override
            public Void then(Task<Route> task) {
                if(task.isFaulted()) {
                    ((TextView)findViewById(R.id.connStatus)).setText("Connection Failed !");
                } else {
                    ((TextView)findViewById(R.id.connStatus)).setText(board.getModel() + " Connected");
                }
                return null;
            }
        });`
    
  • Hi All,
    Can I start or stop a task created using timer from inside a MetaWear react component. When I try to implement that MetaWear board fails to configure.

    Below is my code

    `board.connectAsync().onSuccessTask(new Continuation<Void, Task>() {
    @Override
    public Task then(Task task) {

                accelerometer = board.getModule(Accelerometer.class);
                accelerometer.configure()
                        .odr(60f)
                        .commit();
    
                led = board.getModule(Led.class);
    
                timer = board.getModule(Timer.class);
    
                return accelerometer.acceleration().addRouteAsync(new RouteBuilder() {
                    @Override
                    public void configure(RouteComponent source) {
                        source.map(Function1.RMS).average((byte) 4).filter(ThresholdOutput.BINARY, 0.8)
                                .filter(Comparison.EQ, 1)
                                .react(new RouteComponent.Action() {
                                    @Override
                                    public void execute(DataToken token) {
    
                                        led.editPattern(Led.Color.BLUE, Led.PatternPreset.SOLID).commit();
                                        led.play();
    
                                        accelerometer.acceleration().stop();
                                        timer.lookupScheduledTask((byte) 0).start();
    
                                    }
                                });
    
                    }
                });
            }
        }).continueWith(new Continuation<Route, Void>() {
            @Override
            public Void then(Task<Route> task) {
                if(task.isFaulted()) {
                    ((TextView)findViewById(R.id.connStatus)).setText("Connection Failed !");
                } else {
                    ((TextView)findViewById(R.id.connStatus)).setText(board.getModel() + " Connected");
                }
                return null;
            }
        });
    }
    public Task<Timer.ScheduledTask> scheduleRead(final ForcedDataProducer producer) {
        return timer.scheduleAsync(20000, true, new CodeBlock() {
            @Override
            public void program() {
                accelerometer.acceleration().start();
                led.stop(true);
                timer.lookupScheduledTask((byte) 0).stop();
            }
        });
    }`
    
  • Markdown code blocks are 3 ticks not 1. Please fix your posts and double check that your code blocks are rendering correctly.

    Post the exceptions / error messages generated by your timer code.

This discussion has been closed.