1 / 15

Intro to Arduino Programming

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.

wauna
Download Presentation

Intro to Arduino Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Arduino Programming

  2. Draw your circuits before you build them 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm From Arduino

  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 Measure motor rotation Estimate angle of robot Set motor voltage

  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 Serial monitor output The variable i here is our counter You need to declare the variable with “int” Example: for(inti = 0; i<10 ; i++) { Serial.println(i); } 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 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. intvarName = 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 wasinterrupted. 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

More Related