Interface and Application Programming

"An application programming interface (API) is a way for two or more computer programs to communicate with each other." -Wikipedia

Individual Assignment

1. I wrote an application that interfaces with an output device to run my tamagotchi device.

I wrote an Arduino code that was uploaded to the microcontroller connected to the output device. This allowed me to control the motor using the button on the PCB (Fig. 1E). The .ino Arduino code can be downloaded here.

Fig. 1E. Digital button setup.

After learning how to use the graphical library and integrated development environment Processing, I wrote a code to connect to the Arduino IDE via serial communication, similar to the networking used in Week 11. I referenced a online tutorial from Sparkfun and Shonit which allowed me to establish the serial communication and create a digital button in Processing that could run my gaming device.

Fig. 1F. Activating the button & screen via a physical button then digital button.

Finally, knowing the serial communication worked, I made the digital button animate when clicked, allowing for visual feedback that signal had been sent to the microcontroller to activate the button push.

              
                #include 
                  #include 
                  #include 
                  #include 
                  
                  Adafruit_SSD1306 myDisplay(128, 64, &Wire);
                  
                  ezButton button1(D8);  // create Button object that attach to pin 8;
                  ezButton button2(D9);  // create Button object that attach to pin 9;
                  ezButton button3(D10);  // create Button object that attach to pin 10;
                  
                  const int led1Pin = D0;
                  const int led2Pin = D1;
                  char val;
                  
                  void setup() {
                    Serial.begin(115200);
                    establishContact();
                    button1.setDebounceTime(100); // set debounce time to 50 milliseconds
                    button2.setDebounceTime(100); // set debounce time to 50 milliseconds
                    button3.setDebounceTime(100); // set debounce time to 50 milliseconds
                    button1.setCountMode(COUNT_FALLING);
                    button2.setCountMode(COUNT_FALLING);
                    button3.setCountMode(COUNT_FALLING);
                    pinMode(led1Pin, OUTPUT);
                    pinMode(led2Pin, OUTPUT);
                    myDisplay.begin(SSD1306_SWITCHCAPVCC, 0x3C);
                  }
                  
                  void loop() {
                  
                    myDisplay.clearDisplay();
                    myDisplay.setTextSize(2);
                    myDisplay.setTextColor(WHITE);
                    myDisplay.setCursor(0,0);
                    myDisplay.println("Hello");
                    myDisplay.display();
                  
                    digitalWrite(led1Pin, LOW);
                    digitalWrite(led2Pin, LOW);
                    
                    button1.loop(); // MUST call the loop() function first
                    button2.loop(); // MUST call the loop() function first
                    button3.loop(); // MUST call the loop() function first
                  
                    if (Serial.available() > 0 ) {
                      val = Serial.read();
                  
                      if (val == '1') {
                  
                        Serial.println("A");
                  
                        myDisplay.clearDisplay();
                        myDisplay.println("B1 Pressed");
                        myDisplay.display();
                  
                         for(int i = 0; i < 2; i ++) {
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, HIGH);
                  
                          delay(1000);
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, LOW);
                  
                          delay(1000);
                  
                        }
                  
                        myDisplay.clearDisplay();
                        myDisplay.setCursor(0,15);
                        myDisplay.println("B1 Done");
                        myDisplay.display();
                      }
                  
                      delay(100);
                  
                    }
                    else {
                  
                      Serial.println("hello");
                  
                      if(button1.isReleased() ) {
                  
                        Serial.println("A");
                  
                        myDisplay.clearDisplay();
                        myDisplay.println("B1 Pressed");
                        myDisplay.display();
                  
                        for(int i = 0; i < 2; i ++) {
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, HIGH);
                  
                          delay(1000);
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, LOW);
                  
                          delay(1000);
                  
                        }
                  
                        myDisplay.clearDisplay();
                        myDisplay.setCursor(0,15);
                        myDisplay.println("B1 Done");
                        myDisplay.display();
                      }
                  
                      if(button2.isReleased() ) {
                    
                        Serial.println("B");
                  
                        myDisplay.clearDisplay();
                        myDisplay.println("B2 Pressed");
                        myDisplay.display();
                  
                        for(int i = 0; i < 2; i ++) {
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, HIGH);
                  
                          delay(1000);
                  
                          digitalWrite(led1Pin, LOW);
                          digitalWrite(led2Pin, HIGH);
                  
                          delay(1000);
                        }
                  
                        myDisplay.clearDisplay();
                        myDisplay.setCursor(0,15);
                        myDisplay.println("B2 Done");
                        myDisplay.display();
                      }
                  
                      if(button3.isReleased() ) {
                      
                        Serial.println("C");
                  
                        myDisplay.clearDisplay();
                        myDisplay.println("B3 Pressed");
                        myDisplay.display();
                  
                        for(int i = 0; i < 2; i ++) {
                  
                          digitalWrite(led1Pin, HIGH);
                          digitalWrite(led2Pin, HIGH);
                  
                          delay(1000);
                  
                          digitalWrite(led1Pin, LOW);
                          digitalWrite(led2Pin, LOW);
                  
                          delay(1000);
                        }
                  
                        myDisplay.clearDisplay();
                        myDisplay.setCursor(0,15);
                        myDisplay.println("B3 Done");
                        myDisplay.display();
                      }
                  
                    }
                  
                  }
                  
                  void establishContact() {
                    while (Serial.available() <= 0) {
                    Serial.println("A");   // send a capital A
                    delay(300);
                    }
                  }
              
              

The arduino code used for the exercise



                  import processing.serial.*;
                  Serial port;    // Create an object from Serial class
                  String val;     // Data received from the serial port
                  boolean firstContact = false;
                
                  boolean button = false;
                
                  int x = 150;
                  int y = 150;
                  int w = 50;
                  int h = 50;
                
                  void setup() {
                    port = new Serial(this, "/dev/cu.usbmodem101", 115200);
                    port.bufferUntil('\n'); 
                    size(360, 360);
                    background(128,0,128);
                  }
                
                  void draw() {
                    if (button) {
                      background(0,128,0);
                      stroke(128,0,128);
                    } else {
                      background(128,0,0);
                      stroke(0,128,128);
                    }
                    
                    fill(0,0,0);
                    rect(x,y,w,h);
                  }
                
                  void serialEvent( Serial port) {
                    val = port.readStringUntil('\n');
                    
                    if (val != null) {
                      val = trim(val);
                      println(val);
                      
                      if (firstContact == false) {
                        
                        if (val.equals("A")) {
                          port.clear();
                          firstContact = true;
                          port.write("A");
                          println("contact");
                        }
                      }
                      else {
                      println(val);
                
                      if (mousePressed == true) {                   
                        port.write('1');        //send a 1
                        
                        if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
                        button = !button;
                        }
                        
                        println("1");
                      }
                
                      port.write("A");
                      }
                    }
                  } //<>//
                

The .pde Processing code above



Group Assignment

2. I compared interface and application programming tool options with my group.

This week, students in the Architecture section used different platforms to create applications. I read about Mateo's Three.js, Danny's openFrameworks, and Simon's Rhino Grasshopper applications. I compared these with my use of Processing. My conclusion was that Three.js was best for making dynamic and interactive visualizations, openFrameworks seemed to be good for video processing applications, and Rhino Grasshopper seemed to be useful for making environments without having to write code. I hope to explore these programs more in the future and see if these initial observations hold true.