Skip to main content

Traffic Light with Mblock - 2

 In our previous application, I explained how to make a Traffic Light Application inArduino with Mblock. In this application, we will find the number of vehicles passing the red light by making additions to the mblock with the traffic light application

First of all, we find a traffic light on the internet. At first, all the lamps are dark, by painting the red light, by painting the red and yellow light, by painting the yellow light, and finally, by painting the green and green light, the sprite has 5 disguises in total. When each LED is on, which LED will light up, it will be disguised.


After adding our traffic light, we add a road image to the background decor. In addition, we find a car picture on the Internet for the car that will pass on the road and add it. I drew a line on the road and painted it red in order to detect the people passing through the red light.

 

Now it's time to move the car. Below are the codes that we will write inside the car1 sprite.


Clicking the Green Flag hides the car, and we create a variable from the Variables menu to keep cars running through the red light. Let it be zero at the beginning. Because if the game has been played and closed before, the numbers in that game will not appear. Since our car will be constantly moving, we add the repeat code from the Control menu. Our codes will be for that too. In order for the car not to appear at the same time, we add the "Hold a random number" code from the Operators menu into the 1 second wait code we get from the Control menu. And I'm making 10 out of 4, you can give the numbers you want. Thus, the car will go and appear at the given coordinates at a random time between 1-4 seconds. After it appears, it will move to the coordinate we will determine in the second we will specify, with the float code in the motion menu. Our car will move towards the top and when it comes to the edge it will hide. We check if it is worth it with the if code from the control menu. So our car will constantly appear and move. I added one more car to the application. The codes are the same, only the random seconds and coordinates are different. You can plant as many cars as you want by making a copy.

 

Now it's time to check if the car touches the stick we drew.



Since the red bar was above the cars, it was as if the cars were passing under the bar, so I threw the bar under the cars with the code 1 layer down from the view. We check whether the cars are worth it with the repeat code from the control menu, with the code if. If it's worth it, we add the code to release the news from the Events menu and give it a new name. I added the red bar from I needed 2 data to understand if the cars were crossing the red light. The first one is the red light and the other is the car at that time, I put a stick in it. You guys can find out if the car goes through red or not.

 

Let's change the disguise of traffic lights. We write the following codes into the traffic light puppet we added.



We learned how to turn on the LEDs for the traffic light in the previous application. Click for traffic light

With the code we added from the looks menu, let the traffic light switch to the dark version so that it looks dim at the beginning. Since our lamp will be constantly on, we add the continuous code from the Control menu. When the red led turns on, it will change into red light and wait 10 seconds, then red and yellow will light together, turn into yellow and wait for 2 seconds, then the green light will turn on and turn into green light and wait for 5 seconds, then yellow and green will turn on together and turn to green and yellow 2 Wait a second and go back to the beginning, that is, to the red light. We turned on our traffic lights. Here's how we check if the vehicle goes through a red light. Vehicles released a message every time they touched the red bar. We check with an if code when this news arrives. If our costume number is 2, the costume number 2 is the number of the red light (we get the equal operation from the operators menu, we write the costume number code on the left side, and the cover number on the right side with a number). In other words, if the car has touched the red bar and the light is red, we increase the number of cars passing the red light by 1. The reason we wrote this inside the traffic light puppet was that we couldn't get the costume number of another puppet from inside another sprite. Thus, we can keep track of the number of vehicles passing the red light.

 

In order to run these codes, after connecting our arduino board and circuit to the computer with a usb cable, first we select the Serial port from the Connect menu, let's choose the port to which our arduino board is connected from there. By pressing the Upload to Arduino button, we send the codes we wrote to our Arduino board and upload them. Thus, our program becomes ready to run.

To download the source code of the program CLICK HERE


Similar Topics

For All Scratch Examples CLICK HERE

For Mblock With Arduino Examples CLICK HERE

Comments

Popular posts from this blog

Pacman Game on Scratch Program

 We will make an example of pacman game making with Scratch. Before starting the Pacman Game example that we will do with Scratch, we download the background for the pacman game from the internet and add it to the backdrop part in Scratch. When the background I downloaded was dark, I painted the dark colors with white. I made the borders thicker. After adding our background to our work, it came to creating the pacman character. I drew a full circle in yellow for the Pacman character. After that, I drew two lines towards the center using the line tool. I erased the remaining part with an eraser. If you want, you can use the erase background tool to erase between two black lines. Thus, our pacman character took his place with his mouth open. I created a guide dot right in front of the pacman character. This will keep us moving. I created a red dot. It's a little obvious, you can make this red dot smaller if you want, you can give it another color and make it more invisible. After cre...

How To Make Bubble Shooter Game on Scratch Program

 We will make a bubble shooter game example with Scratch. I am adding 3 balloons and a target picture for the bubble shooter game we will make with Scratch. (You can increase the number of balloons). Now I come to the codes to be written inside balloon1. Let the number of balloons popped when the green flag is clicked is 0. Thus, the remaining numbers from the previous game are reset. Let the balloons be hidden at first. Then, it will appear in a random place between the coordinates we have determined at a random time between 1-5 seconds, wait for 2 seconds, then hide. X: The coordinates between -218/144 are the coordinates of the far left and the right of the screen, Y:134/-235 are the coordinates of the top and bottom of the screen. It will appear randomly in these ranges If our balloon is aimed and hit by the target; Balloon shot news is released. When this news reaches our balloon, the balloon will be hidden. Copy and paste the same code above.   Then we say duplicate...

Apple Picking Game with Potentiometer- Mblock with Arduino

 Before moving on to this application, we had done the LEDLighting with the Potentiometer. You can learn the installation of the potentiometer and the necessary materials here.   Let's write our codes in the Mblock program. First of all, find a background from the internet, we change the background from the scene section. I add 3 apples (you can increase the number of apples) and 1 basket from the new puppet part.   Here are the codes in Apples: Since the green flag is clicked, we add the code to get our variable number of apples 0, which we created from the data section. If we do not add this, the number of apples collected will continue from where it left off if the game was played before. We take the code that generates random numbers from the operations menu and add the code that generates random numbers into the code to go to the x and y point that we added from the movement menu so that the apples come out randomly in a horizontal place. Here, we write the start...