Let's remember the event.

'Do something when the button is pressed' is like a reflex.
Without thinking about anything in particular, "Now! and the program runs.

Let's give it a little more of a try. Use "memory" in your program to do things like "the button was pressed, this is the third time.

If you can do this, you will be able to react to things like, "Oh, the robot only hits to the right, let's make it move more to the left," or "I saw this person's face on the camera a while ago.

Variable

The things that can be remembered in a program are called "variables".
The block program of obniz is based on a language called JavaScript, so you can use the variables of JavaScript.
You can store anything in that variable. Numbers, letters, pictures, and even programs can be memorized, but block programs use numbers and letters primarily.

You can let them memorize it whenever they want and ask, "What was that?" whenever they want.
Let's put it to use right away.

Variable block

Let's take a quick look at the variables.
I'll try to remember the number of times the UI button is pressed and show the number of times it is displayed on the screen.

Variables can be found in the "Variables" section on the left, under "Create variable… button to create one.

Press the button and you will be asked to enter your name.

Pressing OK brings up a block to manipulate the variable you created inside the variable block.

Learning

Let them actually learn.

The "set~to" at the top is the block to remember the variable and the round block with only the name of the variable at the bottom is the one to retrieve the value of the variable.

The idea of variables

Variables are like boxes that you "put" numbers, letters, etc. into and make them memorize. So, when you make him remember, you can say, "Assign to a variable".

Let's program it.

Now, let's actually make them learn.
The block to assign to the variable count is placed before it enters the loop.

And let's set the value to the number 0. There is a number block at the top of the "Calculate" block, so move it to the right side of the block we just placed, to the right of the assignment.

The number 0 is now in the variable called count.
If you take out the item in count after this, it will always be 0.

Let's use count in the actual display of obniz.
Let's grab the round count block in the variable block and put it at the place where "Show Hello World in obniz" is shown.

Now you can see what's in the count variable in the obniz display.
If it is now, it is 0.

This is how the program will be completed.

If you do this and push the button called "button" that comes up, you will see the number 0 on the display of obniz.

Rewrite a value

If you only put a value in a variable once, then there is no point in the variable.
Now let's count the number of button presses using a variable.

There are two ways to rewrite the values, one is to use the dedicated blocks that allow you to increase or decrease the number in them, and the other is to use them.
This is a number in a variable and can only be used if you want to increase or decrease it.

The other way is to put in a new value.
If you want to increase the value by 1, you can add 1 to the previously entered value and put it back into the variable as a new value.

This time, we'll just count the number of button presses, so we'll use blocks to increase the number.

In the block that is executed when the button is pressed, there is a block called "Increase or decrease the variable count 1", so I put this. Then, it looks like this.

Let's try to do this.
Pressing the button in the same way will show 1 in the OBNIZ display.
Since the number in the variable count is increased by 1 before it is displayed in the obniz display, the
The count is 0+1 with a 1 in it. Then the obniz display will show the value in the count, which is 1

If you press the button again, you will see 1+1 this time with 2 in the count and 2 in the obniz. You have a good memory!

With this, you can create an advanced program to easily count something or find out if something has or hasn't happened.

Development

If you can afford it, why don't you try something like this?

  1. When the button is pressed three times, the letters appear on the obniz display.
  2. Use the assignment and addition blocks to increase the numbers in the variable, rather than the increase or decrease blocks in the count.

関連の質問