Skip to content

Commit

Permalink
bruker threading for å kjøre startStop funksjonen og runRobot funksjo…
Browse files Browse the repository at this point in the history
…nen samtidig
  • Loading branch information
Odin Grav committed Sep 5, 2025
1 parent f0c56b3 commit b6167fa
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions oving_2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile

import threading
import sys


# This program requires LEGO EV3 MicroPython v2.0 or higher.
# Click "Open user guide" on the EV3 extension tab for more information.
Expand All @@ -30,31 +33,41 @@

isPressed = False

next_turn = 0

while True:
if(startBtn.pressed() and isPressed == False):
ev3.speaker.say("Excercise 2")
wait(500)
isPressed = True
next_turn = 1
def startStrop():
global isPressed
while True:
if(startBtn.pressed()):
if isPressed == False:
ev3.speaker.say("Excercise 2")
wait(500)
isPressed = True
next_turn = 1
else:
isPressed = False
robotDriveBase.stop()
ev3.speaker.say("Excercise done")

break

elif(isPressed == True):
if startBtn.pressed():
isPressed = False
break

elif(usSensor.distance() >= 75):
robotDriveBase.drive(-250, 0)
else:
robotDriveBase.turn(90 * next_turn)
robotDriveBase.straight(-150)
robotDriveBase.turn(90 * next_turn)
next_turn *= -1

robotDriveBase.stop()
ev3.speaker.say("Excercise done")
print("The end..")
sys.exit()


def runRobot() :
next_turn = 0

while True:
if(isPressed == True):
if(usSensor.distance() >= 75):
robotDriveBase.drive(-250, 0)
else:
robotDriveBase.turn(90 * next_turn)
robotDriveBase.straight(-150)
robotDriveBase.turn(90 * next_turn)
next_turn *= -1

t1 = threading.Thread(target=startStrop)
t2 = threading.Thread(target=runRobot)

print("The end..")
t1.start()
t2.start()

0 comments on commit b6167fa

Please sign in to comment.