Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Touchéの仕組みを理解 - ProcessingとArduinoの連携

Touchéの仕組みを理解 - ProcessingとArduinoの連携

Atsushi Tadokoro

June 16, 2014
Tweet

More Decks by Atsushi Tadokoro

Other Decks in Technology

Transcript

  1. "SEVJOPͱ1SPDFTTJOHͷ࿈ܞ ‣ 1SPDFTTJOHͱ"SEVJOPΛ࿈ܞ͢Δํ๏͸େ͖͘෼͚ͯͭ ! ‣ طଘͷϥΠϒϥϦΛ࢖༻͢Δํ๏ ‣ "SEVJOPଆʹ'JSNBUBϥΠϒϥϦ ‣ 1SPDFTTJOHଆʹ"SEVJOPϥΠϒϥϦ

    ‣ 1SPDFTTJOH͔Β௚઀"SEVJOPͷೖग़ྗΛૢ࡞͢Δ ! ‣ γϦΞϧ௨৴Λࣗ࡞͢Δํ๏ ‣ ֎෦ϥΠϒϥϦ͸࢖༻͠ͳ͍ ‣ "SEVJOPͱ1SPDFTTJOH૒ํͰɺγϦΞϧೖग़ྗͷϓϩάϥϛϯά
  2. "SEVJOPͱ1SPDFTTJOHͷ࿈ܞ ‣ 1SPDFTTJOHͱ"SEVJOPΛ࿈ܞ͢Δํ๏͸େ͖͘෼͚ͯͭ ! ‣ طଘͷϥΠϒϥϦΛ࢖༻͢Δํ๏ ‣ "SEVJOPଆʹ'JSNBUBϥΠϒϥϦ ‣ 1SPDFTTJOHଆʹ"SEVJOPϥΠϒϥϦ

    ‣ 1SPDFTTJOH͔Β௚઀"SEVJOPͷೖग़ྗΛૢ࡞͢Δ ! ‣ γϦΞϧ௨৴Λࣗ࡞͢Δํ๏ ‣ ֎෦ϥΠϒϥϦ͸࢖༻͠ͳ͍ ‣ "SEVJOPͱ1SPDFTTJOH૒ํͰɺγϦΞϧೖग़ྗͷϓϩάϥϛϯά 5PVDIÉ͸ͪ͜Βͷํ๏
  3. ࿈ܞ"SEVJOPϥΠϒϥϦͱ'JSNBUBΛ࢖༻͢Δํ๏ ‣ 1SPDFTTJOHଆͰҎԼͷίʔυΛ࡞੒ import processing.serial.*; import cc.arduino.*; ! Arduino arduino;

    int ledPin = 13; int portNum = 2; color bgColor = color(0); ! void setup() { size(400, 200); arduino = new Arduino(this, Arduino.list()[portNum], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); } ! void draw() { background(bgColor); }
  4. ࿈ܞ"SEVJOPϥΠϒϥϦͱ'JSNBUBΛ࢖༻͢Δํ๏ ‣ 1SPDFTTJOHଆͰҎԼͷίʔυΛ࡞੒ ! void mousePressed() { arduino.digitalWrite(ledPin, Arduino.HIGH); bgColor

    = color(255,0,0); } ! void mouseReleased() { arduino.digitalWrite(ledPin, Arduino.LOW); bgColor = color(0); }
  5. ࿈ܞ"SEVJOPϥΠϒϥϦͱ'JSNBUBΛ࢖༻͢Δํ๏ ‣ ·ͣ͸ɺ఍߅஋ΛจࣈͰදࣔ1SPDFTTJOHଆϓϩάϥϜ import processing.serial.*; import cc.arduino.*; Arduino arduino; int

    input = 0; int portNum = 2; ! void setup() { size(400,200); arduino = new Arduino(this, Arduino.list()[portNum], 57600); } ! void draw() { background(0); fill(255); int analog = arduino.analogRead(input); text("input = " + analog, 10, 20); }
  6. ࿈ܞ"SEVJOPϥΠϒϥϦͱ'JSNBUBΛ࢖༻͢Δํ๏ ‣ ྫ͑͹ɺ఍߅஋Ͱԁͷେ͖͞ΛมԽͤͯ͞ΈΔ import processing.serial.*; import cc.arduino.*; Arduino arduino; int

    input = 0; int portNum = 2; ! void setup() { size(800,600); arduino = new Arduino(this, Arduino.list()[portNum], 57600); } ! void draw() { background(0); fill(31, 127, 255); int analog = arduino.analogRead(input); float diameter = map(analog, 0, 1023, 0, height); ellipse(width/2, height/2, diameter, diameter); fill(255); text("input = " + analog, 10, 20); }
  7. ࿈ܞγϦΞϧ௨৴ͷϓϩάϥϜΛࣗ࡞ ‣ "SEVJOPଆϓϩάϥϜ int firstSensor = 0; int secondSensor =

    0; int inByte = 0; ! void setup(){ Serial.begin(9600); establishContact(); } ! void loop(){ if (Serial.available() > 0) { inByte = Serial.read(); firstSensor = analogRead(0)/4; delay(10); secondSensor = analogRead(1)/4; Serial.write(firstSensor); Serial.write(secondSensor); } }
  8. ࿈ܞγϦΞϧ௨৴ͷϓϩάϥϜΛࣗ࡞ ‣ 1SPDFTTJOHଆϓϩάϥϜ import processing.serial.*; ! Serial myPort; int[] serialInArray

    = new int[2]; int serialCount = 0; PVector pos; boolean firstContact = false; ! void setup() { size(800, 600); noStroke(); pos = new PVector(width/2, height/2); println(Serial.list()); String portName = Serial.list()[5]; myPort = new Serial(this, portName, 9600); } ! void draw() { background(0); fill(31, 127, 255); ellipse(pos.x, pos.y, 20, 20); }
  9. ࿈ܞγϦΞϧ௨৴ͷϓϩάϥϜΛࣗ࡞ ‣ 1SPDFTTJOHଆϓϩάϥϜ void serialEvent(Serial myPort) { int inByte =

    myPort.read(); if (firstContact == false) { if (inByte == 'A') { myPort.clear(); firstContact = true; myPort.write('A'); } } else { serialInArray[serialCount] = inByte; serialCount++; if (serialCount > 1) { pos.x = map(serialInArray[0], 0, 255, 0, width); pos.y = map(serialInArray[1], 0, 255, 0, height); println(pos.x + "\t" + pos.y); myPort.write('A'); serialCount = 0; } } }