Passing data from PC to Ardiuno / CNC shield

I have a CNC shield setup with 3 motors
I want to be able to pass a byte value (Position) from a text file on my PC to Ariduno while its running. It will wait until it recieves a value != 0 and if statements will determine how far the motors move, but im unsure on how to achieve this. Also might send a "at home position" signal to the PC so it knows to send the next byte value.

There is a large portion of the code im using ommited here

void step (boolean dir, byte dirPin, byte stepperPin, int steps) {
  digitalWrite (dirPin, dir);
  delay (50);
  for (int i = 0; i < steps; i++) {
    digitalWrite (stepperPin, HIGH);
    delayMicroseconds (800);
    digitalWrite (stepperPin, LOW);
    delayMicroseconds (800);
  } 
}

void loop () {
byte Position = 0;
//Wait for obtained data here from PC and change Position value

if (Position != 0){
  if (Position == 1){step (true, X_DIR, X_STP, 100); step (true, Y_DIR, Y_STP, 0);}
  if (Position == 10){step (true, X_DIR, X_STP, 400); step (true, Y_DIR, Y_STP, 200);}
  if (Position == 23){step (true, X_DIR, X_STP, 500); step (true, Y_DIR, Y_STP, 600);}
//ect...

you could do something like this

const byte X_DIR = 13;
const byte X_STP = 12;
const byte Y_DIR = 11;
const byte Y_STP = 10;

void step (boolean dir, byte dirPin, byte stepperPin, int steps)
{
    digitalWrite (dirPin, dir);
    delay (50);
    for (int i = 0; i < steps; i++) {
        digitalWrite (stepperPin, HIGH);
        delayMicroseconds (800);
        digitalWrite (stepperPin, LOW);
        delayMicroseconds (800);
    }
}

void loop ()
{
    byte position = 0;

    // check for serial input
    if (Serial.available ())  {
        char buf [80];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';
        position = atoi (buf);
    }

    switch (position) {
    case 1:
        step (true, X_DIR, X_STP, 100);
        step (true, Y_DIR, Y_STP, 0);
        break;

   case 10:
        step (true, X_DIR, X_STP, 400);
        step (true, Y_DIR, Y_STP, 200);
        break;

    case 20:
        step (true, X_DIR, X_STP, 500);
        step (true, Y_DIR, Y_STP, 600);
        break;
    }
}        

void
setup ()
{
    Serial.begin (9600);

    pinMode ( X_DIR, OUTPUT);
    pinMode ( X_STP, OUTPUT);
    pinMode ( Y_DIR, OUTPUT);
    pinMode ( Y_STP, OUTPUT);
}

or something like this

const byte X_DIR = 13;
const byte X_STP = 12;
const byte Y_DIR = 11;
const byte Y_STP = 10;

void step (
    int  nStep, byte dirPin, byte stepPin)
{
    digitalWrite (dirPin, 0 > nStep ? false : true);

    delay (50);
    for (int i = 0; i < abs(nStep); i++) {
        digitalWrite      (stepPin, HIGH);
        delay(80);
        digitalWrite      (stepPin, LOW);
        delay(80);
    }
}

void
stepXy (
    int  xStep,
    int  yStep )
{
    step (xStep, X_DIR, X_STP);
    step (yStep, Y_DIR, Y_STP);
}

// -----------------------------------------------------------------------------
void loop ()
{
    int xStep;
    int yStep;

    // check for serial input
    if (Serial.available ())  {
        char buf [80];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        sscanf (buf, "%d, %d", &xStep, &yStep);
        stepXy (xStep, yStep);
    }
}        

void
setup ()
{
    Serial.begin (9600);

    pinMode ( X_DIR, OUTPUT);
    pinMode ( X_STP, OUTPUT);
    pinMode ( Y_DIR, OUTPUT);
    pinMode ( Y_STP, OUTPUT);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.