The simplest sort of program can make a light blink, but if we want anything more complex, we need to start using programming logic. Programming logic is used in every program, and MakeCode is a great introduction to how these work.

Transcript

Hi, welcome back to the Circuit Playground Express online workshop. In this section, we're going to learn about programming logic so let's look at it and MakeCode and see what I'm talking about.

If we go under logic in MakeCode, we see the first type of logic is a conditional and the most used one is an "if-else" statement. So, an "if-else" statement runs a piece of code whenever a comparison is found to be true and if it's not true then it throws it into the else portion and it runs that part of code and then after it's done it moves on through the loop. So, a good example of this would be if button "A" is pressed, then we'll turn on some lights and if it's not pressed then we'll turn them off. So, if we look at the virtual board, we click "A", while it's pressed the lights run and when we let off after our half second, they turn off again. So, whenever button "A" is pressed, that's going to return a true and anything that's in this diamond shape will return either a true or false value.

So that brings us to our next portion of programming logic, which is a comparison. So, if we don't have something that returns a true or false value we'd use a comparison, so comparisons are one value equal to another value or is one value greater than another value and we can change that by changing it in the middle, but we'll stick with equal to net for now. So, say we want the light level to equal zero, so the comparison will return true whenever the light levels set to zero and when it returns true, then the if statement will run that first portion that's in its brackets and when it's not equal to zero, it will clear.

So, say you have more than one condition that you want to run your "if" statement, you could do with multiple if statements inside each other or you can use a Boolean comparison. So, if we if we use Boolean logic, we will go into logic again, down here is Boolean and that's our ANDS or ORS or NOTS. So that's intuitive I would think, that if we put our AND in our "if" statement and we want our light level to be equal to 0 and we want button "A" to be pressed, then if we want this first portion of code to play, we need to have our light level greater than 0 and we need to press the button, wait, light level equal to 0 and press our button. So other options for that is, OR. So, either the light levels set to 0 or the button "A" pressed will make the rainbow play in this code and then when it's neither of those are true it'll clear and it's worth mentioning as well, there's a NOT. So, if you have some code and for some reason, you want to say the if the light level is not equal to 0. Then you could do that by using Boolean logic, you could also change this comparator to does not equal 0 but there are situations where you need the NOT and another way you can use it is you can create your, NOT AND, NOT OR, which is in a normal programming language is usually NAND or NOR, by putting a AND inside a NOT. So that would that would return true if say, if neither light level or button is pressed if you have NOT AND, then it would return true. So that kind of just reverses them.

Another type of logic that we have is a while loop and a while loop works a lot like an if statement, except an if statement if it's condition is true it plays whatever is in its brackets once and then can continues through the program. A while loop plays whatever's inside its brackets continuously, while its condition is true. So, it will not move on with the code while the conditions true it will wait for that condition to no longer be true for it to stop.

Now the last type of logic I want to talk to you about today is a for the loop. So, a for is a conditional loop that runs for a set number of times through whatever is in the brackets and then it continues with the code and this is most useful for condensing repetitive code. So, when you use a for loop, it creates a variable called the index and the index is how many times the loop is going to run. So, the index is initially set to 0 and then we're going to run it nine times or up to nine. So, 0 being the first value this for loop will run through ten times and then continue with the code. So, in a good example of a way to use this code is to create an animation, where a light runs around the ring. So, if we take the variable index, which has been created for the for loop, we can set the pixel of the index to red, we'll put in a short pause and then we'll set the pixel back to off. So, if we look at the virtual interface we have, an animation of a light running around the ring and then all the other lights are off. Now we do this because each time each time the for loop runs through, it takes the variable index and increases it by one in the count and when it gets to nine it leaves the for loop and goes back to the rest of the program which is just a forever loop. Now that brings us back to the for loop again. So, we can use that index variable and so the first time that the for loop runs through index will be zero, so we'll set pixel zero to red and then turn it off and then the next time the for loop goes through, index will be one so that will set pixel one to red and then turn it off again. So, this is useful for accessing an array or for like I said making a repetitive task, you could do this without a for loop, but you'd need to have the set pixel colour on and off ten various times and then have manually type in the number of the pixel each time. So, this is a much easier way to achieve that sort of goal.

So that wraps up this section of the Circuit Playground Express online workshop. Stick around for the next section, we're going to learn about all the blocks in MakeCode and what they do.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.