2013. 11. 19.

Project 2_Coding

Coding Program
- Arduino (Control Arduino)

- Processing (Application)

Experiment

- LED Blinking using Smartphone
- Sub-Motor running

Processing

import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*; 
import ketai.ui.*;
import ketai.net.*; 
import controlP5.*;

KetaiBluetooth bt;
boolean isConfiguring = true;
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList(); 
ControlP5 cp5;
int margin = 150;
boolean ledState = false; 



import android.content.Intent;
import android.os.Bundle; 
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*; 
import controlP5.*; 


KetaiBluetooth bt;
boolean isConfiguring = true;
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList(); 
ControlP5 cp5;
int margin = 150;
boolean ledState = false;


ARDUINO
#include <SoftwareSerial.h> 
SoftwareSerial BTSrial(2, 3); // RX, TX
int led = 13;
char chByte = 0; 
int servoPin = 9;
Servo servo;
int angle = 0;
String strInput = ""; 
String strON = "ON"; 
String strOFF = "OFF";
void setup() {
    pinMode(led, OUTPUT); 
    BTSrial.begin(9600);
    servo.attach(servoPin);}
void loop() {
  while (BTSrial.available() > 0) {  
    chByte = BTSrial.read();
    if(chByte =='\r'){
        if(strInput.equals(strON)) {
          digitalWrite(led, HIGH);
        }
        if(strInput.equals(strOFF)){
          digitalWrite(led, LOW);
        }
        strInput="";
    }
    else{
      strInput += chByte;}{
      for (angle = 0; angle < 180; angle++) {
      servo.write(angle);
      delay(15);}
      for (angle = 180; angle > 0; angle--) {
      servo.write(angle);
      delay(15);
   } 
  }
}

void onCreate(Bundle savedInstanceState) 
{ super.onCreate(savedInstanceState);
bt = new KetaiBluetooth(this); 


}


void onActivityResult(int requestCode, int resultCode, Intent data) 


{ bt.onActivityResult(requestCode, resultCode, data);
}

void setup() {
size(displayWidth, displayHeight); orientation(PORTRAIT);
bt.start();
isConfiguring = true;
PFont font = createFont("Arial", 40); ControlFont cf = new ControlFont(font);

int buttonSize = min(width, height) - 2 * margin;
cp5 = new ControlP5(this);
Button btnLED = new Button(cp5, "buttonLED"); 

btnLED.setPosition(margin, (height - buttonSize) / 2);
btnLED.setSize(buttonSize, buttonSize); 
btnLED.setCaptionLabel("Turn ON the LED");
btnLED.getCaptionLabel().setControlFont(cf);
btnLED.getCaptionLabel().toUpperCase(false);
btnLED.getCaptionLabel().alignX(CENTER);
}

void draw() 
{
if (isConfiguring) { 
klist = new KetaiList(this, bt.getPairedDeviceNames());
isConfiguring = false; 
}
public void controlEvent(ControlEvent theEvent) 
{
String controllerName = theEvent.controller().name();
if(controllerName == "buttonLED")
{Button btn = (Button)theEvent.controller(); 
if(ledState){ledState = false;
btn.setCaptionLabel("Turn ON the LED");
byte[] data = {'O', 'F', 'F', '\r'}; 
bt.broadcast(data);}
else{
ledState = true; 
btn.setCaptionLabel("Turn OFF the LED");
byte[] data = {'O', 'N', '\r'}; 
bt.broadcast(data);
void onKetaiListSelection(KetaiList klist) 
{String selection = klist.getSelection(); 
bt.connectToDeviceByName(selection); 
klist = null;
}