3.1 Introduction to Arduino - programming APSC 160 – part 2 2 Arduino Programming +Arduino platforms are designed to use the C++ programming language +C++ is a superset of C + So we can use C to program Arduinos + All we have learned so far: control statements, loops, functions, arrays, etc. +In addition, we will learn about: + The structure that Arduino programs use (has to do with Arduino being an embedded systems platform) + Some Arduino functions 3 Arduino Programming +In this slide set, we will learn about: • setup() • loop() Used for initialization and flow control of the program • pinMode() Used to specify the direction of a pin (input, output) • digitalRead() • digitalWrite() Used IO (or I/O): reading from a digital input and writing into a digital output device 4 Arduino program structure +An Arduino program does not have a main() function. This may seem odd as we learned in part 1 that every C program must have a main() function. +main() still exists but it’s hidden +Two functions are used instead in every Arduino program: +setup() +Loop() +Sometimes an Arduino program is called a sketch. 5 Typical Arduino program framework +The starting skeleton (structure) of an Arduino program is: // variable declarations, compiler directives void setup() { // code here } void loop() { // code here } 6 setup() +The setup() function is called exactly once when the program starts. void setup() { // code here +We include here anything that we want executed once when the } program starts, normally to set things up. + Takes no parameter, returns nothing +setup() reference page: + https://www.arduino.cc/reference/en/language/structure/sketch/setup/ 7 loop() +After the setup() function is executed – exactly once – the loop() function is called (executed) repeatedly. The code in the loop() function will loop consecutively: void loop() { // code here } +The loop() function creates a forever (infinite) loop mechanism (also called superloop) +Takes no parameter, returns nothing +loop() reference page: +https://www.arduino.cc/reference/en/language/structure/sketch/loop/ 8 Superloop +Arduino (embedded) programs always include some tasks that we want to be performed repeatedly. +A repetitive structure, usually called a superloop, includes all tasks that need to be executed repeatedly. +This approach is very common in embedded programming (including Arduinos) Arduino implementation void loop() { General purpose C code /* within some function while(true) { /* your code to implement repetitive tasks here */ /* your code to implement repetitive tasks here */ } } 9 Summary +Arduino programs are C (or C++) programs with a specific (modified) structure + There is no main() function. There are two primary functions in all Arduino programs: setup() and loop() + Setup() is executed exactly once and includes all initialization instructions. + Loop() is a function that acts as a superloop (infinite loop) for all programs. This function will be executed repeatedly as long as the Arduino is powered up, immediately after the setup() function is executed. + Other than the above, every Arduino program is a C (C++) program. You can add as much functionality as needed.