intro to arduino programming. draw your circuits before you build them from arduino 330 ohm from...
TRANSCRIPT
Intro to Arduino Programming
Draw your circuits before you build them
From Arduino330 Ohm
From Arduino330 Ohm
From Arduino330 Ohm
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
Estimate angle of robot
Set motor voltage
Measure motor rotation
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
Syntaxif(conditional statement){
Your code goes here}Exampleif(testVal == number)if(testVal < number)if(testVal > number1 && testVal < number2 )
else if and elseSyntaxif(conditional statement){}else if(conditional statement){}else{}
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
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
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)
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.
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);
Functions help make your code much more readable
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