How to create nutrition app with ocr scanner?

These are all of my blocks:

You should also have some code to display the status of the GET from the API.

1 Like

I tried a live test on my phone, it doesn’t actually tell me what the error is, the screen just turns white and it says “there is an error with your blocks”.

1 Like

Thanks! But I’m actually not sure how to add the status of the GET from the API.

Say the carb value is 15… That is neither smaller than 12.5 nor bigger than 19. You should change 12.5 to 18.9.

EDIT: You can also have another statement to se the text of a label if the carbs value is bigger than 12.5 and smaller than 19. You can use an and block for that.

That is easy…

All this debugging is important.

1 Like

Okay thank you! Is this correct?

One problem you’re going to have is that you’re not setting the value of the variable “carbs” to the actual property “carbohydrates_100g”. You’re setting a label to that value… but you need to store it in a variable instead. If you set the variable “carbs” to the value the property returns (with the “get property of object” block), you can then use that variable elsewhere in your code, as you’ve done.

If you want to display the number of carbs, first set the variable to that value and then set GetButtonText’s Text to “app variable carbs”.

2 Likes

I changed the variable name, but it still gives me an error with the blocks, but it does not tell me what the error is.

Watch this:

https://youtu.be/X91egqguwn8

1 Like

Okay! So is this how I set it to the value?
image

Even when I tried this it shows an error with the blocks.

I guess that video covered initializing variables but not setting their values.

By the way, I’m just Googling variables thunkable. You can do the same. There are lots of tutorial videos. Also, the Thunkable documentation is usually helpful: Variables - ✕ Docs.

This is probably a better video for you:

1 Like

I’m not really understanding what setting the value is, because the video didn’t seem to be mentioning that, and I could not find it in the docs either.

From the documentation:

Also, at the 5:59 mark in the video, @darren shows how to set a variable value.

The point is, if you want to use a variable in your code, you need to first set it’s value. And more importantly for your project, when you get the property of a JSON object, you need to set that value to be attached to a variable using the “set” block.

So you need a “set app variable carbs to” block to the immediate left of the “get property of object [carbohydrates_100g]” block.

Okay. So after adding that do I remove the “set variable” block above it?

Yes… but I don’t understand why you’re using different variables below that. When you set the variable “CeleBytes” (whatever that is), your next step is to check the value of the variabe “carbohydrates_100g”. Why? Why not just use the same variable for both? In other words, set “carbohydrates_100g” to “get property of object…”

Otherwise, it’s like you’re doing math this way:

set x to 35
if y < 18.9
…

You want your code to work like this:

set x to 35
if x < 18.9

The “x” and “y” are the variable names. You need consistency.

1 Like

Oh, I’m really sorry. When I took the first block it was set to the name “celebytes” and I forgot to change it “carbohydrates_100g”.

1 Like

These are all my blocks. Is there still an error because when I tried to scan a barcode it told me there was an eror with the blocks again.

Is there something wrong with the way I have parsed?

The way to troubleshoot your code is to use labels to display intermediate values. So each time you set a variable, you should check its value. You can use Wait blocks to pause your app and check each value. That should help pinpoint where the mistake is.

1 Like