Solenoid Engine – Microbit – Micro Python – Speed Controls

<br />
# Solenoid Engine Simple Script - govener<br />
from microbit import *</p>
<p># asking for a power stroke<br />
ison = 0<br />
# saved state of is on<br />
last_ison = 0<br />
# ready for next pulse<br />
ready = 1<br />
# time of last pulse<br />
pulsetime = 0</p>
<p># define speed limits<br />
MAXSPEED = 100<br />
MINSPEED = 1500</p>
<p># speed in mS between pulses (smaller is faster)<br />
Speed = 500</p>
<p>while True:<br />
    # if pin2.read_analog() &lt; 400: if V1 Engine<br />
    if pin2.read_analog() &gt; 600:<br />
        ison = 1<br />
    else:<br />
        ison = 0</p>
<p># start of stroke check speed<br />
# had a pulse if time ready next pulse<br />
    if ison == 1 and last_ison == 0:<br />
        if (running_time()-pulsetime) &gt; Speed:<br />
            ready = 1<br />
            display.show(Image.DIAMOND)<br />
# if pulse needed turn on solenoid<br />
    if ready == 1:<br />
        pin1.write_digital(1)<br />
        pulsetime = running_time()<br />
        if ison == 0:<br />
            ready = 0<br />
            display.show(Image.HAPPY)<br />
    else:<br />
        pin1.write_digital(0)</p>
<p># set last condition<br />
    last_ison = ison</p>
<p>    if button_a.is_pressed():<br />
        if Speed &lt; MINSPEED:<br />
            Speed = Speed + 1<br />
            display.show(Image.ARROW_S)</p>
<p>    if button_b.is_pressed():<br />
        if Speed &gt; MAXSPEED:<br />
            Speed = Speed - 1<br />
            display.show(Image.ARROW_N)<br />