Bescor MP-101 and Arduino v2.4

 

This code is slightly modified to keep the motors from stalling when they run back and forth, but it appears the problem is something to do with motors going to ground.

/*
*
* This program controls the Bescor MP-101 Pan/Tilt system using the Wii Nunchuck
*
* Copyright (c) 2012 Matt Alford, http://www.protechy.com
* Date: Aug 14, 2012
*
* Nunchuck Library http://playground.arduino.cc/Main/WiiChuckClass
*
*/

#include "Wire.h"
#include "WiiChuck.h" // The library used by the Nunchuck

#define left 11  //Pin 11 controls left pan
#define right 3 //Pin 3 controls right pan
#define up 10   //Pin 10 controls up tilt
#define down 9     //Pin 9 controls down tilt

WiiChuck chuck = WiiChuck();

int varx = 0;  // The x-axis variable to store the value coming from the Nunchuck
int vary = 0;  // The y-axis variable to store the value coming from the Nunchuck
int absx = 0;  // The x-axis variable used to store the absolute value.
int absy = 0;  // The y-axis variable used to store the absolute value.

void setup()
{
  Serial.begin(115200); // Opening the serial port

  //TCCR2B = TCCR2B & 0b11111000 | 0x07;  //Adusting PWM frequencies for testing pins 11 and 3
  //TCCR1B = TCCR1B & 0b11111000 | 0x05;  //Pins 9 and 10

// nunchuck_setpowerpins(); // use analog pins 2 & 3 as gnd & pwr (uncomment to use WiiChuck)

  chuck.begin();
  chuck.update();
}

/* //To power the WiiChuck Adapter
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize      
}
*/

void loop()  {
  chuck.update();  // The Nunchuck values update in the loop

  // 130 is the distance from 0 on the joystick
  varx = chuck.readJoyX();  // nunchuk.analogX is the value of the x-axis
  vary = chuck.readJoyY();  // nunchuk.analogY is the value of the y-axis

  // The values used for speed
  absx = varx;
  absx = abs(absx);  // Convert the x-axis value to an absolute value
  absy = vary;
  absy = abs(absy);

  // Tilt based on the input from the joystick
  if (vary > 18)
  {

    analogWrite(up, absy * 2.5);
    digitalWrite(down, LOW);
  }

  else if (vary < -18)
  {

    analogWrite(down, absy * 2.5);
    digitalWrite(up, LOW);
  }

  // Stop tilt
  else
  {
    analogWrite(up, LOW);
    analogWrite(down, LOW);
  }

  // Pan based on the input from the joystick
  if (varx > 10)
  {

    analogWrite(right, absx * 2);
    digitalWrite(left, LOW);

  }

  else if (varx < -10)
  {
    analogWrite(left, absx * 2);
    digitalWrite(right, LOW);

  }

  // Stop pan
  else
  {
    analogWrite(right, LOW);
    analogWrite(left, LOW);
  }
   Serial.print(chuck.readJoyX());
   Serial.print(", ");  
   Serial.print(chuck.readJoyY());
   Serial.print(", ");  
   Serial.println();
}

 

Submit a Comment

Your email address will not be published. Required fields are marked *