TouchLED sensor

I added a touchLED to my code and I want the code to run when I press the touchLED. I’m not sure where I went wrong in this code, but when I start the code, it just begins the code before I even touch the LED. Could anyone take a look at my code and tell me if anything stands out? Thank you!

Read your code out loud to yourself. Do you see what I see?

If you don’t, it’s the while loop condition. Your code says while not pressing. This will run the code … Wait for it … While you aren’t pressing the touch led. Maybe try a “wait until” block instead of a while loop. Try: wait until touchled pressed, then start the code.

1 Like

So this code reads when not touchled_9,pressing():

The code runs when the touch led is not in the process of pressing.

You need to start the code and then let it run. So what I recommend is that you create a memory variable called robotRun.

> robotRun = False
> while True:
>     if touchled9.pressing() 
>         if not robotRun:
>             robotRun = True
>         else:
>             robotRun = False
>     if robotRun:
>             your run code
>            robotRun = False #This resets your code so that you can press again and it will run