Dear all,
Currently, I am developing a project in controlling a stepper motor using leap motion which is a hand-tracking camera sensor. The project consists of two sections. The first section is using a python to get hand coordinate data from the hand-tracking sensor and send these data to the Arduino IDE through serial communication. Second part is the teensy 4.1 will receive the coordination data to control the stepper motor.
Part 1: Python code verification
The code is tested correct by looking into the message printed in cmd. The command is sent from python to teensy in this format "WSJ5stepPin:J5dirPin?value_to_write". For testing purpose, I only consider controlling one stepper motor, so the command will always be in this form "WS8:9?an integer value" for now.
Part 2: Teensy code verification
The code is tested correct when the command is observable from the cmd, the stepper motor rotates as expected. Else (hand is not tracked), the motor stops. However, the motor rotates very very slow (which is not as expected with the delay(1))
Problem: I am suspecting there is something wrong with my code written in arduino IDE (referring to Part 2). Below is the code. Appreciate for any suggestion and advice on solving this! Do let me know if more explanation is needed.
Thank you.
char operation; // Holds operation (R, W, ...)
char mode; // Holds the mode (D, A)
int pin_number; // Holds the pin number
int digital_value; // Holds the digital value
int value_to_write; // Holds the value that we want to write
int wait_for_transmission = 1; // Delay in ms in order to receive the serial data
int direction;
unsigned long currentTime = 0;
unsigned long previousTime = 0;
// create stepper motor object to control a servo
// a maximum of eight stepper objects can be created
const int J5stepPin = 8;
const int J5dirPin = 9;
const int J5calPin = 31;
int limit5 = 0;
int step5 = 1000;
void setup() {
Serial.begin(9600); // Serial Port at 9600 baud
Serial.setTimeout(100); // Instead of the default 1000ms, in order
// to speed up the Serial.parseInt()
pinMode(J5stepPin, OUTPUT);
pinMode(J5dirPin, OUTPUT); //HIGH = GO TOWARDS LIMIT SWITCH
pinMode(J5calPin, INPUT);
}
void stepper_write(int stepPin, int dirPin, int rotation_value){
if (stepPin==J5stepPin)
{
digitalWrite(dirPin, LOW); //MOVE UP
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
delay(1);
}
}
void loop() {
if (Serial.available() > 0)
{
operation = Serial.read();
delay(wait_for_transmission); // If not delayed, second character is not correctly read
mode = Serial.read();
pin_number = Serial.parseInt(); // Waits for an int to be transmitted
if (Serial.read()==':')
{
direction = Serial.parseInt(); // Collects the value to be written
if (Serial.read()== '?')
{
value_to_write = Serial.parseInt();
}
}
if (operation == 'W'){
if (mode == 'S'){
stepper_write(pin_number, direction, value_to_write);
}
}
}
}