Hi I hope I put my question on the right thread, else sorry ![]()
I just went thru arduino experiment last week, new and still learning, i just got lucky to make my experiment run via Visual studio C#, my current problem is I'm confuse on how to create and where to create a formula to control my stepcount once a user input a number, would it be on the arduino or the visual studio IDE. At present my code method is within arduino IDE. My codes are working via specific methods and position only without inputData from user.Looking forward for someone to give me a reference to learn. Thank you in advanced. My code is below for arduino.
#include <L298N.h>
#include <Stepper.h>
// Define Constants
String readString;
// Stepper Motor1 Connections to A4988
const int M1_dirPin = 2; // Direction
const int M1_stepPin = 3; // Step
// Motor1 steps per rotation
const int M1_STEPS_PER_REV = 1.8;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Connected via serial"); // so I can keep track of what is loaded
// Setup the pins as Outputs for Stepper Motor1
pinMode(M1_stepPin,OUTPUT);
pinMode(M1_dirPin,OUTPUT);
//<-------------- STEPPER MOTOR1 -------------->
void stm1A(){
// Set motor direction clockwise
digitalWrite(M1_dirPin,HIGH);
// Spin motor two rotations quickly 45 deg
for(int x = 0; x < (M1_STEPS_PER_REV * 25); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1B(){
// Set motor direction clockwise
digitalWrite(M1_dirPin,HIGH);
// Spin motor two rotations quickly 90 deg
for(int x = 0; x < (M1_STEPS_PER_REV * 50); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1C(){
// Set motor direction clockwise
digitalWrite(M1_dirPin,HIGH);
// Spin motor two rotations quickly 180 deg
for(int x = 0; x < (M1_STEPS_PER_REV * 100); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1D(){
// Set motor direction clockwise
digitalWrite(M1_dirPin,HIGH);
// Spin motor two rotations quickly 360 deg
for(int x = 0; x < (M1_STEPS_PER_REV * 200); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1E(){
// Set motor direction counterclockwise
digitalWrite(M1_dirPin,LOW);
// Spin motor two rotations quickly
for(int x = 0; x < (M1_STEPS_PER_REV * 25); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1F(){
// Set motor direction counterclockwise
digitalWrite(M1_dirPin,LOW);
// Spin motor two rotations quickly
for(int x = 0; x < (M1_STEPS_PER_REV * 50); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1G(){
// Set motor direction counterclockwise
digitalWrite(M1_dirPin,LOW);
// Spin motor two rotations quickly
for(int x = 0; x < (M1_STEPS_PER_REV * 100); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void stm1H(){
// Set motor direction counterclockwise
digitalWrite(M1_dirPin,LOW);
// Spin motor two rotations quickly
for(int x = 0; x < (M1_STEPS_PER_REV * 200); x++) {
digitalWrite(M1_stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(M1_stepPin,LOW);
delayMicroseconds(1000);
}
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char sdata = Serial.read();
readString += sdata;
Serial.println(readString);
}
//<-------------- STEPPER MOTOR1 -------------->
if (readString == ("stm1A")){
stm1A();
readString="";
}
if (readString == ("stm1B")){
stm1B();
readString="";
}
if (readString == ("stm1C")){
stm1C();
readString="";
}
if (readString == ("stm1D")){
stm1D();
readString="";
}
if (readString == ("stm1E")){
stm1E();
readString="";
}
if (readString == ("stm1F")){
stm1F();
readString="";
}
if (readString == ("stm1G")){
stm1G();
readString="";
}
if (readString == ("stm1H")){
stm1H();
readString="";
}
}
Old code reposted
