W12 - Interface and Application Programming

1. Weekly Assignments ( -> what I did this week )

  • Group assignment

  • Compare as many tool options as possible.

( -> I tried Processing, including some libraries such as “ControlP5” and “processing.serial” )

  • Individual assignment

  • Write an application that interfaces a user with an input and/or output device that you made

( -> I wrote some interfaces to control servo motors ( SG90-HV ) by using ATtiny44 board I made week08. )

SG90 ; lever-type, limited angle

SG90-HV ; continuous rotation

test board input device software output device software
1 serial ATtiny44-board-week08 none Processing SG90-HV Arduino IDE
2 GUI none none Processing none Processing
3 GUI none none Processing none Processing
4 serial ATtiny44-board-week08 none Processing SG90-HV Arduino IDE
5 serial ATtiny44-board-week08 none Processing SG90 Arduino IDE
6 serial
2 sensor inputs
ATtiny44-board-week08 2 photoResistors Arduino IDE none Processing
7 Graphics Processing

Have you?

Questions from “Fab Academy 2020 Assignments and Assessment

Interface and Application Programming

( -> my answers )

  • Linked to the group assignment page

  • Documented your process ( -> yes )

  • Explained the UI that you made and how you did it

  • Outlined problems and how you fixed them

  • Included original code (or a screenshot of the app code if that’s not possible) ( -> yes )

  • Included a ‘hero shot/video’ of your application running with your board ( -> yes )

I tried Blynk to communicate with ESP32 via BLE.

Link

3. Works, steps and some details

1) test 1

I used SoftwareSerial library to communicate with Processing.

TX & RX connection was a little confusing.

test board input device software output device software
1 serial ATtiny44-board-week08 none Processing SG90-HV Arduino

code, Processing ” sketch_dimmer_test.pde “

code, Arduino ” Dimmer_test_ATtiny44.ino “

/* Processing code for this example

  // Dimmer - sends bytes over a serial port

  // by David A. Mellis
  // This example code is in the public domain.
  */

  import processing.serial.*;
  Serial port;

  void setup() {
    size(256, 150);

    println("Available serial ports:");
    // if using Processing 2.1 or later, use Serial.printArray()
    printArray(Serial.list());

    // Uses the first port in this list (number 0). Change this to select the port
    // corresponding to your Arduino board. The last parameter (e.g. 9600) is the
    // speed of the communication. It has to correspond to the value passed to
    // Serial.begin() in your Arduino sketch.
    port = new Serial(this, Serial.list()[5], 9600);

    // If you know the name of the port used by the Arduino board, you can specify
    // it directly like this.
    //port = new Serial(this, "COM1", 9600);
  }

  void draw() {
    // draw a gradient from black to white
    for (int i = 0; i < 256; i++) {
      stroke(i);
      line(i, 0, i, 150);
    }

    // write the current X-position of the mouse to the serial port as
    // a single byte
    port.write(mouseX);
     delay(50);
  }
#include <SoftwareSerial.h>
SoftwareSerial mySerial =  SoftwareSerial(0, 1);//rx,tx

const int ledPin = 5;      // the pin that the LED is attached to

void setup() {
  // initialize the ledPin as an output:
//  pinMode(0, INPUT);
//  pinMode(1, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // initialize the serial communication:
  mySerial.begin(9600);
//  delay(200);

}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (mySerial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = mySerial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
//    Serial.println(brightness);
//    delay(100);
  }
}

2) test 2

test board input device software output device software
2 GUI none none Processing none Processing

code, Processing ” Slider_Ball_ControlP5_test.pde “

import controlP5.*;

ControlP5 cp5;

int sliderX= 200;
int sliderY= 200;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);

  cp5.addSlider("sliderX")
    .setPosition(150, 350)
    .setSize(100, 30)
    .setRange(50, width-50)
    //    .setNumberOfTickMarks(5)
    ;

  cp5.addSlider("sliderY")
    .setPosition(50, 150)
    .setSize(30, 100)
    .setRange(height-50, 50)
    //    .setNumberOfTickMarks(5)
    ;
}


void draw() {
  background(0, 255, 255);
  ellipse(sliderX, sliderY, 50, 50);
}

3) test 3

test board input device software output device software
3 GUI none none Processing none Processing

code, processing ” Slider_ControlP5_Bouncing_Ball.pde “

import controlP5.*;

ControlP5 cp5;

float x = 300;
float y = 150;

float xSpeed = 2;
float ySpeed = 2;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);

  cp5.addSlider("xSpeed")
    .setPosition(150, 350)
    .setSize(150, 30)
    .setRange(1, 10)
    //      .setNumberOfTickMarks(5)
    ;

  cp5.addSlider("ySpeed")
    .setPosition(20, 150)
    .setSize(30, 150)
    .setRange(1, 10)
    //   .setNumberOfTickMarks(5)
    ;
}

void draw() {
  background(0, 255, 255);

  x += xSpeed;
  if (x > width-12.5 || x <  12.5) {
    xSpeed *= -1;
  }

  y += ySpeed;
  if (y > height-12.5 || y <  12.5) {
    ySpeed *= -1;
  }

  ellipse(x, y, 25, 25);
}

4) test 4

test board input device software output device software
4 serial ATtiny44-board-week08 none Processing SG90-HV Arduino IDE

code, processing ” slider_serial_test.pde “

code, Arduino ” servo_Dimmer_test_ATtiny44.ino “

// send signal to ATtiny44 (Arduino IDE), via SoftwareSerial
// signal is produced by moving ControlP5 "slider" with mouse, 
// X-potision of a circle (ellipse) represents the signal value.

import controlP5.*;
ControlP5 cp5;

