Skip to content

16. Interface and application programming

Group Assignment

In this week’s group assignment, we compared the difference between JAVA, C and Pyton. This helps us better understand what may be the best choice for our projects.

Individual Assignment

In the inidividual assignment part, I wrote two programs with Arduino IDE and Processing. To trigger a servo on a laser gun target board moving.

Video demo

Explaination of the programs

Part of Processing_Send_Start_Command.pde

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(255, 0, 0);
    myPort.write("Start\n");
  } else {                        // If mouse is not over square,
    fill(0);
    myPort.write("Stop\n");
  }
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= windowWidth/4) && (mouseX <= windowWidth*3/4) && (mouseY >= 50) && (mouseY <= 150));
}

Explanation: In the draw() function, the program will check mouseOverRect(), which returns true if the mouse position is over the square in the middle. If true, then send a start command to the arduino board via serial port, or a stop command if false.

Part of ServoTest.ino

bool checkStringComplete() {
  if (stringComplete) {
    Serial.println(inputString);
    if (inputString == "Start\n")
    gameStart = true; //start the game
    else if (inputString == "Stop\n")
    gameStart = false; //stop the game
    // clear the string:
    inputString = "";
    stringComplete = false;
    return true;
  }
  return false;
}

Explanation: In the checkStringComplete() function, the program will check if the input string was “Start\n”, if it’s true, the gameStart flag becomes true and cause the servo swinging. If the received string is “Stop\n”, gameStart will become false and servo stops moving.

Files Download

ServoTest.zip

Processing_Send_Start_Command.zip


Last update: June 28, 2022