Transcript
Page 1: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Intro to Arduino Programming

Page 2: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Draw your circuits before you build them

From Arduino330 Ohm

From Arduino330 Ohm

From Arduino330 Ohm

Page 3: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Use comments and meaningful variable names

Page 4: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Develop separate pieces of code for each part of your lab or project

Balancing Arduino Robot

Read Accelerometer/

Gyroscope

Estimate angle of robot

Set motor voltage

Measure motor rotation

Page 5: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

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

Page 6: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

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?

Page 7: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

if statements

Syntaxif(conditional statement){

Your code goes here}Exampleif(testVal == number)if(testVal < number)if(testVal > number1 && testVal < number2 )

Page 8: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

else if and elseSyntaxif(conditional statement){}else if(conditional statement){}else{}

Page 9: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

for loop

Example:

for(int i = 0; i<10 ; i++){

Serial.println(i);}

The variable i here is our counterYou 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 monitor output

This code will now run 10 times

Page 10: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

The serial monitor is going to be your best friend when it comes to

debugging your codeThe serial monitor lets you print information from your codeIf your code is doing something strange, try printing out different values and see if they make sense

Click here

Page 11: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

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)

Page 12: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

FunctionsAlso 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.

Page 13: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Functions can return values directlyexample:int add(int a, int b){

int c = a + b;return c;

}

To call “add” in your code:sum = add(num1,num2);

Page 14: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

Functions help make your code much more readable

Page 15: Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm

InterruptsInterrupts 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 interruptsnoInterrupts(); – disables all interruptsattachInterrupt(interrupt, ISR, mode); - activates specific interrupt, specifies Interrupt Service Routine (ISR) and selects mode for the pindetachInterrupt(interrupt); - deactivates specific interrupt


Top Related