import processing.serial.*;
Serial port;

int sliderX= 200;

void setup() {
  size(256, 150);

  cp5 = new ControlP5(this);

  cp5.addSlider("sliderX")
    .setPosition(50, 100)
    .setSize(130, 30)
    .setRange(0, 255)
    //    .setNumberOfTickMarks(5)
    ;

  println("Available serial ports:");

  // if using Processing 2.1 or later, use Serial.printArray()
  printArray(Serial.list());

  // Uses the first port in this list (number 0). Change this to select the port
  // corresponding to your Arduino board. The last parameter (e.g. 9600) is the
  // speed of the communication. It has to correspond to the value passed to
  // Serial.begin() in your Arduino sketch.

  port = new Serial(this, Serial.list()[5], 9600);

  // If you know the name of the port used by the Arduino board, you can specify
  // it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw() {

  background(0, 255, 255);
  ellipse(sliderX, 50, 50, 50);

  port.write(sliderX);
  delay(50);
}
/* receive signal from Processing, via SoftwareSerial
 * control servomotor
 */

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo;//サーボのインスタンス
SoftwareSerial mySerial =  SoftwareSerial(0, 1);//rx,tx (ATtiny44)

const int servoPin = 5;      // the pin that the servo is attached to

void setup() {

  //サーボの信号線を servoPin に接続
  servo.attach(servoPin);

  // initialize the serial communication:
  mySerial.begin(9600);
  delay(200);

}

void loop() {
  byte serialIn;

  // check if data has been sent from the computer:
  if (mySerial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    serialIn = mySerial.read();

  //map()を使ってセンサ読取り値を角度(回転速度)に変換
  int servospeed=map(serialIn,0,255,10,170);

    // set the serbospeed of the servo motor:
      servo.write(servospeed);

//    delay(200);
  }
}

5) test 5

test board input device software output device software
5 serial ATtiny44-board-week08 none Processing SG90 Arduino IDE

code

( same as Test4 )

6) test 6

test board input device software output device software
6 serial
2 sensor inputs
ATtiny44-board-week08 2 photoResistors Arduino IDE none Processing

code, processing ” sketch_2sensors_SSreceive_ATtiny44.pde “

code, Arduino ” 2sensors_SSsend_ATtiny44.ino “

/*
/* receive 2 sensors values via SoftwareSerial

 * based on Y. Tsuchiya, Fab Academy 2019 week16 "Interface and application programming" 
 * http://fabacademy.org/2019/labs/kamakura/students/tsuchiya-yosuke/assignments/week16/
 * ( port number etc. modified.  )
 */


import processing.serial.*;

Serial port;

boolean DEBUG = false;
int[][] values = new int[2][100];
color[] colors = {color(0, 255, 255), color(255, 255, 0), color(255, 0, 255)};


void setup() {

  size(800, 600);
  frameRate(50);
  String[] ports = Serial.list();

  if (DEBUG) {
    for (int i = 0; i < ports.length; i++) {
      println(i + ": " + ports[i]);
    }
  } else {

    println("Available serial ports:");

    // if using Processing 2.1 or later, use Serial.printArray()
    printArray(Serial.list());

    port = new Serial(this, ports[5], 9600);
  }
}

void draw() {

  background(color(30, 30, 30));
  stroke(255, 255, 255);
  textSize(16);
  text("Input Device Value", 10, 20);

  for (int i=0; i<2; i++) {
    stroke(colors[i]);
    for (int j=0; j<99; j++) {
      float a = map(values[i][j], 0, 1023, 0, 512);
      float b = map(values[i][j + 1], 0, 1023, 0, 512);

      line(8 * j, 512-a, 8* (j + 1), 512-b);
    }
  }
}

void serialEvent(Serial p) {

  if (p.available() > 0) {
    try {
      String input = p.readStringUntil('\n');
      if (input != null) {
        input = trim(input);
        String [] value = split(input, ',');
        println(value[0] +","+ value[1] );
        for (int i = 0; i < 2; i++) {
          values[i] = append(subset(values[i], 1), int(value[i]));
        }
      }
    } 
    catch (RuntimeException e) {
    }
  }
}
/* get 2 sensors values
 * send them to Processing, via SoftwareSerial
 * based on Y. Tsuchiya, Fab Academy 2019 week11 "input device" 
 * http://fabacademy.org/2019/labs/kamakura/students/tsuchiya-yosuke/assignments/week11/
 * ( port number etc. modified.  )
 */



#include <SoftwareSerial.h>

const int in1pin = 5;
const int in2pin = 6;

SoftwareSerial ss(0,1);   //rx,tx (ATtiny44)

void setup() {
  // put your setup code here, to run once:
  ss.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:

  int val = analogRead(in1pin);
  int val2 = analogRead(in2pin);

  ss.print(val2);
  ss.print(',');
  ss.print(val);
  ss.print('\n');
  delay(500);

}

7) test 7

test board input device software output device software
7 Graphics Processing

4. Important Learning Outcome

1) Although I felt some difficulties, SoftwareSerial worked fine.

2) Processing has a great potential for the control and graphics.

Test1

” sketch_dimmer_test.pde ” download

” Dimmer_test_ATtiny44.ino ” download

Test2

Slider_Ball_ControlP5_test.pde download

Test3

Slider_ControlP5_Bouncing_Ball.pde download

Test4

slider_serial_test.pde download

servo_Dimmer_test_ATtiny44.ino download

Test5

( same as Test4 )

Test6

sketch_2sensors_SSreceive_ATtiny44.pde download

2sensors_SSsend_ATtiny44.ino download

6. Appendix

ATtiny44A data sheet

[Secrets of Arduino PWM](