Hi! I'm fairly new to Arduino and programming in general (this is my first project) and even though I'm pretty proud of myself so far, I need some help!
I'm trying to create a dynamic video mapping installation. Dynamic in the sense, that the object which I'm gonna project on is rotating. To achieve this rotation I build a little display turntable using a Nema 17 stepper motor, an Arduino mega 2560 and an Easy Driver stepper motor driver by Cylewet hooked up to a 9V power source.
The setup generally works, meaning with a simple code I can make the turntable turn at different velocities and both clockwise and anti-clockwise.
Right now I'm trying to write a code, which would make the Arduino wait for a serial input (in this case the Key '1'), once triggered would make the motor turn for 1 rotation using 50 seconds and then stop and wait for the next trigger.
This is what I have got so far:
// X = delay to make 1 rotation last 50 secs.
#define X1 7812
#define X2 7813
long rotationCount = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW); //LOW = Enabled, HIGH = Disabled
digitalWrite(8, HIGH);
digitalWrite(13, LOW);
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0);
int val = (Serial.read() - '0');
if (val == 1 && rotationCount < 50000000) { // 50'000'000 = 1 Rotation in 50 secs
Serial.println("Rotation Mode: ON");
digitalWrite(13, HIGH);
rotation();
}
if (rotationCount >= 50000000) {
rotationCount = 0;
Serial.println("Rotation Mode: OFF");
digitalWrite(13, LOW);
}
}
void rotation() {
digitalWrite(9, HIGH);
delayMicroseconds(X1);
digitalWrite(9, LOW);
delayMicroseconds(X2);
rotationCount = rotationCount + 15625; // X1 + X2
}
When I run this code and I send the trigger to the Arduino, I get the correct Message in my console (Rotation Mode: ON), however, the stepper motor isn't turning. When I slightly modify the code just to eliminate the serial trigger and the if statement, the motor turns fine and at (what seems to be) the correct speed.
Modified code:
// X = delay to make 1 rotation last 50 secs.
#define X1 7812
#define X2 7813
long rotationCount = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW); //LOW = Enabled, HIGH = Disabled
digitalWrite(8, HIGH);
digitalWrite(13, LOW);
Serial.begin(9600);
}
void loop() {
//while (Serial.available() == 0);
//int val = (Serial.read() - '0');
//if (val == 1 && rotationCount < 50000000) { // 50'000'000 = 1 Rotation in 50 secs
if (rotationCount < 50000000) {
//Serial.println("Rotation Mode: ON");
//digitalWrite(13, HIGH);
rotation();
}
if (rotationCount >= 50000000) {
rotationCount = 0;
//Serial.println("Rotation Mode: OFF");
//digitalWrite(13, LOW);
}
}
void rotation() {
digitalWrite(9, HIGH);
delayMicroseconds(X1);
digitalWrite(9, LOW);
delayMicroseconds(X2);
rotationCount = rotationCount + 15625; // X1 + X2
}
Since both actions (serial communication & rotation) seem to be working on there own, I suspect they must be interfering with each other, but I don't know where or how I could fix it...
I hope someone has a better approach to this than me...
Thank you in advance for the help!