Programming Virtual Robot to do 2 tasks at the same time. Please help

Hi,

Could someone please help to debug my code. I am new in Python programming and I wanted to make my robot scan the ‘barcode’ and at the same time count the ‘inventory boxes’ on the left. I tried doing it in Python Project using a function but my variable ‘inventory’ doesn’t increase the value whenever the distance sensor reads < 30cm. In the end, the display always shows ‘None’. Here is my Python Project link: https://www.robotmesh.com/studio/60a7162ad2cf776482d02749

I tried an alternative way in a Blockly Project to learn using a thread. However, the problem still persist. My variable on thread2 doesn’t count up. Here is my Blockly Project link: https://www.robotmesh.com/studio/60aafe5dd07e4038a62aaf82

I have no problem in scanning my ‘barcode line segments’, my only problem is to count the boxes.

Looking forward to your suggestions.

Thank you.

Marco

Hi Marco.

When you define detectLines() you have this code:

def detectLines():
    global inventory    
    inventory = 0

Later, you have this code:

# call detectLines function for 4 times. Store the letter to scannedLetters list for every iteration.
for i in range(5):
    scannedLetter[i] = detectLines()
vexiq.lcd_write('Inventory = ' + str(inventory), 3)

You zero out inventory every time detectLines() is called. As a result, it’s 0 when you try to print it. If you want to hold on to the contents of that variable to use later, don’t set it to 0.

Cheers, Sam.

Hi Sam,

Thank you for your advice. However, after deleting the code “inventory = 0” in my detectLines function I encountered this error:

Please help.

Thank you.

Marco

It is throwing a name error because you haven’t defined inventory yet. Move inventory = 0 outside of the function.