Hi there,
I'm working on my final task in school and am stuck with the serial communication between a Pi and Arduino.
I am using a HC-06 and HC-05 module for this to work. I've already gotten communication to work between the 2 codes in one direction, I don't need any more than this.
Here comes the problem: I need to send 2 numbers between 0-100. These represent the PWM cycle for 2 DC motors. (this has to be quite fast) If I do this I get 2 things that happen depending on how I read it.
Or I get these numbers all behind eachother, for example: If I send 90 and 85 I get: "908590859085..." in 1 string.
Or I get with the same example: "9", "0", "8", "5", etc... All in seperate strings.
What I need is to get these as 90 and 85 as a final product.
Using substrings may solve this but I have to constantly refresh the motors so I can't wait for a long string to divide. I hope I explained this clearly enough.
I use a Arduino UNO and pins 2 and 3 with softwareserial. I use a PCB so I can't chance these pins altough this really shouldn't be a problem.
If anyone knows how to do this or could give some guidelines, feel free to leave some advice or answers.
My code should be in the attachment, I'll leave my python code for good measure if this may give some insight in my thougt proces.
Thanks in advance.
Lander Renaerts
Arduino code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int totaalmotor1 = "0";
int motorlinks = 0;
int motorrechts = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ready to receive!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(totaalmotor1 = mySerial.read());
Serial.println("\n");
}
}
Python code
import time
import serial
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
while 1:
send = input("What to send: ")
ser.write(str(send))
time.sleep(1)
arduino_gip.ino (602 Bytes)