' {$STAMP BS2} ' {$PBASIC 2.5} ' ' Copyright (c) 2007 quirkfactory.com ' All rights reserved. ' ' BasicStamp program to decode a 7-sensor single-track ' Gray code disk. ' ' Read from the 7-sensor array, on pins 0-6. Print the ' sequence number after performing a look up. ' ' This program suffices for sensor value look-ups with ' a working encoder disk; if things aren't working you'll ' need to add some debugging information. ' ' counter VAR Byte sensor VAR Byte last VAR Byte char VAR Byte index VAR Byte ' This is an array of 126 sequence numbers. We'll look ' through this list to determine what angle we're at with ' a given input from the sensors. DATA 1, 5, 13, 9, 73, 89, 121, 125, 61, 53, 55, 39, 37, 101, 69, 71, 7, 3, 2, 10, 26, 18, 19, 51, 115, 123, 122, 106, 110, 78, 74, 75, 11, 15, 14, 6, 4, 20, 52, 36, 38, 102, 103, 119, 117, 85, 93, 29, 21, 23, 22, 30, 28, 12, 8, 40, 104, 72, 76, 77, 79, 111, 107, 43, 59, 58, 42, 46, 44, 60, 56, 24, 16, 80, 81, 17, 25, 27, 31, 95, 87, 86, 118, 116, 84, 92, 88, 120, 112, 48, 32, 33, 35, 34, 50, 54, 62, 63, 47, 45, 109, 105, 41, 57, 49, 113, 97, 96, 64, 66, 70, 68, 100, 108, 124, 126, 94, 90, 91, 83, 82, 114, 98, 99, 67, 65 DEBUG CR ' we'll measure 100 angle transitions, then stop counter = 100 DO WHILE counter > 0 sensor = 0 ' ' shift each bit into 'sensor' ' sensor = sensor | IN6 sensor = sensor << 1 sensor = sensor | IN5 sensor = sensor << 1 sensor = sensor | IN4 sensor = sensor << 1 sensor = sensor | IN3 sensor = sensor << 1 sensor = sensor | IN2 sensor = sensor << 1 sensor = sensor | IN1 sensor = sensor << 1 sensor = sensor | IN0 ' flip sensor bits to agree with the on/off ' orientation of the encoder disk sensor = sensor ^ %1111111 ' only print if sensor indicates a different ' angular position from last loop IF sensor <> last THEN ' map sensor value to angular position GOSUB map ' remember value for next loop, decrement count last = sensor counter = counter - 1 ENDIF LOOP DEBUG CR,"done",CR END ' map the current sensor value to an index in the DATA ' array, which indicates an angular position (0 to 125) ' ' not super-efficient, but it works map: FOR index = 0 TO 125 READ index,char IF char = sensor THEN DEBUG "position: ",DEC index,CR RETURN ENDIF NEXT ' if we get here, we have a sensor value which ' failed to map (presumably 0 or 127) DEBUG "sensor value failed to map: ",DEC sensor,CR RETURN