Connecting Arduino Ethernet shield with Processing using Ethernet Client

Hi to all
I am trying to communicate with my Arduino ethernet shield over a Mega and Processing. I have seen several examples using the serial port, but I need to use Ethernet communications. I want to take advantage of Game Control Plus to use an XBox controller to communicate with Processing, then send commands over an ethernet cable to an ethernet shield/Arduino Mega board. Connecting the Xbox controller to processing was fairy simple for me. I am using EthernetClient protocol to talk to the Mega, but here is where I get confused. I can send one bit, byte, word, not sure the proper term of a command from the Xbox's Y-axis which is a -1 to +1 range and map it to a servo output using a PWM output with a range of 0 to 180. The PWM output will be connected to a motor controller (eg Talon, Spark) that sees 0 to 90 as Forward and 90 to 180 as Reverse. I need to connect 6 motors and a couple of Lights for a Remote Underwater Vehicle ROV. Having just one neutral buoyant ethernet cable makes things so much easier to use than a multiconductor cable. I can talk from Processing to the Mega as long as I only us e one motor. When I include 3 motors in my sketch, the latency drops to 3 minutes. I piecemealed my code from a number of sources so if you ask why I did such and such on line 28 for example I'll have to plead ignorance. I really don't know much about client servers or sending bytes or strings. Maybe I'm using the wrong protocol or maybe I'm just close and need a tweak here or there. If you can look at what I've done or offer something different, I would be greateful. I left out a lot of the control code in the Arduino sketch until I could get the 3 slider signals to work properly. The codes were retyped not copied from my working computer so if there are any syntax errors, it is because of this. I did not run this program, but the premise is the same.

Processing Code

import g4p_controls.;
import Processing.net.
;
import processing.serial.*;

import net.java.games.input.;
import org.gamecontrolplus.
;
import org.gamecontrolplus.gui.*;

import cc.arduino.;
import org.firmata.
;

ControlDevice cont;
ControlIO control;
Arduino arduino;
Client myClient;

float leftRightMotion;
float fwdRevMotion;
float upDownMotion;
int i;
int j;
int k;
boolean xButton;
boolean yButton;
boolean aButton;
boolean bButon;

void setup(){
size(230,230);
control = ControlIO.getInstance(this);
cont = control.filter(GCP.GAMEPAD).getMatchedDevice("ROVServo");
if (cont == null){
println("no valid controller connected");
System.exit(-1); }

myClient = new Client (this, "192.168.137.211", 80); // IP address of Mega
}

public void getUserInput() {

fwdRevMotion = map(cont.getSlider("FwdRev").getValue(), -1, 1, 0, 180); // grabs the z-Axis from the XBox Controller
leftRightMotion = map(cont.getSlider("LeftRight").getValue(), -1, 1, 0, 180);
upDownMotion = map(cont.getSlider("UpDwn").getValue(), -1, 1, 0, 180);
xButton = cont.getButton("FwdSpdSw").pressed;
yButton = cont.getButton("RevSpdSw").pressed;
aButton = cont.getButton("Light1").pressed;
}

void draw() {
getUserInput();
background(200, 100,255); // changes window from gray to purple to prove program is running

   if(xButton)
     myClient.write('H'); // Buttons to select fast/slow speeds or turn on lights
     
   if (yButton)
      myClient.write('A');
      
   if (aButton)
      myClient.write('C');
      
   
   
  
    int i = (int) fwdRevMotion; // covert the float to an int
    int j = (int) leftRightMotion;
    int k = (int) upDownMotion;
    myClient.write(i); 
    myClient.write(j);
    myClient.write(k);
     println(i);   // displays value on console to see if 'i' responds from controller signal
      delay(150);
      }

Arduino Code

#include <servo.h>
#include <SPI.h>
#include <Ethernet.h>

int i;
int j;
int k;

int talon1_pin= 8; //Left Mtr 1
int talon1_pin= 9; //Left Mtr 2
int talon1_pin= 11; // Right Mtr 3
int talon1_pin= 12; // Right Mtr 4
int talon1_pin= 13; // Up Mtr 5
int talon1_pin= 14; // Down Mtr 6

byte mac[] = {0xBB, 0xCA, 0x3A, 0xC8, 0x0D, 0x30);
IPAddress ip(192, 168, 137, 211);
IPAddress gateway ( 192, 168, 137 ,1);
IPAddress subnet (255,255,255,0);

EthernetServer server(80);
boolean alreadyConnected = false;

Servo talon1;
Servo talon12;
Servo talon3;
Servo talon4;
Servo talon5;
Servo talon6;

float mtrLeft1Fwd;
float mtrLeft1Rev;
float mtrLeft2Fwd;
float mtrLeft2Rev;
float mtrRight3Fwd;
float mtrRight3Rev;
float mtrRight4Fwd;
float mtrRight4Rev;
float mtrUp5;
float mtrDown6;
float fwdRevMotion;
float leftRightMotion;
float upDownMotion;

int Led25 = 25;
int Led27 = 27;

void setup() {
Ethernet.begin(mac,ip,gateway, subnet);
server.begin;
Serial.begin(9600);
Serial.print("Web Server Address is: ");
Serial.println(Ethernet.localIP());

talon1.attach(talon1_pin);
talon2.attach(talon1_pin);
talon3.attach(talon1_pin);
talon4.attach(talon1_pin);
talon5.attach(talon1_pin);
talon6.attach(talon1_pin);
}

void loop() { // bracket 1
EthernetClient client = server.available;
if(client) { //bracket 2
boolean currentLineIsBlank = true;
String buffer = "";

  While client.connected()) { //bracket 3
        if(client.available() >0 { // bracket 4
           i =client.read();
          j = client.read();
          k =client.read();

float fwdRevMotion = (float) i; // convert the int i back to a float
float leftRightMotion= (float) j:
float upDownMotion (float) k;

talon1.write(fwdRevMotion); // checks to make sure the signal makes it to one controller

Serial.print("Fwd Rev Motion is: "); // for the other values, a print statement is used
Serial.print(fwdRevMotion);
Serial.print(""):
Serial.print("left Right Motion is : ");
Serial.print(leftRightMotion):
Serial.print("");
Serial.print(Up Down motion is : ");
Serial.println(UpDownMotion);

       }   //bracket 1  
       }  // bracket 2
      }   // bracket 3
      }  // bracket 4

Interesting I just saw a question about Rover control, maybe you can find similar solutions together:

https://forum.arduino.cc/t/sending-complex-data-across-i2c-arduino/967619/2

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.