Skip to content

16. Interface and application programming

link to group assignment

For my assigment i decided to work with the samd21 board that I have been using in previous assignments.

foto_placa

To get the information from two pottenciometers and one button i wrote this code in arduino, that basicly writes a line with the three lectures and print it wirh comma separated data.

int pott = 2;     //pottenciometer 1
int pottt = 4;    //pottenciometer 2
int butt = 7;     //button

void setup() {

  pinMode( pott, INPUT);
  pinMode( pottt, INPUT);
  pinMode( butt, INPUT);
  Serial.begin(9600);

}

void loop() {

  Serial.print(digitalRead(butt));
  Serial.print(",");
  Serial.print(analogRead(pottt));
  Serial.print(",");
  Serial.println(analogRead(pott));
  delay(10);

}

serial.print esquema components cable_pott

In the processing part I wantetd to make a “game” known as “Etch A Sketch” the idea was to replicate it in processing. to prepare this I need to get the information from the arduino code and be able to read it in processing.

In the processing documentation we can find an example on how to read() the serial port.

// Example by Tom Igoe

import processing.serial.*;

Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
    println(inByte);
  }
}

this will let us read the serial and get the information, and after divide it with diferent integers using String[] list = split(inString, ","); this will get the numers and make a list wit the numbers separated by commas.

Check out some other references from processing.org

import processing.serial.*;

Serial myPort;    // The serial port
PFont myFont;     // The display font
String inString;  // Input string from serial port
int lf = 10; // ASCII linefeed

int butt;
int pott;
int pottt;
int bg;
boolean dataRecived;
//int newHeight;
//int newWidth;

void setup() {
  //fullScreen(2, P2D);
  size(1226, 714);
  // You'll need to make this font with the Create Font Tool
  //myFont = loadFont("CharterBT-Italic-48.vlw");
  //textFont(myFont, 18);
  // List all the available serial ports:
  printArray(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil(lf);

  //newHeight = height/2;
  //newWidth = width/2;

  //translate(newHeight-257, newWidth-513);

  butt = 0;
  pott = 0;
  pottt = 0;
  dataRecived = false;
  background(#711A1A);
  fill(#9D8E4F);
  noStroke();
  rect(100, 100, 1026, 514);
}

void draw() {
  translate(100, 100);
  //text("received: " + inString, 10, 50);
  frameRate(600);
  noStroke();
  fill(0);
  rect(pott, pottt*0.5, 3, 3);

  if (butt == 1) {
    //translate(-100, -100);
    fill(#9D8E4F);
    noStroke();
    rect(0, 0, 1026, 514);
  }

  if (dataRecived) {
    println("butt: " + butt + "\t" + "pott: " + pott + "\t" + "   " + "pottt: " + pottt);
    dataRecived = false;
  }
}
void serialEvent(Serial p) {
  inString = p.readString();
  if (inString != null) {
    dataRecived = true;
    // split tge comma delimited string
    String[] list = split(inString, ",");
    if (list.length > 1) {
      butt = int(list[0]);
      pott = int(list[1]);
      pottt = int(trim(list[2]));
    }

    //    println("list: ");
    //    printArray(list);
    //    println("list length; " + list.length);
  }
}

game conjunt

VIDEO


Last update: June 29, 2022