How to use Mechaduino's Rx Tx pins?

102 views
Skip to first unread message

pe...@ascest.com

unread,
Sep 1, 2018, 4:19:39 PM9/1/18
to Mechaduino
Hi - I'm struggling with commanding my Mechaduino via the Rx (D0) and Tx (D1) pins. I have not had any problem obtaining the behavior I'm looking for (drum striking) by coding r values in the main loop. However, when I developed my code further to use the Rx pin to receive serial data from an Arduino I can't get a response from the Mechaduino. I can confirm that command characters are being generated from the Arduino... I feel I'm missing something but can't see what. Help to get on the right track would be very helpful. I see this question has been asked before so I'm sure any help will be of interest to others too. Thanks in advance, Peter

Receiving code on the Mechaduino:


/*
  -------------------------------------------------------------
  Mechaduino 0.1 & 0.2 mFirmware  v0.1.4
  SAM21D18 (Arduino Zero compatible), AS5047 encoder, A4954 driver

  All Mechaduino related materials are released under the
  Creative Commons Attribution Share-Alike 4.0 License

  Many thanks to all contributors!
  --------------------------------------------------------------
*/  


#include "Utils.h"
#include "Parameters.h"
#include "State.h"
#include "analogFastWrite.h"

int incomingByte;

//////////////////////////////////////
/////////////////SETUP////////////////
//////////////////////////////////////


void setup()        // This code runs once at startup
{                         
   
  setupPins();                      // configure pins
  setupTCInterrupts();              // configure controller interrupt

  SerialUSB.begin(115200);          
  delay(3000);                      // This delay seems to make it easier to establish a connection when the Mechaduino is configured to start in closed loop mode.  
  serialMenu();                     // Prints menu to serial monitor
  setupSPI();                       // Sets up SPI for communicating with encoder
  
  Serial.begin(115200);             // Starts Rx/Tx communication on Mechaduino pins D0 (Rx) and D1 (Tx)

  // Uncomment the below lines as needed for your application.
  // Leave commented for initial calibration and tuning.
  
  // configureStepDir();           // Configures setpoint to be controlled by step/dir interface
  // configureEnablePin();         // Active low, for use wath RAMPS 1.4 or similar
     enableTCInterrupts();         // uncomment this line to start in closed loop 
     mode = 'x';                   // start in position mode

}
  


//////////////////////////////////////
/////////////////LOOP/////////////////
//////////////////////////////////////


void loop()                 // main loop

{
  serialCheck();
  Serial.println ("Loop starting point");
  // see if there's incoming serial data:
  if (Serial.available() > 0) 
  {
    // read the oldest byte in the serial buffer
    incomingByte = Serial.read();
    Serial.println (incomingByte);
    
    // if it's a capital H (ASCII 72), start/continue HITTING the drum:
    if (incomingByte == 'H')
    {
       Serial.println ("H character received");
       r = 10;
       delay (random(100,110));
       r = (random(-15, -10));
       delay (random(1000,950));
    }
    // if it's an S (ASCII 83) STOP hitting the drum/continue waiting at r=10
    if (incomingByte == 'S')
    { 
      Serial.println ("S character received");
      r = 10;
    }
  }
}

Code that generates command characters ('H' and 'S') on the Arduino:
// Determine action code to send to drum based on velocity value received
    if (velocityByte > 0 && velocityByte <= 127 && hMessageSent == false)
    {
      digitalWrite(6, HIGH);
      Serial.println ('H'); // send tx message to Mechaduino to START playing
      hMessageSent = true; // Register that 'H' message was sent. Don't send more
      sMessageSent = false;

    }

    if (velocityByte == 0 && sMessageSent == false)
    {
      digitalWrite(6, LOW);
      Serial.println ('S'); // send tx message to Mechaduino to STOP playing
      sMessageSent = true; // Register that 'S' message was sent. Don't send more
      hMessageSent = false;
    }


Reply all
Reply to author
Forward
0 new messages