Exercise 16

Interface and application programming

The interface I have programmed enables the user to remote control the Dalek of my final project via bluetooth and serial communication. The UI shown below catches the mouse position and mouse clicks. For example if the mouse hovers over the forward rectangle and the mouse is cliked an 'w' is sent via serial communication to the board inside the dalek. On the board the switch statement then activates both motors resulting in forward movement. The LEDs start to blink when the blink button is pressed once and stop blinking if the button is pressed a second time and so forth. If no communication is available one can simply change the mode string to one of many modes and also add more modes. Below is a demonstration of the wireless communication showing the remote controlls working with the motors. I had struggle to get the bluetooth communication to work since it was working fine with wired connection through an msp430 board.

Code: Board(Energia IDE)

String mode = "remote";
boolean blink = false;
char val;

void setup()
{
  Serial.begin(9600);

  pinMode(12, OUTPUT); //motor1
  pinMode(13, OUTPUT); //motor2
  //pinMode( 9, OUTPUT); //speaker
  pinMode( 8, OUTPUT); //LEDs
  pinMode( 2, OUTPUT); //test
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
}

void loop()
{
  if(mode == "drivebeepdrive"){
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
    delay(3000);
    for(int i = 0; i <=3; i++){
      digitalWrite(8, HIGH);
     // digitalWrite(9, HIGH);
      delay(300);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      Serial.println("test");
      delay(300);
    }
    delay(3000);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
    delay(5000);
  }
  
  
  if(mode == "driveledblink"){
    digitalWrite(9, LOW);
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
    digitalWrite(8, HIGH);
    delay(500);
    digitalWrite(8, LOW);
    delay(300);
  }
  
  if(mode == "remote"){
    while (Serial.available()){
      val = Serial.read();
      switch(val){
        case 'w':     digitalWrite(12, HIGH);
                      digitalWrite(13, HIGH); break;
        case 'a':     digitalWrite(12,  LOW);
                      digitalWrite(13, HIGH); break;
        case 'd':     digitalWrite(12, HIGH);
                      digitalWrite(13,  LOW); break;
        case 'b':     blink = true;
                      digitalWrite(8, HIGH);
        case 'n':     digitalWrite(12, LOW);
                      digitalWrite(13, LOW);
                      digitalWrite(8, LOW);
                      blink = false;        
      }
      if(blink){
        digitalWrite(8, HIGH);
        delay(500);
        digitalWrite(8, LOW);
        delay(500);
      }
    }
  }
}

Code: Inteface(Processing)

import processing.serial.*;

Serial myPort;

void setup(){
  size(640, 360);
  background(0);
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw(){
  //forward
  rect(100, 50, 100, 100);
  //left
  rect(0, 150, 100, 100);
  //right
  rect(200, 150, 100,100);
  //blink
  rect(400, 150, 100, 100);
  fill(0, 0, 100);  
  textAlign(CENTER, CENTER);
  text("Forward", 150, 100);
  text("Left", 50, 200);
  text("Right", 250, 200);
  text("Blink", 450, 200);
  fill(255);
  
  //forward
  if(mouseX >= 100 && mouseX <= 200 && mouseY >= 50 && mouseY <= 150 && mousePressed){
    myPort.write('w');
    fill(0);
    rect(100, 50, 100, 100);
    fill(255);
  } else
  //left
  if(mouseX >= 0 && mouseX <= 100 && mouseY >= 150 && mouseY <= 250 && mousePressed){
    myPort.write('a');
    fill(0);
    rect(0, 150, 100, 100);
    fill(255);
  } else
  //right
  if(mouseX >= 200 && mouseX <= 300 && mouseY >= 150 && mouseY <= 250 && mousePressed){
    myPort.write('d');
    fill(0);
    rect(200, 150, 100,100);
    fill(255);
  }else
  //blink
  if(mouseX >= 400 && mouseX <= 500 && mouseY >= 150 && mouseY <= 250 && mousePressed){
    myPort.write('b');
    fill(0);
    rect(400, 150, 100, 100);
    fill(255);
  } else
  //nothing
  {
    myPort.write('n');
  }
}