[{"data":1,"prerenderedAt":383},["ShallowReactive",2],{"lesson-1-02-intro-code":3},{"id":4,"title":5,"body":6,"chapterName":371,"chapterNumber":46,"description":372,"extension":373,"includesCode":374,"lessonNumber":306,"meta":375,"navigation":321,"path":376,"seo":377,"starterCode":378,"stem":379,"tags":380,"__hash__":382},"lessons/lessons/1-02-intro-code.md","Introduction to C++ Code",{"type":7,"value":8,"toc":363},"minimark",[9,14,18,22,25,29,32,49,86,92,101,110,113,117,128,137,147,150,159,170,173,176,270,273,277,294,340,349,353,356,359],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17],"p",{},"In this lesson, we'll cover the basics of programming in C++ and how to control your circuits with code. You'll learn\nabout\nvariables, functions, and how to write simple programs to interact with your circuits.",[10,19,21],{"id":20},"c-basics","C++ Basics",[15,23,24],{},"C++ is a powerful programming language that is widely used in embedded systems and microcontroller programming. It\nallows you to write efficient code that can run on small devices with limited resources. Here, you can define\nvariables to store data, call functions to interact with your hardware, and use control structures like loops and\nconditionals to control the flow of your program.",[10,26,28],{"id":27},"variables","Variables",[15,30,31],{},"Variables are used to store data in your program. You can define a variable by specifying its type and name, and\noptionally initializing it with a value. For example:",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-cpp shiki shiki-themes github-light github-dark","int specialNumber = 4;\n","cpp","",[40,41,42],"code",{"__ignoreMap":38},[43,44,47],"span",{"class":45,"line":46},"line",1,[43,48,36],{},[15,50,51,52,55,56,59,60,63,64,67,68,71,72,75,76,78,79,81,82,85],{},"This line of code had a few key sections to it. First, we defined the type. In this case, ",[40,53,54],{},"int"," stands for integer,\nwhich is a whole number. Other types include ",[40,57,58],{},"float"," for decimal numbers, ",[40,61,62],{},"bool"," for true/false values, and ",[40,65,66],{},"char"," for\nsingle characters.\nNext, we gave our variable a name, ",[40,69,70],{},"specialNumber",", which is how we will refer to it in our\ncode. Then, we used the assignment operator ",[40,73,74],{},"="," to assign it a value of 4. This means that ",[40,77,70],{}," now holds the\nvalue 4, and we can use it in our program wherever we need to refer to that value.\nWe then gave it a value of 4. This means that ",[40,80,70],{}," now holds the value 4, and we can use it in our program\nwherever we\nneed to refer to that value. Finally, we ended the line with a semicolon ",[40,83,84],{},";",", which is used to indicate the end of a\nstatement in C++.",[87,88],"img",{"alt":89,"src":90,"width":91},"Guide showing the parts of initializing a variable","/images/lessons/02-initialize-variable.svg","50%",[15,93,94,95,97,98,100],{},"In this example, we define an integer variable called ",[40,96,70],{}," and initialize it with the value 4. Later, if we\nwanted to change the value of ",[40,99,70],{},", we could simply assign it a new value like this:",[33,102,104],{"className":35,"code":103,"language":37,"meta":38,"style":38},"specialNumber = 10;\n",[40,105,106],{"__ignoreMap":38},[43,107,108],{"class":45,"line":46},[43,109,103],{},[15,111,112],{},"You can use variables to store values that you want to use later in your program, such as sensor readings\nor the state of a component.",[10,114,116],{"id":115},"calling-functions","Calling Functions",[15,118,119,120,123,124,127],{},"Functions are blocks of code that perform a specific task. You can call a function by using its name followed by\nparentheses ",[40,121,122],{},"()",". For example, if you want to set a digital pin to ",[40,125,126],{},"HIGH",", you might call a function like this:",[33,129,131],{"className":35,"code":130,"language":37,"meta":38,"style":38},"digitalWrite(3, HIGH);\n",[40,132,133],{"__ignoreMap":38},[43,134,135],{"class":45,"line":46},[43,136,130],{},[15,138,139,140,143,144,146],{},"In this example, ",[40,141,142],{},"digitalWrite"," is the name of the function, and we pass it two arguments: the pin number (3) and the\nvalue\n(",[40,145,126],{},"). This function would set pin 3 to a high voltage level, which could turn on an LED or activate another\ncomponent\nconnected to that pin.",[15,148,149],{},"When calling a function, it may also return a value that you can use in your program. For example, if you want to read\nthe value from an analog pin, you might call a function like this:",[33,151,153],{"className":35,"code":152,"language":37,"meta":38,"style":38},"int sensorValue = analogRead(2);\n",[40,154,155],{"__ignoreMap":38},[43,156,157],{"class":45,"line":46},[43,158,152],{},[15,160,161,162,165,166,169],{},"Here, ",[40,163,164],{},"analogRead"," is the function that reads the value from pin 2 and returns it as an integer. We store this returned\nvalue\nin a variable called ",[40,167,168],{},"sensorValue"," for later use in our program. We can use this returned result in place of a literal\nnumber.",[15,171,172],{},"Functions can take different numbers of arguments and can return values as well. You can use functions to interact\nwith your hardware, perform calculations, or organize your code into reusable blocks.",[15,174,175],{},"Here are a few more examples of function calls you might see in your code:",[177,178,179,202],"table",{},[180,181,182],"thead",{},[183,184,185,192,197],"tr",{},[186,187,188],"th",{},[189,190,191],"strong",{},"Function",[186,193,194],{},[189,195,196],{},"Description",[186,198,199],{},[189,200,201],{},"Example",[203,204,205,216,227,238,248,259],"tbody",{},[183,206,207,210,213],{},[208,209,142],"td",{},[208,211,212],{},"Instructs a pin to output a digital signal until changed.",[208,214,215],{},"digitalWrite(1, HIGH);",[183,217,218,221,224],{},[208,219,220],{},"digitalRead",[208,222,223],{},"Senses and returns HIGH or LOW from a specified pin.",[208,225,226],{},"bool isOn = digitalRead(1);",[183,228,229,232,235],{},[208,230,231],{},"analogWrite",[208,233,234],{},"Intstructs a pin to output an analog signal until changed. Accepts from 0-256.",[208,236,237],{},"analogWrite(1, 212);",[183,239,240,242,245],{},[208,241,164],{},[208,243,244],{},"Senses and returns from 0-1024 the signal strength from a specified pin.",[208,246,247],{},"int signal = analogRead(1);",[183,249,250,253,256],{},[208,251,252],{},"pinMode",[208,254,255],{},"Sets a pin to either be an ouptut or an input from here on out.",[208,257,258],{},"pinMode(1, OUTPUT);",[183,260,261,264,267],{},[208,262,263],{},"delay",[208,265,266],{},"Waits without doing anything for a specified length of time, in milliseconds. (1000 milliseconds = 1 second)",[208,268,269],{},"delay(1000);",[15,271,272],{},"There are many more functions available for you to use in your code, and we will go over some of these as they become\nrelevant in the upcoming lessons.",[10,274,276],{"id":275},"arduino-code-layout","Arduino Code Layout",[15,278,279,280,283,284,287,288,290,291,293],{},"When programming for microcontrollers, we typically have two main functions: ",[40,281,282],{},"setup()"," and ",[40,285,286],{},"loop()",". The ",[40,289,282],{},"\nfunction is called once when the program starts and is used to initialize your hardware and set up any\nnecessary configurations. The ",[40,292,286],{}," function is called repeatedly in a continuous loop and is where you will write\nthe\nmain logic of your program to interact with your circuits.",[33,295,297],{"className":35,"code":296,"language":37,"meta":38,"style":38},"void setup() {\n    // This code runs once when the program starts\n}\n\nvoid loop() {\n    // This code runs repeatedly in a loop\n}\n",[40,298,299,304,310,316,323,329,335],{"__ignoreMap":38},[43,300,301],{"class":45,"line":46},[43,302,303],{},"void setup() {\n",[43,305,307],{"class":45,"line":306},2,[43,308,309],{},"    // This code runs once when the program starts\n",[43,311,313],{"class":45,"line":312},3,[43,314,315],{},"}\n",[43,317,319],{"class":45,"line":318},4,[43,320,322],{"emptyLinePlaceholder":321},true,"\n",[43,324,326],{"class":45,"line":325},5,[43,327,328],{},"void loop() {\n",[43,330,332],{"class":45,"line":331},6,[43,333,334],{},"    // This code runs repeatedly in a loop\n",[43,336,338],{"class":45,"line":337},7,[43,339,315],{},[15,341,342,343,345,346,348],{},"In the ",[40,344,282],{}," function, you might set pin modes, precalculate some numbers, or perform any setup tasks\nrequired for your project. In the ",[40,347,286],{}," function, you would write the code that controls\nyour circuits, such as reading sensor values, controlling LEDs, or responding to user input.",[10,350,352],{"id":351},"next-steps","Next Steps",[15,354,355],{},"In the next lessons, we'll be building projects that use the components in your kit and writing code to control them.\nYou'll learn how to read sensor values, control LEDs, and create interactive projects with your circuits. Remember to\nexperiment with the code and have fun exploring the possibilities of what you can create!",[15,357,358],{},"If this is your first time programming, don't worry if it feels a bit overwhelming at first. Programming is a skill that\ntakes\ntime to develop, and the best way to learn is by doing. Start with simple projects and gradually build up your skills as\nyou go. Don't be afraid to make mistakes and learn from them, and remember that there are many resources available\nonline to help you along the way. Happy coding!",[360,361,362],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":38,"searchDepth":306,"depth":306,"links":364},[365,366,367,368,369,370],{"id":12,"depth":306,"text":13},{"id":20,"depth":306,"text":21},{"id":27,"depth":306,"text":28},{"id":115,"depth":306,"text":116},{"id":275,"depth":306,"text":276},{"id":351,"depth":306,"text":352},"Getting Started","Learn the basics of programming in C++ and how to control your circuits with code.","md",false,{},"/lessons/1-02-intro-code",{"title":5,"description":372},null,"lessons/1-02-intro-code",[381,40],"beginner","_jD_UFJ2GGZGHQ267lTgrvLbNUzfuFk0B8ChjIbuNeE",1775950750610]