ece3400-2017

ECE 3400 Intelligent Physical Systems course web page and assignments

This project is maintained by CEI-lab

ECE3400 Fall 2017

Introduction to Arduinos

By Kirstin Petersen, Aug 25th 2017.

This tutorial gives a very brief introduction to Arduino Unos. (Basically everything we went over in Lecture 2, but more legible).

Arduino IDE

Arduino IDE is one of the most popular popular tool for programming Arduino boards. IDE stands for Integrated Development Environment. The interface is meant to enable people with very little experience to program exciting robots/devices. This also means that it integrates a large number of libraries automatically to make sure everything works.

void setup() {
  //Put your setup code here; this function will run once after reset. 
  //(initialize input/output pins, analog pins, ADC, timers, whatever you need). 
}
void loop() {
  //Put your main code here. Once the setup-routine has finished this function will keep running in a loop.
}

Libraries

//You can write a comment in your code like this
/* or like this if
you want to encase multiple lines*/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); 
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Delay-function

Variables

Functions

void loop() { blink_LEDS(); }

void blink_LEDS(void) { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }

* The 'voids' note that the function takes no parameters and returns none. 
* We could also change the function to take an input, e.g.:
```C
void loop() {
  blink_LEDS(4); //Blink LEDs four times
  while(1); //This is always true, therefore after blinking the LED the processor will idle forever
}

void blink_LEDS(char repeats) {
  for(char i=0; i<= repeats; i++) 
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
  } //For every time this for-loop runs, i is incremented by one - it finishes when i is greater than repeats
}

bool blink_LEDS(char repeats) { for(char i=0; i<= repeats; i++) { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } //This for-loop finished when i is greater than repeats return 1; } ```

Tips and tricks