Skip to Content
Dan k
Chen
Read and write data with Arduino Yun
Explorations Dan Chen / January 6, 2015

In this post I will show you how to write data to Yun’s SD card with the PS2 track pad then play back with the servos by reading the recorded data from the SD card on Yun.

Writing the PS/2 Trackpad data to SD card.

I used  PS/2 Trackpadfrom http://www.adafruit.com/products/837

Once you open the trackpad, here is how to wire it. You can wire all 6 pins from the left side, but here I am only wiring 4 pins, the 5th and 6th pins are for right and left mouse clicks (I am not using it here).

#1: Black Wire / 5V
#2: White Wire / Digital 2 / Data
#3: Gray Wire / Digital 3 / Clock
#4: Purple Wire / GND

DSC00305

IMG_3505

#include 
#include 

// PS2 uses two digital pins
#define PS2_DATA 2
#define PS2_CLK 3
//Adafruit_PS2 ps2(PS2_CLK, PS2_DATA);
// Use thid declaration when you want the trackpad to act like a 'mouse'
// with relative positioning
//Adafruit_PS2_Mouse ps2(PS2_CLK, PS2_DATA);
// Use this declaration when you want 'absolute' tablet mode
Adafruit_PS2_Trackpad ps2(PS2_CLK, PS2_DATA);
int premillis=0;
String dataString="";
String currentdata="";

void setup() {
  
  Bridge.begin();
  delay(2000);  // wait 2 seconds
  FileSystem.begin();
  delay(2000);  // wait 2 seconds
  while (!Serial); // wait for Serial port to connect.
  //Serial.println("Filesystem datalogger\n");
  
  Serial.begin(57600);
  Serial.println("go");
  
    while (!ps2.begin()){
        Serial.println("Did not find PS2 mouse device");
        delay (100);
      }
      Serial.println("Successfully found PS2 mouse device");

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

}

uint16_t lasttap_x = 0, lasttap_y = 0;

void loop() {


  if (! ps2.readData()){  return;}
  
    //number format 180x90y29t
    currentdata=String(ps2.x, DEC) +"x"+ String(ps2.y, DEC) +"y"+ String(getTimeStamp())+"t";
  
   File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
  // if the file is available, write to it:
  if (dataFile) {
    //dataFile.println(dataString);
    dataFile.print(currentdata);
    dataFile.close();
    // print to the serial port too:
    //Serial.println(dataString);
    Serial.println(currentdata);
    
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
 
  delay(25);
}

String getTimeStamp() {

  int cmillis=millis();
  String result= String(cmillis-premillis);
  premillis=cmillis;

  return result;
}

Reading the SD Card Data

Reads the SD text file that looks something like this
100x180y99t

This means go to x position x at 100, y position at 180, and delay 99 (in miliseconds).

#include 
boolean donereading=false;


String dataString;
char c;
int x;
int y;
int t; // time servo delay
String s;

void setup() {
  // Initialize the Bridge and the Serial
  Bridge.begin();

  Serial.begin(57600);
  FileSystem.begin();

  while (!Serial); // wait for Serial port to connect.
  
      delay(2000);  // wait 2 seconds

  Serial.println("Filesystem reader\n");
}


void loop () {
  

  //open the file for reading:
  File myFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_READ);
  
  
  if (myFile && !donereading) {
    
    // read from the file until there's nothing else in it:
            while (myFile.available()) {

                        c=myFile.read();
                        s=s+String(c);
                        
                        
                        //pharse x y t, poation x, y and the time t delay
                        //number format 180x90y29t
                       
                        if ( c=='x'){
                          s.replace("x", "");
                          x=s.toInt(); 
                          s="";
                          //Serial.print("X: ");
                          //Serial.println(x);
                        }  
                        
                        else if ( c=='y'){
                          s.replace("y", "");
                          y=s.toInt(); 
                          s="";
                          //Serial.print("Y: ");
                          //Serial.println(y);
                        }
                        else if ( c=='t'){
                          s.replace("t", "");
                          t=s.toInt(); 
                          s="";
                          //Serial.print("T: ");
                          //Serial.println(t);
                          
                          
                          Serial.print("X: ");
                          Serial.print(x);
                          Serial.print(" Y: ");
                          Serial.print(y);
                          Serial.print(" T: ");
                          Serial.println(t);
                          delayMicroseconds(t*1000);

                          x=0;
                          y=0;
                          t=0;

                        }


                  
            }
           donereading=true;

   

   }  
  else if (donereading){
          myFile.close();
          Serial.println("END OF THE FILE");
          delay(10000);
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }


}

Download Writer and Reader sketch

Here is the picture of the “Play Back” based on the motion data.

DSC00247

DAN K CHEN © 2024