Teacher notes: Solution to the challenges Pro Preview

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #870
  • 04 May 2018

Car stays and starts moving with а button press

#Define PWM functions
forward = GPIO.PWM(forwardPin, frequency)
backward = GPIO.PWM(backwardPin, frequency)
left = GPIO.PWM(leftPin, frequency)
right = GPIO.PWM(rightPin, frequency)
# initialize in stopped position. Helps to avoid problems
forward.stop()
backward.stop()
left.stop()
right.stop()


while GPIO.input(buttonPin) == False:	# while is a loop until a condition becomes not True; 'until pressed' in this case
	sleep(0.010)
#eof while not pressed


forward.start(100)			# full speed ahead
sleep(3)
right.start(100)			# hard turn right
forward.stop()				# breaks!
backward.start(100)			# full reverse
sleep(1)
right.stop()				# release steering
backward.stop()				# stop the car


# we purposely do not close the GPIO, as that breaks communication with the BlueTooth app
# GPIO.cleanup()

Button control - one press to start, second press to reverse, third press to stop

#Define PWM functions
forward = GPIO.PWM(forwardPin, frequency)
backward = GPIO.PWM(backwardPin, frequency)
left = GPIO.PWM(leftPin, frequency)
right = GPIO.PWM(rightPin, frequency)
# initialize in stopped position. Helps to avoid problems
forward.stop()
backward.stop()
left.stop()
right.stop()


while GPIO.input(buttonPin) == False:	# while is a loop until a condition becomes not True; 'until pressed' in this case
	sleep(0.010)
#eof while not pressed
while GPIO.input(buttonPin) == True:	# 'until released' in this case
	sleep(0.010)
#eof while pressed


forward.start(100)			# full speed ahead


while GPIO.input(buttonPin) == False:	# while is a loop until a condition becomes not True; 'until pressed' in this case
	sleep(0.010)
#eof while not pressed
while GPIO.input(buttonPin) == True:	# 'until released' in this case
	sleep(0.010)
#eof while pressed


forward.stop()				# breaks!
backward.start(100)			# full reverse


while GPIO.input(buttonPin) == False:	# while is a loop until a condition becomes not True; 'until pressed' in this case
	sleep(0.010)
#eof while not pressed
while GPIO.input(buttonPin) == True:	# 'until released' in this case
	sleep(0.010)
#eof while pressed


forward.stop()				# breaks!
backward.stop()				# stop the car


# we purposely do not close the GPIO, as that breaks communication with the BlueTooth app
# GPIO.cleanup()

Car moves in a square shape

#Define PWM functions
forward = GPIO.PWM(forwardPin, frequency)
backward = GPIO.PWM(backwardPin, frequency)
left = GPIO.PWM(leftPin, frequency)
right = GPIO.PWM(rightPin, frequency)
# initialize in stopped position. Helps to avoid problems
forward.stop()
backward.stop()
left.stop()
right.stop()

for i in range(4):
	left.start(100)
	forward.start(100)			# steer a while
	sleep(0.5)

	left.stop()                 # no steering, go straight
	sleep(1)
#eof for 4 times

forward.stop()				# fully stop the car

# we purposely do not close the GPIO, as that breaks communication with the BlueTooth app
# GPIO.cleanup()

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Perfect STEM course. Module 1 - Smart Car with Raspberry PI
  • 118
  • 42:47
  • 136