0

Serial Connection Problem between Processing and CircuitPython Code on a Adafruit Grand Central M4 Metro.

I have a Adafruit Grand Central M4 Metro Micro-Computer with 3 Motors connected to it. On the Grand Central are to Files with CircuitPython Code on it. The first one is for the initialization of the 3 Motors and the Definition of the Movements of them. The Motors are connected to the Pins: D5, D6 ; D14, D15 ; D52, D53. The Code of the second File that imports the Code from the first File is for the Function of the Motors shown below:

import time
import board
from motor import *
import busio

uart = busio.UART(board.TX, board.RX, baudrate=9600)

def vorwärts():
    while True:
        motor1_R1(30)
        motor2_R2(30)

def rückwärts():
    while True:
        motor1_R2(30)
        motor2_R1(30)

def drehung_rechts():
    while True:
        motor1_R2(30)
        motor2_R2(30)
        motor3_R2(30)

def drehung_links():
    while True:
        motor1_R1(30)
        motor2_R1(30)
        motor3_R1(30)

while True:
    data = uart.readline()
    if data is not None:
        received_data = data.decode().strip()
        if received_data == 'L':
            drehung_links()
        elif received_data == 'R':
            drehung_rechts()
        

Now I want to controll these Functions with Buttons that I visualized in Processing. I am sending the Signals via the serial connection:

import processing.serial.*;

Serial myPort;

void setup() {
  size(400, 400); // Größe des Fensters festlegen
  
   String portName = "COM3";
  
  myPort = new Serial(this, portName, 9600); 
}

   if (mouseX > 50 && mouseX < 150 && mouseY > 50 && mouseY < 150) {
    // Linker Button wurde geklickt
    // Sende Signal über die serielle Schnittstelle
    myPort.write('L');
    println("Linker Button wurde geklickt");
  }
  
   if (mouseX > 250 && mouseX < 350 && mouseY > 50 && mouseY < 150) {
    // Rechter Button wurde geklickt
    // Sende Signal über die serielle Schnittstelle
    myPort.write('R');
    println("Rechter Button wurde geklickt");
  }

My Problem is that the Motors does not move at all if I press or hold one of the Buttons, but if I start a Function manuel in the CircuitPython Code, the Motors move, so the Problem must be the serial connection. Does anyone have an Idea what the Problem could be?

3
  • In your receiving routine you use uart.readline() to get the data, that means you read a line at a time. Your sending routine sends single characters without an EOL char. Perhaps it works when you send a '\n' as well. Commented May 24, 2023 at 7:54
  • Doesn't change anything. Commented May 24, 2023 at 15:48
  • First you should find out where the problem is, sending side or receiving side. Instead of your Processing code use a terminal emulation (as in Thonny or VSCode already present, or use putty) with correct settings. Then try to send an L or R via this terminal. Do that trigger the motors? With a terminal emulation you can also add a print() to your code to generate log messages. So you can visualize the variable "data" etc. Commented May 25, 2023 at 10:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.