Download presentation
Presentation is loading. Please wait.
Published byEleanor Powell Modified over 9 years ago
1
Intro to Arduino Programming
2
Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm
3
Use comments and meaningful variable names
4
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
5
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
6
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?
7
if statements Syntax if(conditional statement) { Your code goes here } Example if(testVal == number) if(testVal < number) if(testVal > number1 && testVal < number2 )
8
else if and else Syntax if(conditional statement) { } else if(conditional statement) { } else { }
9
for loop Example: for(int i = 0; i<10 ; i++) { Serial.println(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 monitor output This code will now run 10 times
10
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
11
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)
12
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.
13
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);
14
Functions help make your code much more readable
15
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.