Hi Peter, > This quite long because the guy that wrote it documented every step.
And because this chunk in the middle is repeated. :-) > frequency_hertz = 50 > pwm = GPIO.PWM(pin_number, frequency_hertz) > > > # How to position a servo? All servos are pretty much the same. > # Send repeated purses of an absolute duration (not a relative duty cycle) > # between 0.40 ms and 2.5 ms in duration. A single pulse will only move it > # a short distance in the desired direction. Repeated pulses will continue > # its movement and then once it arrives at the specified position it will > # insruct the motor to forcefully hold its position. > left_position = 0.40 > right_position = 2.5 > middle_position = (right_position - left_position) / 2 + left_position > > # I'll store a sequence of positions for use in a loop later on. > positionList = [left_position, middle_position, right_position, > middle_position] > > # total number of milliseconds in a a cycle. Given this, we will then > # know both how long we want to pulse in this cycle and how long tghe > # cycle itself is. That is all we need to calculate a duty cycle as > # a percentage. > ms_per_cycle = 1000 / frequency_hertz > Note his ordering of using `pwm' in the loop, stopping afterwards, and then `cleaning up' GPIO. > for i in range(3): > for position in positionList: > duty_cycle_percentage = position * 100 / ms_per_cycle > pwm.start(duty_cycle_percentage) > time.sleep(.5) > > pwm.stop() > GPIO.cleanup() And compare it with yours where the `pwm' is in a callback function to handle the URL, > @app.route('/motor-forwards/') > def motorforwards(): > duty_cycle_percentage = left_position * 100 / ms_per_cycle > pwm.start(duty_cycle_percentage) > time.sleep(.5) > pwm.stop() > return render_template('index.html') And the GPIO clean-up is after that definition, just before you decide to hand control over to Flask by calling app.run(). > GPIO.cleanup() > > if __name__ == '__main__': > app.run(debug=True, host='0.0.0.0') You're cleaning up the GPIO before Flask gets to run and that probably undoes all your GPIO and PWM configuration. Assuming run() can return, I forget, move the clean up: if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') GPIO.cleanup() -- Cheers, Ralph. -- Next meeting: BEC, Bournemouth, Tuesday, 2019-07-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk