FabShop: Arduino

Microcontrollers are too much fun

Salvatore "Sal" Testa /@saltesta14

March 15, 2014

What is Arduino?

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. -Arduino.cc

How does it work?

I/O plus Logic

INPUT

buttons, accelerometers, heat sensors, switches, photo resistors

OUTPUT

LEDs, motors, speakers

LOGIC

How do I make a simple Arduino program?

Code structure


void setup()
{
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
}
                    

Basic Example


int RED_LED = 13;

void setup()
{
  pinMode(RED_LED, OUTPUT);   // set the 13 pin to output
  digitalWrite(RED_LED, HIGH); // set the 13 pin high
}

void loop()
{
}
                    

Run the code!

Blink


int RED_LED = 13;

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

void loop()
{
  digitalWrite(RED_LED, HIGH); // turn the light on
  delay(1000);                 // wait 1000 milliseconds
  digitalWrite(RED_LED, LOW);  // turn the light off
  delay(1000);                 // wait 1000 milliseconds
}
                    

Syntax Shtuff

Functions


void party() {
  digitalWrite(RED_LED, HIGH);
  delay(1000);                
  digitalWrite(RED_LED, LOW); 
  delay(1000);                
}
                    

Party Time


// ... some other code
party();
party();
party();
// ... some more code
                    

Other Types Functions


int max(int num1, num2) {
    ...
}
int avg(int[] nums) {
    ...
}
void goToCrawlStop(int collegeNumber) {
    ...
}
                    

If Statement


if (statement) {
    // this code only gets called
    // if statement is true
}
                    

If Example


int max(int num1, num2) {
    if(num1 > num2) {
        return num1;
    } else {
        return num2;
    }
}
                    

I/O Example

Button + LED


int LED = 10;
int BUTTON = 3;

void setup() {
    pinMode(LED, OUTPUT);
    pinMode(BUTTON, INPUT); // input mode
}

void loop() {
    int readVal = digitalRead(BUTTON); // read the button
    if(readVal == HIGH){
        digitalWrite(LED, HIGH); // on if the button is down
    } else {
        digitalWrite(LED, LOW); // off otherwise
    }
}
                    

Thank you!

howtmm.com/fabshop/