Intro to Arduino Programming Draw your circuits before you build them 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm From Arduino Use comments and meaningful variable names Develop separate pieces of code for each part of your lab or project Balancing Arduino Robot Read Accelerometer/ Gyroscope Measure motor rotation Estimate angle of robot Set motor voltage First sketch Make sure you can upload the code blink from examples/basic. If it worked, there should be a light slowly blinking on your Arduino void setup() and void loop() Your code must have only one function called setup and one called loop but They can be empty If you only want a piece of code to run once, where should you put it? if statements Syntax if(conditional statement) { Your code goes here } Example if(testVal == number) if(testVal < number) if(testVal > number1 && testVal < number2 ) else if and else Syntax if(conditional statement) { } else if(conditional statement) { } else { } for loop Example: for(int i = 0; i<10 ; i++) { The variable i here is our counter You need to declare the variable with “int” This section says to run the loop while the counter i is less than 10 This section says to add 1 to our counter each time through the for loop Serial.println(i); } This code will now run 10 times Serial monitor output The serial monitor is going to be your best friend when it comes to debugging your code The serial monitor lets you print information from your code If your code is doing something strange, try printing out different values and see if they make sense Click here Common mistakes • Forgetting SEMICOLONS • Incorrect variable types (e.g. unsigned should never go negative, use float if you need fractions) • Accidentally trying to declare a variable twice (e.g. int varName = 3; declares a new variable, later varName = 2; sets it to a new value) • Use == instead of = in an if statement • Serial lines to USB are connected to pins 0 and 1 on the Uno. You usually do not want to connect anything else to these lines. • Only pins 3, 5, 6, 9, 10 and 11 on the Uno can do PWM • Some libraries use specific pins (e.g. the Servo library deactivates PWM on pins 9 and 10) Functions Also sometimes called subroutines Functions are a great way to organize your code. They allow you to break your code in small pieces and they help you troubleshoot. The two main types of functions are void and int, but you can also use other data types if needed. Functions can return values directly example: int add(int a, int b) { int c = a + b; return c; } To call “add” in your code: sum = add(num1,num2); Functions help make your code much more readable Interrupts Interrupts are subroutines that are called by hardware pins rather than by software (e.g. int.0 is on pin 2 and int.1 is on pin 3 for Uno). They can be invoked at any time and return control to the program step where your code was interrupted. interrupts(); – enables interrupts noInterrupts(); – disables all interrupts attachInterrupt(interrupt, ISR, mode); - activates specific interrupt, specifies Interrupt Service Routine (ISR) and selects mode for the pin detachInterrupt(interrupt); - deactivates specific interrupt