Hello,
First of all many thanks in advance for any help you can bring to me.
My main problem is:
I load the code to the board and everything works. When I unplug and plug the board, it just stops working, and it won’t do anything until I load the code again.
What I’m using:
Arduino UNO + Arduino Ethernet + TP Link (to get wifi).
Two Dynamixel Servos controled through 74LS241.
Arduino pro mini to control a Zoom H4n recorder remotely.
Smartphone to send and receive OSC packages to the micro through wifi.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <DynamixelSerial.h>
#include <Z_OSC.h>
byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[] = { 192, 168, 1, 30 };
int serverPort = 19000;
byte destIp[] = { 192, 168, 1, 22 };
int destPort = 18000;
Z_OSCServer server;
Z_OSCClient client;
Z_OSCMessage *rcvMes;
//OSC Packets to be sent//
char oscAdr[] = "/1/fader5/";
char oscAdr2[] = "/1/fader6/";
char oscAdr3[] = "/remote";
char oscAdr4[] = "/4/multitoggle/7/1";
char oscAdr5[] = "/4/multitoggle/6/1";
char oscAdr6[] = "/4/multitoggle/5/1";
float fCount=0;
float fCount2=0;
const int DATA = 2; // Pin to control the Servos through 74LS241
long int intValue=0;
char str[]="myslid";
int count =0;
int vel1=100;
int vel2=100;
int pos=0;
int pos2=0;
int gravant=0;
void setup()
{
Ethernet.begin(myMac ,myIp);
server.sockOpen(serverPort);
Dynamixel.begin(1000000,2); // Iniciem el servo a 1Mbps al pin de control 2
delay(1000);
//Setup Servos:
Dynamixel.setMaxTorque(1,512); //Parell al 50%
Dynamixel.setMaxTorque(2,512); //Parell al 50%
Dynamixel.setSRL(1,2); // Retorna-ho tot del servo 1.
Dynamixel.setSRL(2,2); // Del servo 2
Dynamixel.setTempLimit(1,80); // Màxima temperatura de 80ºC
Dynamixel.setVoltageLimit(1,65,160); // Alimentació entre 6,5 i 16V
Dynamixel.setTempLimit(2,80); // Màxima temperatura de 80ºC
Dynamixel.setVoltageLimit(2,65,160); // Alimentació entre 6,5 i 16V
//Pins to control Arduino pro mini.
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
}
void loop()
{
if(server.available())
{
rcvMes=server.getMessage(); //If we get a packet, process it
logMessage();
}
}
//////////////////////
//Functions to send from UNO to Smartphone (works but not used yet):
//////////////////////
void sendProcess()
{
Z_OSCMessage message;
message.setAddress(destIp,destPort);
message.setZ_OSCMessage(oscAdr ,"ff" ,&fCount, &fCount2);
client.send(&message);
}
void sendProcess2()
{
Z_OSCMessage message2;
message2.setAddress(destIp,destPort);
message2.setZ_OSCMessage(oscAdr2 ,"f" ,&fCount);
client.send(&message2);
}
//Process the packet recived from the smartphone:
void logMessage()
{
uint16_t i;
float floatValue;
for(i=0 ; i<rcvMes->getArgsNum(); i++)
{
switch( rcvMes->getTypeTag(i) )
{
case 'i':
intValue = rcvMes->getInteger32(i);
if(( !strcmp(rcvMes->getZ_OSCAddress() ,"/audio1/rec"))&&(intValue==1)) //We want to record with Zoom H4n
{
if(gravant==0)
{
digitalWrite(8, LOW); // Sends the order to Arduino pro mini (which controls the recorder)
delay(200);
digitalWrite(8, HIGH);
gravant=1;
}
}
if(( !strcmp(rcvMes->getZ_OSCAddress() ,"/audio1/stop"))&&(intValue==1)) /*Same, but now we want to stop recording/*
{
if(gravant==1)
{
digitalWrite(9, LOW);
delay(200);
digitalWrite(9, HIGH);
gravant=0;
}
}
//Control position of servo 1
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/cam1/vertical"))
{
pos = map(intValue, 0, 100, 575, 775);
vel1=100;
Dynamixel.moveSpeed(1,pos,vel1);
//Deceleration when we are reaching the desired position.
while(Dynamixel.moving(1)==1)
{
if(abs(Dynamixel.readPosition(1)-pos)<300)
{
vel1=90;
}
if(abs(Dynamixel.readPosition(1)-pos)<250)
{
vel1=80;
}
if(abs(Dynamixel.readPosition(1)-pos)<200)
{
vel1=70;
}
if(abs(Dynamixel.readPosition(1)-pos)<150)
{
vel1=60;
}
if(abs(Dynamixel.readPosition(1)-pos)<100)
{
vel1=50;
}
if(abs(Dynamixel.readPosition(1)-pos)<50)
{
vel1=40;
}
Dynamixel.moveSpeed(1,pos,vel1);
if(vel1==40)
{
return;
}
}
}
//Same with servo 2
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/cam1/horitzontal"))
{
pos2 = map(intValue, 0, 100, 0, 1023);
vel2=100;
Dynamixel.moveSpeed(2,pos2,vel2);
while(Dynamixel.moving(2)==1)
{
if(abs(Dynamixel.readPosition(2)-pos2)<300)
{
vel2=90;
}
if(abs(Dynamixel.readPosition(2)-pos2)<250)
{
vel2=80;
}
if(abs(Dynamixel.readPosition(2)-pos2)<200)
{
vel2=70;
}
if(abs(Dynamixel.readPosition(2)-pos2)<150)
{
vel2=60;
}
if(abs(Dynamixel.readPosition(2)-pos2)<100)
{
vel2=50;
}
if(abs(Dynamixel.readPosition(2)-pos2)<50)
{
vel2=40;
}
Dynamixel.moveSpeed(2,pos2,vel2);
if(vel2==40)
{
return;
}
}
}
//Other orders we can get from the smartphone...nothing to do yet.
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/cam1/zoom"))
{
}
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/cam1/focus"))
{
}
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/audio3/rec"))
{
}
if( !strcmp(rcvMes->getZ_OSCAddress() ,"/audio1/pause"))
{
}
break;
}
}
}
Any other information needed please let me know.
Thanks again!