Examples - Wiring Lib - Compass

Wiringlib - Compass

This example shows the use of a digital compass in Processing using the WiringLib. No coding in Wiring required!

Download Images.zip

Copy this code to your Processing Sketch



// 31 October 2008

// Infrared
// by Christoph Wartmann and Etienne Ribeiro


import processing.core.*;
import gnu.io.*;
import Wiring.*;
import processing.serial.*;


// Const
static int DigitalPin = 0; // DigitalPin: connect your compass to this pin
static int minKompassVal = 1000;
static int maxKompassVal = 40000;
static String comPort = "COM4";


// Var
SerialLib objSerialLib;
int valKompass = 0;
PImage imgKompass;
PImage imgNadel;


void setup(){

        size(400,400);

        imgKompass = loadImage("kreiselkompass.jpg");
        imgNadel = loadImage("kreiselkompassNadel.jpg");
        objSerialLib = new Wiring.SerialLib(this, comPort);

}


public void draw(){

        // background img:
        background (0);
        image(imgKompass, width/2-imgKompass.width/2, height/2-imgKompass.height/2);


        // Direction
        imageMode(CORNERS);
        translate(width/2, height/2);
        rotate(radians(valKompass * 360 / (maxKompassVal-minKompassVal)));
        image(imgNadel, -1 * imgNadel.width/2, -1 * imgNadel.height/2, imgNadel.width/2, imgNadel.height/2);


        // Get Impulse
        objSerialLib.getImpulse(DigitalPin, true, 'B', false); // Option 'B': mesure twice to avoid wrong results


        // Have a break
        delay(25);

}


void SerialData(Integer v1, Integer v2, Integer v3, Integer v4, Integer v5, Integer v6, Integer v7, Integer v8) {

        if ((char)v1.intValue() == 'I' && (char)v2.intValue() == 'I' && v3.intValue() == DigitalPin) {
                println (v4.intValue());
                if (v4.intValue() == 0 && v5.intValue() == 0)
                return;
                valKompass = v4.intValue() * 256 + v5.intValue();
        }

}

This website has been archived and is no longer maintained.