/*
-------------------------------------------------------------
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;
}
}
}