Skip to main content
Question Protected by CommunityBot
added programming tag for syntax highlighting
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). 

However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduinoArduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))
import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))

Arduino code

#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}
#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))

Arduino code

#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). 

However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the Arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))

Arduino code

#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}
deleted 268 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5

I'm trying to send 53 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# the 5 ints to send
servo0 = 0
servo1 = 45
servo2 = 90
servo3 = 135
servo4 = 180

# send the first int in binary format
bge.arduino.write(struct.pack('>B', servo045))

Arduino code

#include <Servo.h>
 
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

void setup(){
  Serial.begin(9600);
  
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
  servo3.attach(9);
  servo4.attach(10);
}

void loop(){
  
  if(Serial.available()){
    
    int message = Serial.read();
    
    // control the servosservo
    servo0.write(message);
 
  }
  
}

I'm trying to send 5 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# the 5 ints to send
servo0 = 0
servo1 = 45
servo2 = 90
servo3 = 135
servo4 = 180

# send the first int in binary format
bge.arduino.write(struct.pack('>B', servo0))

Arduino code

#include <Servo.h>
 
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

void setup(){
  Serial.begin(9600);
  
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
  servo3.attach(9);
  servo4.attach(10);
}

void loop(){
  
  if(Serial.available()){
    
    int message = Serial.read();
    
    // control the servos
    servo0.write(message);
 
  }
  
}

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works). However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))

Arduino code

#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}
deleted 169 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
Post Undeleted by ZanQdo
Post Deleted by ZanQdo
Post Undeleted by ZanQdo
Post Deleted by ZanQdo
deleted 13 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
deleted 13 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
deleted 29 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
deleted 150 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
deleted 1059 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
added 3113 characters in body
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading
Source Link
ZanQdo
  • 31
  • 1
  • 1
  • 5
Loading