1

I am developing and app and I would like to measure the turns of a wheel. I bought a cheap bicycle odometer that counts the turns of the bike wheel using a magnet switch that closes the circuit once per turn, when the magnet is close enough to the sensor. I had the idea of adding this magnet switch to the mic circuit of an iphone headset and then use the audio frameworks to manage the connection of the mic, making a switch that can be identify when the mic is or not connected and then make a counter with it. The way I could make the switch was using the AVFoundation framework with [AVCaptureDevice] class, as I found out be the only way to differentiate external and internal mic following [this-post] answered question.

I paste an excerpt from my code to test the switch. The code in in the loop of a timer.

NSString *name;
static int micSwitch=0;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
    for (AVCaptureDevice *device in devices) {
        name = [ device localizedName];
    }
    if ([name isEqual:@"iPhone Microphone"]) {
        micSwitch=0;
    }
    else{
        micSwitch=1;
    }

However, this switch updates slower than I need. According to my estimation, the switch will need to be updated in less than 1.5ms . Somebody has a better idea how can I do that using the audio jack input ?

Community
  • 1
  • 1
tsvilela
  • 53
  • 1
  • 6
  • My math is going to be rough. With a switch at 12' from wheel center rotating at 20Hz its speed is about 3800 sm/s. If switch is operating at 5sm radius, you have about 2ms window to detect it, it means that you need to be able to check the device once each N s where `N < 1ms`, not 50ms. – A-Live Apr 11 '13 at 22:25
  • Yes, I got your point. I have made this calc quickly just thinking about the period of the turn. But thinking about how fast the magnet passes through the sensor is the point.I measured correctly the wheel size and the sensor distance from wheel center and I got a 1.5ms window. Thanks. – tsvilela Apr 12 '13 at 00:27
  • I'm looking at the exact same project. I considered monitoring the headset play/pause button and the volume button see: https://stackoverflow.com/q/7428783/1573326 but this will of course interfere with music playback. My next idea is to mock up a switch configured as a HID device eg bluetooth keyboard or something similar to a switch used for accessibility – TimD Nov 16 '22 at 10:37

1 Answers1

0

Instead of hooking the reed switch up to the mic circuit such that it virtually connects and disconnects the headphones, have the switch directly generate noise on the mic line-in and process the audio input. Depending on how noisy the line is you may need to apply a very short-period averaging filter to the incoming audio data in order to reliably detect the spike.

If you can't get the reed switch to generate a sufficiently long and noisy spike as it pops on/off then a small coil in its place should do the trick with the magnet attached to the wheel.

Update: I found this tutorial for detecting the noise-spike of blowing into the mic - the principle for detecting the click from the reed-switch or coil should be the same (with a much shorter detection window) and the code will be very similar.

http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/

jhabbott
  • 18,461
  • 9
  • 58
  • 95
  • Tks for the advice, sorry for the delay.I followed the tutorial and tested the switch with it.It worked good, it detects the peaks, but not faster enough to make the counter.I mean, I tested with an oscilloscope app and one spike is created when the switch is activated.Then, I tested following the tutorial , but the NSLog shows the "Mic blow detected" several times, while there is just one spike. I suppose it`s because the time window the counts peakPowerForChannel is too long. Do you have any idea how I can have a shorter window, as the [recorder updateMeters] cannot be changed?? – tsvilela Apr 16 '13 at 00:50
  • Oh, I guess the filter period of the built-in level metering must be too long for your needs. In that case, instead of using `AVAudioRecorder` you'll have to process the audio yourself. Look at this sample project from Apple: http://developer.apple.com/library/ios/#samplecode/aurioTouch2/Introduction/Intro.html - if you measure how long (how many samples) the spike lasts for you can apply a very short filter of just under that length to reliably detect the spike. – jhabbott Apr 16 '13 at 10:17
  • How did you get on with this project? – jhabbott May 01 '13 at 13:18
  • Actually I stopped working on that for a while. As soon as I come back I let you updated. Thanks. – tsvilela Mar 23 '14 at 03:21