How to send stream of ASCII characters to serial port on mousePressed

edited March 2018 in Arduino

Hi. I'm pretty new to this so apologies if this is a no-brainer. However, I've looked around and cant find a solution to this...

The background/setup is this: I have an Arduino which has a bunch of L9110 motor drivers connected to it for 7 motors. Each one will be controlled via ASCII values sent down the serial port using a Processing GUI rather than the Arduino serial monitor. I want the motor to run while the relevant keyboard key is pressed AND also while the relevant GUI button is pressed with the mouse (so, equal functionality for both). So far the program works fine when sending key presses from the keyboard - essentially a stream of ASCII characters are sent to the Arduino which results in a series of pulses to the motor controller and this is fine. The mouse press also works fine to send a single character (and thus a pulse) to the motor controller. But I need it to send a continuous stream while the mouse is pressed.

The code for the mouse is below, with explanations for the variables shown first (and I've deleted the end curly brackets for simplicity):

RButton is the class for each motor direction button (2 for each motor, ie forward and reverse). So 14 buttons in the array. Each object in the array is called rButton isMouseOver is a boolean check to see if the mouse is over the button coordinates chgButtonCol is the boolean variable which changes the colour of the button when it is pressed arduinoPort is the variable for the serial port of my Arduino assKey is the ASCII character associated with each button :-D

void mousePressed() {
for(RButton currentButton : rButton) { if(currentButton.isMouseOver() == true) { currentButton.chgButtonCol = true; arduinoPort.write(currentButton.assKey); delay(50);

void mouseReleased() {
for(RButton currentButton : rButton) { currentButton.chgButtonCol = false;

Thanks in advance for your help...

Tagged:

Answers

  • Answer ✓

    Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Is this solved? One approach is that you send the character in mousePressed and then you send a signal in mouseReleased so for the motor to stop running. The advantage of this is that you are not flooding your serial with commands to make your motor run. Instead, you give the signal to start working and a signal to stop working. This implies you need to change your Arduino side. Another advantage is that you don't have to call delay so many times in your sketch, making your sketch more responsive. Notice that this also would apply to keyPressed and keyReleased functions...

    What OS are you using? If you still want to continuously stream characters when the mouse is pressed, you can do it by using mousePressed in draw like this:

    void draw(){
    
      if(mousePressed) {   //Yes, a variable here, not a function
          ...Identify the key pressed and send the respective signal
      }
    
    }
    

    Kf

  • Thanks very much. I'll give it a shot.

Sign In or Register to comment.