Hi,
First i dont know much about coding, that's why i need help!
I got Nunchuck with NANO controling Bescor MP-101 working well.
from http://protechy.com/bescor-mp-101-and-arduino-v3-0/
I got two NANO communicating over RS 485 i think working well....
from: http://www.gammon.com.au/forum/?id=11428
Now i need merging both together....
I got parts of it "working" but i miss the knowledge to go further....
There is my actual MASTER script:
/*
*
* This sketch controls the Bescor MP-101 Pan/Tilt system using the Wii Nunchuck
*
* Copyright (c) 2015 Matt Alford, http://www.protechy.com
* Date: April 6, 2015
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses></http:>.
* I am not responsible for any damage that may occur from the use of this software.
* Nunchuck Library http://playground.arduino.cc/Main/WiiChuckClass
*
*
*RS485 from Nick Gammon
*http://www.gammon.com.au/forum/?id=11428
*
*/
#include "Wire.h"
#include "WiiChuck.h" // The library used by the Nunchuck
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
// From original script i dont need this animore because Bescore is attached to slave
#define left 12 //Pin 11 controls left pan
#define right 11 //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, vary, absx, absy, mapx, mapy = 0;
//------------------485-----------------------
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
SoftwareSerial rs485 (2, 3); // receive pin, transmit pin
// callback routines
void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
//------------------485-----------------------
void setup()
{
//-----------------485 setup
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
// end of setup
//----------485 end of setup
Serial.begin(115200); // Opening the serial port
TCCR2B = TCCR2B & 0b11111000 | 0x07; //Adjusting 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();
}
//------485---------
byte old_level = 0;
//------485---------
//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 = abs(varx); // Convert the x-axis value to an absolute value
absy = abs(vary);
int buttonC = chuck.buttonC;
Serial.println(buttonC);
int buttonZ = chuck.buttonZ;
Serial.println(buttonZ);
// Map the x/y value to get the full range
if (buttonZ == 0 && buttonC == 0)
{
mapx = map(absx, 0, 100, 0, 60);
mapy = map(absy, 0, 100, 0, 60);
}
if (buttonZ == 0 && buttonC == 1)
{
mapx = map(absx, 0, 100, 0, 100);
mapy = map(absy, 0, 100, 0, 100);
}
if (buttonZ == 1 && buttonC == 0)
{
mapx = map(absx, 0, 100, 0, 150);
mapy = map(absy, 0, 100, 0, 150);
}
if (buttonZ == 1 && buttonC == 1)
{
mapx = map(absx, 0, 100, 0, 220);
mapy = map(absy, 0, 100, 0, 220);
}
// Tilt based on the input from the joystick
if (vary > 18)
{
analogWrite(up, mapy);
digitalWrite(down, LOW);
}
else if (vary < -18)
{
analogWrite(down, mapy);
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, mapx);
digitalWrite(left, LOW);
}
else if (varx < -10)
{
analogWrite(left, mapx);
digitalWrite(right, LOW);
}
// Stop pan
else
{
analogWrite(right, LOW);
analogWrite(left, LOW);
}
Serial.print(chuck.readJoyX());
Serial.print("X, ");
Serial.print(chuck.readJoyY());
Serial.print("Y, ");
Serial.print(mapx);
Serial.print("MapX, ");
Serial.print(mapy);
Serial.print("MapY, ");
Serial.println();
//--------------485-loop--------------
// read potentiometer
//byte level = analogRead (0) / 4; //original script
byte level = mapx; //to test data and it works.... it sent PWM Nunchuck value to Slave
// no change? forget it keep me at the ent of sketch!
if (level == old_level)
{
// return;
/* digitalWrite (LED_PIN, HIGH);
delay(10);
digitalWrite (LED_PIN, LOW);
delay(10);*/
}
// assemble message
byte msg [] = {
1, // device 1
2, // turn light on
level // to what level
};
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
// receive response
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
digitalWrite (LED_PIN, received == 0); // turn on LED if error
// only send once per successful change
if (received)
old_level = level;
//-485 end of loop-----------------
}
And my SLAVE script (nothing changed yet from example from Nick Gammon except RX TX pins):
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (5, 6); // receive pin, transmit pin
const byte ENABLE_PIN = 4;
void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
if (buf [0] != 1)
return; // not my device
if (buf [1] != 2)
return; // unknown command
byte msg [] = {
0, // device 0 (master)
3, // turn light on command received
};
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
analogWrite (11, buf [2]); // set light level
} // end if something received
} // end of loop
On my Master where is my first obstacle is to get some real value to level
//byte level = analogRead (0) / 4; //original script
byte level = mapx; //to test data and it works.... it sent PWM Nunchuck value to Slave
Also the original RS485 example was to send one potentiometre reading to a light attached to slave,
i need four PWM values to be sent to slave.... left right up down
eventually i like to add Sony Lanc code to it but it's an other storry...
I need some programmer willing to help, please let me know how much you would charge to do it!
Thanks
Daniel