Mousecam data and Servo Control

Dear Arduinians,

I am trying to merge two scripts, the first one collects coordinate data from a mousecam hack, the second translates mouse movement to servo pulses. Each script works independently, but I am hoping to merge the two so that the coordinate data from the mousecam sends a pulse to the servo directly on the arduino board. After working a while on putting it all together, the script sort of works but nothing seems to be happening. Does anyone have any idea what my next step should be?

sketch_apr05b.pde (5.05 KB)

Does anyone have any idea what my next step should be?

Probably post your code if you expect help with it.

i posted an attachment in my original post, but here comes the code aswell. I would be incredibly grateful for any assistance

#define SCLK 2
#define SDIO 3
#define PD 4

#define REG_PRODUCT_ID 0x00
#define REG_REVISION_ID 0x01
#define REG_MOTION 0x02
#define REG_DELTA_X 0x03
#define REG_DELTA_Y 0x04
#define REG_SQUAL 0x05
#define REG_AVERAGE_PIXEL 0x06
#define REG_MAXIMUM_PIXEL 0x07
#define REG_CONFIG_BITS 0x0A
#define REG_DATA_OUT_LOWER 0x0C
#define REG_DATA_OUT_UPPER 0x0D
#define REG_SHUTTER_LOWER 0x0E
#define REG_SHUTTER_UPPER 0x0F
#define REG_FRAME_PERIOD_LOWER 0x10
#define REG_FRAME_PERIOD_UPPER 0x11

#include <Servo.h>

Servo myservo; // create a servo object to control the servo

int servoPin1 = 2; // Control pin for servo motor
int servoPin2 = 9;

int minPulse = 600; // Minimum servo position
int maxPulse = 3000; // Maximum servo position

int pulse1 = 0; // Amount to pulse the servo
int pulse2 = 0;

long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int sread=0;

int dumpWidth = 256; // Number of pixels to read for each frame.
byte frame[256];

void setup() {

Serial.begin(9600); //Communication to PC ?

myservo.attach(9); //attaches servo to digital pin 9 to the servo object in program

pulse1 = minPulse; // Set the motor position value to the minimum
pulse2 = minPulse;

//Serialservo.begin(9600); //Communication from PC to servo

reset();
byte productId = readRegister(REG_PRODUCT_ID);
byte revisionId = readRegister(REG_REVISION_ID);
Serial.print(“Found productId “);
Serial.print(productId, HEX);
Serial.print(”, rev. “);
Serial.print(revisionId, HEX);
Serial.println(productId == 0x02 ? " OK.” : " Unknown productID. Carry on.”);

byte config = readRegister(REG_CONFIG_BITS);
config |= B00000001; // Don’t sleep (LED always powered on).
writeRegister(REG_CONFIG_BITS, config);
}

void loop() {
// Allows to set the dump window by sending the number of lines to read via the serial port.
if(Serial.available() > 0) {
dumpWidth = 16 * Serial.read();
dumpWidth = constrain(dumpWidth, 0, 256);

readRegister(REG_MOTION); // Freezes DX and DY until they are read or MOTION is read again.
char dx = readRegister(REG_DELTA_X);
char dy = readRegister(REG_DELTA_Y);

Serial.print(“DELTA:”);
Serial.print(dx, DEC);
Serial.print(" ");
Serial.println(dy, DEC);

// minPulseDX = //something related to dx
// maxPulseDX = //something else related to dx

// minPulseDY = //something related to dy
// maxPulseDY = //something else related to dy

// pulse1 = map(sread,0,180,minPulseDX,maxPulseDX); // convert the analog value
// pulse2 = map(sread,0,180,minPulseDY,maxPulseDY); // to a range between minPulse

pulse1 = int(dx); //Might need some conversion from char to int.
pulse2 = int(dy); //Same same.

// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime){

myservo.write(pulse1); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there

lastPulse = millis(); // save the time of the last pulse
}
}
if( dumpWidth > 0 )
dumpFrame();
}

void dumpFrame() {
byte config = readRegister(REG_CONFIG_BITS);
config |= B00001000; // PixDump
writeRegister(REG_CONFIG_BITS, config);

int count = 0;
do {
byte data = readRegister(REG_DATA_OUT_LOWER);
if( (data & 0x80) == 0 ) { // Data is valid
frame[count++] = data;
}
}
while (count != dumpWidth);

config = readRegister(REG_CONFIG_BITS);
config &= B11110111;
writeRegister(REG_CONFIG_BITS, config);

Serial.print(“FRAME:”);
for(int i = 0; i < dumpWidth; i++) {
byte pix = frame*;*

  • if( pix < 0x10 )*
  • Serial.print(“0”);*
  • Serial.print(pix, HEX);*
  • }*
  • Serial.println();*
    }
    void reset() {
  • pinMode(SCLK, OUTPUT);*
  • pinMode(SDIO, INPUT);*
  • pinMode(PD, OUTPUT);*
  • digitalWrite(SCLK, LOW);*
  • digitalWrite(PD, HIGH);*
  • delayMicroseconds(1);*
  • digitalWrite(PD, LOW);*
    }
    byte readRegister(byte address) {
  • pinMode (SDIO, OUTPUT);*
  • for (byte i=128; i >0 ; i >>= 1) {*
  • digitalWrite (SCLK, LOW);*
  • digitalWrite (SDIO, (address & i) != 0 ? HIGH : LOW);*
  • digitalWrite (SCLK, HIGH);*
  • }*
  • pinMode (SDIO, INPUT);*
  • delayMicroseconds(100); // tHOLD = 100us min.*
  • byte res = 0;*
  • for (byte i=128; i >0 ; i >>= 1) {*
  • digitalWrite (SCLK, LOW);*
  • digitalWrite (SCLK, HIGH);*
  • if( digitalRead (SDIO) == HIGH )*
  • res |= i;*
  • }*
  • return res;*
    }
    void writeRegister(byte address, byte data) {
  • address |= 0x80; // MSB indicates write mode.*
  • pinMode (SDIO, OUTPUT);*
  • for (byte i = 128; i > 0 ; i >>= 1) {*
  • digitalWrite (SCLK, LOW);*
  • digitalWrite (SDIO, (address & i) != 0 ? HIGH : LOW);*
  • digitalWrite (SCLK, HIGH);*
  • }*
  • for (byte i = 128; i > 0 ; i >>= 1) {*
  • digitalWrite (SCLK, LOW);*
  • digitalWrite (SDIO, (data & i) != 0 ? HIGH : LOW);*
  • digitalWrite (SCLK, HIGH);*
  • }*
  • delayMicroseconds(100); // tSWW, tSWR = 100us min.*
    }
dumpWidth = 16 * Serial.read();

How much is 16 * '1'?

thanks for all the awesome help. the problem was a few lines of code. it works now. if anyone is interested in the working code i can upload it

If you do upload it, please use the # (code) icon on the editor's toolbar.

hackney22: thanks for all the awesome help. the problem was a few lines of code. it works now. if anyone is interested in the working code i can upload it

I am interested, and also would like you to refresh my memory about mousecam hack. Sounds like fun! Thanks!

Cheers, Kari