Intro to Arduino with TinkerCAD

To begin your journey coding, there are a few basic commands you need to master: Loops, Logic statements, conditions, inputs, math, variables, and counts.

Loops:

In coding when you ask a computer perform a command more often than not it will perform it and then stop. This is because the command has been completed and the computer reads this a finished. Code commands will typically work down from the top down like reading a book and will do each command once unless it says otherwise. The way commands are coded to repeat are called loops. Below you can see a few different types of loops. Depending on the condition and what you need to control, these different loops perform differently. A "on start" loop will loop a command and repeat that command forever until sopping the code. An "on start" loop will typically be placed at the beginning of a code and the rest of the code will be nested in it. A "forever" loop can be placed anywhere in a code and like its name states will repeat forever unless commanded otherwise or the program is turned off. A "repeat" loop is set to repeat a number of times. If you have something that is finite, you would use this loop function. There are also loops that can be set to repeat indefinitely, like the "on start" and "forever" commands but require a condition to be met. In this loop, you everything in the loop repeats only while the condition is true. Typically, you want all of your code for the rest of the project nested inside at least one of these loops so that it repeats for as long as you need it to. 

Logic: 

Logic in coding works a lot like the logic in our logic gates. Logic asks for conditions to be met, and if they are then you can have a specific determined output. In the example below, the logic reads like “If digital pin 1 reads high, then set pin 2 to high.” High is 100% output power of the Arduino which is 5V. 

There are many types of logic statements but the basic three are IF, IF ELSE, and IF ELSEIF ELSE. TinkerCAD only has two. The other example is an IF, ELSE. This logic is asking if the input condition is met, then output a command, otherwise output a different command. 


Math: 

You can see from the examples above that many of them include math. Math is a great way to define highs and lows, and other ranges of inputs and outputs. Looking to the left, you can see math will have regular functions like addition or multiplication, but also will have ranges, constraints, and other less-used mathematical functions. Math in TinkerCAD shows up green. These operators are essential for calculations and manipulating variables.

Variables:

Variables are containers for storing data in your program. They can hold various types of information, like integers, floating-point numbers, or characters. Properly declaring and using variables allows you to store and manipulate data effectively, making your code more organized and readable. Variables allow you to rename inputs, outputs, or conditions to be in plain English. This way you can set a condition to a variable and then reuse that variable throughout the code without having to reset the conditions over and over. To the right, you can see I created a variable called “brightness” which gives you two commands immediately for that variable. Also is an example of the variable being implemented to a condition. 

Counts:

Counts are the final key command you will need to start your coding journey. Counts do exactly what they sound like- they count up or down. You can set ranges for counts and how much each unit will count for.  Below is an example of a count. This count is increasing the brightness of an LED output so it slowly fades on from 0% to 100%. You can see the other variables and conditions needed in this statement to make it work. In Arduino, PWM is a range from 0 to 255 because of the frequency of its square wave output. 0 is off and 255 is 100% on. Any number in between is a percentage ratio between off and on. The wait command is embedded in the count loop so that the brightness increase doesn’t happen all at once.

PWM:

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between the full Vcc of the board 5V on the Uno board and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and Vcc controlling the brightness of the LED. In the graphic to the right, the red lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the red lines would measure 2 milliseconds each max. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

In TinkerCAD you can adjust the Pulse Width Modulation output by setting a pin to a number between 0 and 255. When you set a pin to 255, the duty cycle output will be 100%. When you set a pin to 127.5, the duty cycle will be 50%. You can continue this trend for any percentage between 0% and 100% or 0 to 255 PWM. The device that is connected to the output pin will effectively be controlled in proportion to the percentage of PWM that is being output. Because PWM is a series of on and off cycles, the device will be controlled in proportion of the percentage of PWM without actually losing any power since voltage and current are staying the same. PWM can control the brightness of LEDs, the speed and direction of motors and servos, lights, and more!