Output from 1 arduino to use as input of the other arduino

Hi everyone,

As part of my project, I have 2 PC’s, 1 as the client and 1 as the server. I can send the information between the PC’s but I’m not exactly sure what function to use for communicating between the arduinos. I’ve got a servo-gripper connected to 1 arduino and a pressure sensor connected to the other. I want to move the servo-gripper depending on the pressure sensor arduino output i.e the information send from the client. I’m not exactly sure how to do that

Below is the code I used to move the servo-gripper depending on the pressure sensor. Now, I want to connect them to different arduinos over the internet. The analogRead(sensorPin) would be the output of the client arduino.

#include <Servo.h>

const int servoPin = 8;
const int sensorPin = A0;

int pos;
int val;

Servo myservo;

void setup(){
Serial.begin(115200);
myservo.write(80); // Open claw.
pos = 80;
myservo.attach(servoPin );
}

void loop(){
openClaw();
delay(2000);
closeClaw();
delay(2000);
}

void openClaw() {
if(analogRead(sensorPin) < 10){
myservo.write(80); // Go to open position
delay(100);
pos = 80;
}
}

void closeClaw() {
for (; pos > 25; pos -= 1) {

if(analogRead(sensorPin) > 10){
break;
}
else{
myservo.write(pos);
}
delay(50);
Serial.println (myservo.read());
}
}

First reading the rules on how to post on the forum would be a good start...

Once you understand how to post code and question you can come back with the missing information - like how do you plan to have them interconnected on the internet for example, what latency is acceptable, can you afford to lose some data, ...

I've got a robot arm and I'm controlling it over Robot Operating Software (ROS). The pressure sensor is attached to the robot arm gripper. This pressure sensor is attached to an arduino. The output from the pressure sensor is used to move an external servo-electric gripper attached to another arduino. The movement of the servo-electric gripper which is the output from the second arduino is then send to the robot arm over ROS. The gripper of the robot arm will now move accordingly. The 2 arduinos are connected to different PCs but the same network but by using a network emulator, I'll be investigating packet loss delay etc.

