You are reading a single comment by @Rek and its replies.
Click here to read the full conversation.
-
@allObjects This is all very useful stuff, thanks for the detail. I am using the standard board, I am feeling my way through the world of electronics and loving how easy it is to get results (using espruino), I now have the led strip reacting to distance from the sensor.
I will make sure to keep well clear of the batt connector until I am ready to go stand alone with my contraption.
var distance = 0; var running; var sensor = require("HC-SR04").connect(A2,A3,function(dist) { distance = dist; }); SPI2.setup({baud:3200000, mosi:B15}); function runIt(){ sensor.trigger(); console.log("response "+Math.floor(distance)+" cm away"); if (distance < 10){ SPI2.send4bit([255,255,255], 0b0001, 0b0011); // white }else if(distance <= 20){ SPI2.send4bit([255,0,0], 0b0001, 0b0011); // red }else if(distance <= 30){ SPI2.send4bit([0,255,0], 0b0001, 0b0011); // green }else if(distance <= 40){ SPI2.send4bit([0,0,255], 0b0001, 0b0011); // blue }else{ SPI2.send4bit([0,0,0], 0b0001, 0b0011); // black } } function stopIt(){ console.log("button pressed"); clearInterval(running); } running = setInterval(runIt, 100); setWatch(stopIt, BTN1, {repeat:true, edge:"rising"});
;)
Now I know that you later plan to run your stuff disconnected from USB, just off of your 5V power supply, correct?
You can do that final configuration already now and still have only 4 connections between Espruibo and the distance sensor device 'exactly' as described on the HC-SR04 page (except that you use A2 rather A1 for capturing the Echo from the device).
If you have an Espruino standard board, you connect your power supply to the white Battery connecter in the corner of the standard board next to B2 pin. Make sure that (-)GND of the power supply goes to (-)GND to (-)(GND) of the battery connector, and (+)5V to the (+) of the battery connector. 'NEVER' connect the power supply 5V(+) to the Bat pin of the board, even though there are situations with such a connection that do not lead to disaster (your power supply delivers 5V and that is more than what comes from 5V USB through the blocking diode - 4.7/4.3V, but when our power supply delivers less or has another issue, USB will try to feed it and that leads to all sorts of unesirable (final fatal) results, even though there is a 1000mA fuse in the circuit). With connecting to the Battery connector you can have safely run USB connected parallel to your power supply.
If ou have an Espruino Pico - and I assume that's the case - you connect your power supply's (-)GND to the Pico GND pin between to the USB tab and VBat pin - the GND pin is the first pin on the right sight looking from top and USB tab, and you connect the power supply's (+)5V to the BAT_IN pin between the USB tab and pin B15 - the BAT_IN pin is the first pin next to the TAB opposite the GND pin. Again, do not connect the power supply 5V(+) to Pico's VBAT pin beteween GND and 3.3V pin, for the same reasons as describe for the standard board, with a small difference: there is no fuse that can prevent disaster, not even for a limited time...
Now, connected to and run from your power supply, you add an onInit() as described below, then you upload the code, type
save()
into the console, and you are all set:Disconnecting USB, pulling the power supply from the wall socket (or what ever allows you to shut it off), and then repowering with the power supply will start run your code... ;)
Having added the variable to hold on to the intervalTimer returned by setInterval() allows you to stop the triggering by software:
and to restart it calling
Since you are an old hand, you will say that you do not like to call onInit() for restarting the triggering at all, because in your final (SW) setup you will have other things in the onInit() which should be called only exactly once... Said so, you create two functions, start() and stop() - start setting the intervall, and stop clearing the interval - and invoke start() in the onInit(). To be on the defensive side your stop looks then like this:
If this is a waste of breath on you because you are already beond that, just ignore the code litany.... ;-)