1 //COM3
2 //pins
3 int buttonPin = 11;
4 int button = 0;
5 int comPin = 12; //Communication pin to other stepper motor
6 int stoPin = 10; //Pin that stops other stepper motor simultaneously.
7 int ledPin = 13;
8 int pins[] = {2, 5, 3, 4};
9
10 //Variables
11 int stepCount = 0;
12 String steps = "";
13
14 //Functions
15 void stepUp(int pins[], int comPin, int stoPin, String steps, int stepDelay);
16 void stepDown(int pins[], int comPin, int stoPin, String steps, int stepDelay);
17
18
19 void setup() {
20 //Stepper Pins
21 pinMode(pins[0], OUTPUT);
22 pinMode(pins[1], OUTPUT);
23 pinMode(pins[2], OUTPUT);
24 pinMode(pins[3], OUTPUT);
25 pinMode(ledPin, OUTPUT); //Built in led for debugging
26 pinMode(buttonPin, INPUT); //Push button pin
27
28 //Start Terminal
29 Serial.begin(9600);
30 }
31
32
33 void loop() {
34 if (digitalRead(buttonPin) == HIGH){ //If button pressed
35 button = HIGH;
36 }
37 if (button == HIGH){
38 while (Serial.available() > 0) { //While text is available in Serial port
39 steps = Serial.readStringUntil('\n');
40 if (steps[0] == 'z'){ //If Z dimension motor is prioritized, send information to other motor
41 pinMode(comPin, OUTPUT);
42 pinMode(stoPin, OUTPUT);
43 digitalWrite(stoPin, LOW); //If Y dimension motor is prioritized, recieve all information from other motor.
44 }else if (steps[0] == 'y'){
45 pinMode(comPin, INPUT);
46 pinMode(stoPin, INPUT);
47 }
48 if (steps[1] == 'd'){ //If 2nd letter is d for down
49 stepDown(pins, comPin, stoPin, steps, 500);
50 }else if (steps[1] == 'u'){ //If 2nd letter is u for up
51 stepUp(pins, comPin, stoPin, steps, 500);
52 }
53 }
54 }
55 }
56
57
58 //move Z dimension motor down
59 void stepDown(int pins[], int comPin, int stoPin, String steps, int stepDelay){
60 stepCount = 0;
61 if (steps[0] == 'z'){ //If z is prioritized, send info
62 while (stepCount < steps.length()){
63 if (steps[stepCount] == '1'){ //1 represents y motor
64 digitalWrite(comPin, HIGH);
65 }
66 digitalWrite(ledPin, HIGH); //Led for debugging
67 digitalWrite(pins[4-stepCount%4], HIGH); //First pin turns on
68 delay(stepDelay); //Delay controls speed
69 digitalWrite(pins[4-stepCount%4], LOW); //Turn previous varible off
70 digitalWrite(ledPin, HIGH);
71 digitalWrite(comPin, LOW);
72 stepCount++;
73 }
74 digitalWrite(stoPin, HIGH); //Stop other motor aswell
75 Serial.print(steps);
76 } else if (steps[0] == 'y') { //If y is prioritized, recieve info
77 while (true){
78 if (digitalRead(comPin) == HIGH){ //If other ardiuno sent data
79 digitalWrite(ledPin, HIGH); //led for debugging
80 digitalWrite(pins[4-stepCount%4], HIGH); //First variable turns on
82 delay(stepDelay); //Delay controls speed
82 digitalWrite(pins[4-stepCount%4], LOW); //Turn previous varible off
83 digitalWrite(ledPin, LOW);
84 }
85 if (digitalRead(stoPin) == HIGH)
86 break;
87 }
89 }
90 }
Hello ya'll,
My code tries to take serial input and control two stepper motors at the same time, by having two Arduinos talk to each other. Each serial input starts with the character z (for z dimension motor) and y (for y dimension motor). This is specifically my z dimension code, so we also check for the letter d for down, and u for up. The next characters of the input consist of a series of ones and zeros, which represents moving the y motor and z motor in a particular order. Lets just say for example my input is "zd001001001001" By using pin 13 for debugging, These are the if statements on lines 40 and 48:
if (steps[0] == 'z'){
pinMode(comPin, OUTPUT);
pinMode(stoPin, OUTPUT);
digitalWrite(stoPin, LOW); //If Y dimension motor is prioritized, recieve all information from other motor.
}
if (steps[1] == 'd')
stepDown(pins, comPin, stoPin, steps, 500);
They successfully execute, as expected. However, in the stepDown function, the exact same if statement "if (steps[0] == 'z')" is suggested in line 61, yet it never executes. It can't be a serial issue, because the first if statement works as intended. But since the exact same string is being read, I've got no clue what's going on here. After some further testing, it appears that the string that enters stepDown() is an empty string, which makes no sense to me. I thought it might be taking the initialized version of the steps string, but i tried changing its initialization to an actual word and it gave the same result. The actual string that's being processed is around 1200 characters long, which might have an effect? I'm just grasping at straws at this point.