Hello, everyone!



Let's start this assignment!



In this assignment, I have:

  1. To write an application that interfaces with an input &/or output device.



At first I focused on the software part. So I took the decision and built on it.


Arduino Uno, Sensor shield and 2-axis joystick.


Which code? Arduino and Processing.



Both codes are very simple.


Arduino code:

It calibrates the axes of the joystick,
determines the maximum and minimum position
and sends data via port.

  
  
int Xmax = 0, Xmin = 255, Ymax = 0, Ymin = 255;
int posX, posY;
int MposX = 0, MposY = 0;


void setup() {
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  Serial.begin(9600);

  delay(500);
  for (int i = 0; i < 10; i++)
  {
    MposX += analogRead(A3);
    MposY += analogRead(A4);
    delay(10);
  }
  delay(100);
  MposX = MposX / 10;
  MposY = MposY / 10;
}

void loop()
{
  posX = analogRead(A3);
  posY = analogRead(A4);

  if (posX > Xmax)
    Xmax = posX;
  if (posX < Xmin)
    Xmin = posX;

  if (posY > Ymax)
    Ymax = posY;
  if (posY < Ymin)
    Ymin = posY;

  if (posX < MposX)
    posX = map(posX, MposX, Xmin, 500, 0);
  else
    posX = map(posX, MposX, Xmax, 501, 1000);

  if (posY < MposY)
    posY = map(posY, MposY, Ymin, 500, 0);
  else
    posY = map(posY, MposY, Ymax, 501, 1000);

  Serial.print(posX);
  Serial.print(",");
  Serial.print(posY);
  Serial.print(".");
  delay(10);
}
 
  

Processing code:

Reads the values,
parses the data in the required form,
visualizes through the mesh and the circle.

  
  
import processing.serial.*;
Serial port;


int X = 0;
int Y = 0;
String data = "";
int index = 0;

void setup()
{
  size(1200, 1200);
  port = new Serial(this, "COM5", 9600);
  port.bufferUntil('.');
  delay(1000);
}
void draw()
{ //<>//
  background( 255 );
  drawGrid();
  stroke( 0 );

  ellipse(X+600, Y+600, 20, 20);
}


void serialEvent (Serial port)
{
  data = port.readStringUntil('.');
  data = data.substring(0, data.length()-1);

  index = data.indexOf(",");

  X = int(data.substring(0, index)) -500;
  Y = int(data.substring(index+1, data.length())) - 500;
}

void drawGrid()
{
  stroke( 225 );
  for ( int i = 0; i < 120; i++ ) { 
    line( i*10, 0, i*10, height );
  }
  for ( int i = 0; i < 120; i++ ) { 
    line( 0, i*10, width, i*10 );
  }
}

 
  

The video of working on arduino uno:



Little bit later I developed PCB with ATiny45 (with Serial and i2c connectors).

USB-UART converter, PCB, joystick.


To download the Altium project click here.

Arduino code for ATtiny 45 and software Serial:

  
  

int Xmax = 0, Xmin = 255, Ymax = 0, Ymin = 255;
int posX, posY;
int MposX = 0, MposY = 0;


#include 

SoftwareSerial mySerial(2, 1); // RX, TX

void setup() {
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  mySerial.begin(4800);

  delay(500);
  for (int i = 0; i < 10; i++)
  {
    MposX += analogRead(A2);
    MposY += analogRead(A3);
    delay(10);
  }
  delay(100);
  MposX = MposX / 10;
  MposY = MposY / 10;
}

void loop()
{
  posX = analogRead(A2);
  posY = analogRead(A3);

  if (posX > Xmax)
    Xmax = posX;
  if (posX < Xmin)
    Xmin = posX;
  if (posY > Ymax)
    Ymax = posY;
  if (posY < Ymin)
    Ymin = posY;

  if (posX < MposX)
    posX = map(posX, MposX, Xmin, 500, 0);
  else
    posX = map(posX, MposX, Xmax, 501, 1000);
  if (posY < MposY)
    posY = map(posY, MposY, Ymin, 500, 0);
  else
    posY = map(posY, MposY, Ymax, 501, 1000);

  mySerial.print(posX);
  mySerial.print(",");
  mySerial.print(posY);
  mySerial.print(".");
  delay(10);
}
 
  

Processing code is the same (need to check COM port):

  
  
  
import processing.serial.*;
Serial port;


int X = 0;
int Y = 0;
String data = "";
int index = 0;

void setup()
{
  size(1200, 1200);
  port = new Serial(this, "COM11", 4800);
  port.bufferUntil('.');
  delay(1000);
}
void draw()
{ //<>//
  background( 255 );
  drawGrid();
  stroke( 0 );

  ellipse(X+600, Y+600, 20, 20);
}


void serialEvent (Serial port)
{
  data = port.readStringUntil('.');
  data = data.substring(0, data.length()-1);

  index = data.indexOf(",");

  X = int(data.substring(0, index)) -500;
  Y = int(data.substring(index+1, data.length())) - 500;
}

void drawGrid()
{
  stroke( 225 );
  for ( int i = 0; i < 120; i++ ) { 
    line( i*10, 0, i*10, height );
  }
  for ( int i = 0; i < 120; i++ ) { 
    line( 0, i*10, width, i*10 );
  }
}



 
  

The video of working on ATtiny45:


Thank you.