ECE 3400 Intelligent Physical Systems course web page and assignments
This project is maintained by CEI-lab
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 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.
}
//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
}
This program flashes the onboard LED in 1 second (1000ms) intervals.
#define LED_BUILTIN 13
Note: Normally you do NOT need to view these files and certainly not change them.
pinMode(13,HIGH);
pinMode(PB5,HIGH);
pinMode(LED_BUILTIN,1);
int i = 1000;
...
delay(i);
unsigned char var = 0; //Takes 1 byte of memory, range [0:255]
char var = 0; //Takes 1 byte, range [-127:127]
unsigned int var = 0; //Takes 2 bytes, range [0:2^16]
int var = 0; //Takes 2 bytes, range [0:2^15]
double var = 0; //Takes 4 bytes, range [0:2^31]
float var = 0; //Takes 4 bytes, and can hold fractions
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; } ```
Of course this is a stupid example, because blink_LEDS() can’t fail to execute, but you get the drift!
If your main file gets too cluttered, you can also move the function into a separate tab. Make sure your main file is saved first (saving it will automatically create a folder with the same name as the file). Then, in the upper right corner of the IDE, click the downward facing arrow and make a new tab. Or press ctrl+shift+n. It will ask you for the name of the new tab and then create an .ino file in your folder with the same name. Any functions you move over into the new tab can be called directly from your main routine.
Many issues can cause the Arduino to reset unexpectedly which will make the robot behavior seem off. This can be things such as a power surge, call to a non-existent interrupt, if the watchdog timer was enabled by mistake, etc. These problems can be hard to see and therefore hard to debug, so it can be a good idea to make it very (visually) clear that your Arduino has reset. An example can be to flash the onboard LED a couple of times.
The compiler isn’t perfect. Here are some standard issues you’ll run into during the class: 1) It doesn’t automatically save your code when you compile. 2) It doesn’t detect issues such as saving too big a number in a variable.
Happy coding! You’ll go through many functions in Lab 1, you can also try out stuff at home by following these instructions