I'm a newbie! Please forgive me! :'(

Why have you now got three Threads about the same project? It is much easier to help when all the info is in one place.

Why have you two separate Arduinos? Wouldn't it make life a lot easier if the sensor was on the same Arduino as the servo?

...R

I'm so sorry! I should've put them all in 1 thread. I thought different questions need different threads.

I'm investigating how internet delay affects the receiving and sending of information so I'm using a network emulator to do that. That's why I need to use 2 arduinos, 1 for the pressure sensor and 1 for the servo where the information from the sensor is send to the servo through the internet.

You've got massive delays in your code - anything the Internet introduces will be trivial by comparison.

My concern with internet delays is that they may be very unpredictable. Something might work fine for a week and then have a 5 minute delay.

…R

Yeah, I get what you mean. Without the delay in the arduino code, it doesn't work for some reason

sgaabdu4: Yeah, I get what you mean. Without the delay in the arduino code, it doesn't work for some reason

I hope that is not a response to my Reply #6, because I certainly was not advocating the use of delay() in an Arduino program

...R

Guys! I feel like crying right now. I honestly have no clue on how server and client information is transferred using PySerial. I’ve got a code for the server receiving information from the client i.e the pressure sensor readings (data =recv (1024) below in the python code). I need to use that information to move the gripper accordingly. I have no idea on how to get the data=recv (1024) information which ranges from 0 to 900 to be used in the arduino program. Please help and forgive me if the code looks really bad. I have no idea what I’m doing.

Python Code
#!/usr/bin/python # This is server.py file

import socket # Import socket module
import serial, time
s = socket.socket() # Create a socket object
host = ‘192.168.32.51’ # Get local machine name
port = 51000 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port

s.listen(5) # Now wait for client connection.
print host

arduino = serial.Serial(’/dev/ttyACM1’, baudrate=115200, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
#timeout=1)

(c,addr) = s.accept()
print 'Connection address: ',addr
while True:
data = c.recv(1024)
arduino.flush()
if not data: break
print 'received data: ',data
arduino.write(str(data)+"\n")

c.send(‘ECHO’)
#test = arduino.readline()
#print "from arduino " + test
#time.sleep(1) #give the connection a second to settle

#while True:
#data = arduino.readline()
#if data:
#print data.rstrip(’\n’) #strip out the new lines for now

(better to do .read() in the long run for this reason

#c.close()
s.close()

Arduino code

#include <Servo.h>
#include <stdlib.h>

const int servoPin = 8;

int pos;
int val;
//char* c;
long int newNumber;

Servo myservo;

void setup(){
Serial.begin(115200);
myservo.write(90); // Open claw.
pos = 90;
myservo.attach(servoPin );
}

void loop(){
char incoming [11] = " ";
int i = 0;
while (Serial.available() > 0) {
// read the incoming byte:
incoming = Serial.read();

  • i++;*

  • }*

  • //Convert the incoming string into an integer!*

  • newNumber = atoi(incoming);*

  • Serial.println(newNumber);*

  • openClaw();*

  • delay(200);*

  • closeClaw();*

  • delay(200);*

  • //Serial.println (a);*
    }
    void openClaw() {

  • if(newNumber< 6){*

  • myservo.write(90); // Go to open position*

  • pos = 90;*

  • }*
    }
    void closeClaw() {
    // char c= ;

  • for (; pos > 5; pos -= 2) {*

  • if(newNumber > 6)*

  • {*

  • break;*

  • }*

  • else*

  • {*

  • myservo.write(pos);*

  • }*

  • delay(20);*

  • }*
    }
    //for loop/ while loop read all the characters

I think the delays are not synchronised. I have no idea how to get them synchronised.

I've not read your code posted without tags. too painful. please see how to use the forum

Serial communication is asynchronous in nature. Trying to second guess what delay is necessary to handle that correctly is doomed.

You have no idea of how to post code properly either.

Use [code][/code] tags, or you risk infecting your code with italics

#include <Servo.h>
#include <stdlib.h>

const int servoPin = 8;

int pos;
int val;
//char* c;
long int newNumber;

Servo myservo;

void setup(){
  Serial.begin(115200);
  myservo.write(90); // Open claw.
  pos = 90;
  myservo.attach(servoPin );
}

void loop(){
   char incoming [11] = "          ";
    int i = 0;
    while (Serial.available() > 0) {
        // read the incoming byte:
        incoming[i] = Serial.read();
        i++;
    }
    
    //Convert the incoming string into an integer!
    newNumber = atoi(incoming);
     Serial.println(newNumber);
  openClaw();
  delay(200);
  closeClaw();
  delay(200);
  //Serial.println (a);
}

void openClaw() {
  
  if(newNumber< 10){
    myservo.write(90); // Go to open position
    pos = 90;
  }
}

void closeClaw() {
//  char c= ;
  for (; pos > 5; pos -= 2) {

    if(newNumber > 10)
    {
      break;
    }
    else
    {
      myservo.write(pos);
    }
    delay(20);

  }
}
//for loop/ while loop read all the characters

Python code

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module
import serial, time
s = socket.socket()         # Create a socket object
host = '192.168.32.51' # Get local machine name
port = 51000                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
print host

arduino = serial.Serial('/dev/ttyACM1', baudrate=115200, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
#timeout=1)

(c,addr) = s.accept()
print 'Connection address: ',addr
while True:
	data = c.recv(1024)
	arduino.flush()
	if not data: break
	print 'received data: ',data
	arduino.write(str(data)+"\n")
	#
	c.send('ECHO')
	time.sleep(1.262)
	#test = arduino.readline()
	#print "from arduino " + test
	#time.sleep(1) #give the connection a second to settle

	#while True:
		#data = arduino.readline()
		#if data:
			#print data.rstrip('\n') #strip out the new lines for now
		# (better to do .read() in the long run for this reason
#c.close()
s.close()

This

while (Serial.available() > 0) {
        // read the incoming byte:
        incoming[i] = Serial.read();
        i++;
    }

Will only ever collect a single character.

Robin2 has a serial handling basics topic which I recommend you read.

This Python - Arduino demo may help.

And have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

…R

Serial is S. L. O. W.

Imagine you are locked inside your house and sometimes somebody pushes a piece of paper under the door. There is only one letter on the paper. This week you get a "1", next week you get a "2". The next week you don't get anything.

So did somebody just try to send you the number 12 or are they part-way through sending 123 and you just didn't get the "3" yet?

Even worse, they might have tried to send two numbers and some papers were lost so the "1" is the end of one number and the "2" is the first part of the next number.