AdSense

Saturday, 24 January 2015

Read out heartbeat sensor

(Deutsche Version) If you look for "heartbeat sensor" on ebay, you find a small circuit board which contains an LED and a photo transistor. This board claims to be able to measure the heart beat. I bought one sensor and tried it. Unfortunately, there is very few (ok, basically absolutely no) documentation about the sensor and not even something like a product key, only xinda and Lcup can be found on the sensor.
The heartbeat sensor
The sensor has 3 pins which are labelled very good. One of them has a -, here you have to connect GND. The next pin has an S, this is the pin for the sensor output. The last pin (in the middle) which has no label is connected to the LED. This pin has to be connected to +5V and the sensor output has to be connected to an analog input of the arduino.

The program reads the sensor value and sends it to the computer via the serial port where you can further process the data (e.g. with Matlab, maybe I will write a post about that). Here is a picture of the plotted heartbeat data on the computer and below the code for the Arduino.
//Define pins for LED and sensor.
int ledPin = 13;
int sensorPin = 0;
//alpha is used in the original proposed code by the company (see below).
double alpha = 0.75;
//lasttime is used to have a very precise measurement of the time so the calculated pulse is correct.
unsigned long lasttime;

void setup ()
{
  //Switch on the LED.
  pinMode (ledPin, OUTPUT);
  digitalWrite(ledPin,HIGH);
  Serial.begin (9600);
  lasttime = micros();
}

void loop ()
{
  //Also used for smoothening the signal.
  static double oldValue = 0;
  
  //Wait 10 ms between each measurement.
  while(micros() - lasttime < 10000)
  {
    delayMicroseconds(100);
  }
  
  //Read the signal.
  int rawValue = analogRead (sensorPin);
  lasttime += 10000;
  
  //In the "original" code example, "value" was sent to the computer. This calculation basically smoothens "rawValue".
  //double value = alpha * oldValue + (1 - alpha) * rawValue;
  
  //Send back the measured value.
  Serial.println (rawValue);
  oldValue = value;
}

Sunday, 18 January 2015

A gaming table for tabletops

(Deutsche Version) For tabletop games you need some kind of base to play on. For the beginning, a small table or simply the floor is sufficient but if you start to have large battles, something bigger is needed. The usual size of tables for 28 mm tabletops is about 6 x 4 feet, this is about 183 x 122 cm. Such a table is quite large and needs a lot of space, it can take up to a whole room. Therefore I created a table which can be set up and dismounted within 2 minutes.

The basic idea is simple. A large plate of wood is layed onto an existing coffee table. My coffee table is about 110 x 70 cm, so a plate of 183 x 122 cm would hang down. Therefore, the gaming table required to have feet. The result was a gaming table consisting of two plates of the size 183 x 61 cm which you can plug together easily. One side of the table is layed down on the coffee table whereas two feet are placed below the other side. I will now explain the single parts of the table.
The complete table.
The plates
For the plates I used 8 mm thick chipboard. A thicker plate is more stable but also heavier and more expensive. 8 mm works pretty fine for me but you shouldn't sit on top of the table.

The feet
At the end of each plate there is a foot mounted via a hinge. If the table is dismounted, you can easily fold up the foot and it does not take up much room.
The feet of the table.
Plugging together
At Obi, a local hardware store, there are special locks for closets which you can easily plug together. I attached them to both plates so you can plug the plates together.
The connection between the plates.
Positioning on the coffee table
Finallz, I glued some small pieces of wood below the table so the plate fits perfectlz onto the coffee table and cannot move anz more.
On the left the coffee table with the piece of wood below the plate.
On the table, you can now distribute the terrain or a grass mat. Alternatively, you can also put grass directly onto the plates, attach a piece of cloth or paint the plates which can be useful for large areas of water.

Tuesday, 13 January 2015

Arduino - read keypad

(Deutsche Version) Today I want to explain how to read out a simple keypad with an Arduino, as seen in the following image. You could use this keypad from ebay: Keypad.
The keypad has 8 pins. 4 in each case are for the rows respectively the columns. A pressed key connects two of these pins. 4 pins are used as an output on the Arduino, the other 4 pins as an input. I used pin 22, 24, 26 and 28 as output and 30, 32, 34, 36 as input so you can easily attach the keypad to the Arduino using a pin header.
Now a voltage is applied to one output after the other and the input pins are measured whether they receive this voltage. This shows directly which key was pressed. My code reads the pressed keys and sends them via the serial bus to the computer as soon as a key state has changed.

//For different sizes of Keypads, you can adjust the numbers here. Important: Also change the keyValues array!
const int numOuts = 4;
const int numIns = 4;
//These are the 4 output pins, adjust it if you use a different pin mapping
int outs[numOuts] = {22, 24, 26, 28};
//These are the 4 input pins, adjust it if you use a different pin mapping
int ins[numIns] = {30, 32, 34, 36};
//This array contains the values printed to the different keys
char keyValues[numOuts][numIns] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
//This array contains whether a pin is pressed or not
boolean pressed[numOuts][numIns];

void setup() {  
  Serial.begin(9600);
  //Define all outputs and set them to high
  for (int i = 0; i < numOuts; i++)
  {
    pinMode(outs[i], OUTPUT);
    digitalWrite(outs[i], HIGH);
  }
  //Define all inputs and activate the internal pullup resistor
  for (int i = 0; i < numIns; i++)
  {
    pinMode(ins[i], INPUT);
    digitalWrite(ins[i], HIGH);
  }
}

//Read whether a key is pressed
void KeyPressed()
{
  for (int i = 0; i < numOuts; i++)
  {
    //Activate (set to LOW) one output after another
    digitalWrite(outs[i], LOW);
    //Wait a short time
    delay(10);
    for (int j = 0; j < numIns; j++)
    {
      //Now read every input and invert it (HIGH = key not pressed (internal pullup), LOW = key pressed (because the output was set to LOW)
      boolean val = !digitalRead(ins[j]);
      //If the value changed, send via serial to the computer and save the value in the "pressed" array
      if (pressed[i][j] != val)
      {
        char str[255];
        sprintf(str, "%c pressed: %d", keyValues[i][j], val);
        Serial.println(str);
        pressed[i][j] = val;
      }
      
    }
    //Deactivate (set back to HIGH) the output
    digitalWrite(outs[i], HIGH);
  }
}

//Yeah, do this forever...
void loop()
{
  KeyPressed();
}