I am new to coding, and could use some help. The problem is the if statement, and I just don't get whats wrong. Any help would be appreciated.
// defines pins
#define dirPin 10
#define stepPin 11
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
if (int i = 0; i <= 5; i++){
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 400 pulses for making one full cycle rotation
for(int x = 0; x < 1600; x++) {
digitalWrite(stepPin,HIGH);
// by changing the time delay between the steps below
// we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
delayMicroseconds(100);
digitalWrite(stepPin,LOW);
// Do not mess with this delay as it will throw things off.
delayMicroseconds(200);
}
exit(0);
}
}
I changed the if to a for and it solves the syntax problem, but after running a test the code does not work as I wanted it too. I wanted to make code inside repeat 5 times and it only does it once.
If @cyberman42 wants the code to run once, perhaps every time he hits reset, that statement just makes it stop when it has.
Of course usually stuff to be done once gets put in setup(), so @cyberman42 could move all those lines in loop() on up there.
If he is using the reset button as a "do that thing (again)" button, he can look forward to learning how to program so a button press would make the sequence repeat in a sketch that just ran and ran, that is to say not rely on the relatively crude technique of exploiting a full reset on the board.
I had to refresh my memory of what it does in this context!
I have removed the exit part as this appears not how you write a script like this - forgive my ignorance.
Below is the current code I am using and it still does not work the way I want. When I use this on the motor is keeps going around and never stops. I want to use the first "for" loop to make the motor rotate 5 full revolutions and stop. So how do I do this?
// defines pins
#define dirPin 10
#define stepPin 11
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
for (int i = 1; i <= 5; i++) {
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
// Makes 400 pulses for making one full cycle rotation
for (int x = 0; x < 1600; x++) {
digitalWrite(stepPin, HIGH);
// by changing the time delay between the steps below
// we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
// Do not mess with this delay as it will throw things off.
delayMicroseconds(200);
}
}
}
This does what you wrote in the previous post, but perhaps you will need to redefine your goal when you want to do more than stop the motor.
// defines pins
#define dirPin 10
#define stepPin 11
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
for (int i = 1; i <= 5; i++) {
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
// Makes 400 pulses for making one full cycle rotation
for (int x = 0; x < 1600; x++) {
digitalWrite(stepPin, HIGH);
// by changing the time delay between the steps below
// we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
// Do not mess with this delay as it will throw things off.
delayMicroseconds(200);
}
}
while(1); // this halts the code here, forevAr
}
Because, you have kept the codes in the loop function. Place the codes in the setup() function.
or
Connect a Button with the UNO, place the codes under a user-defined function, keep polling the Button in the loop() function for close condition and then call the function to rotate the motor for five full revolutions.
void loop()
{
if(Button is closed)
{
rotateMotor();
}
}
void rotateMotor()
{
for (int i = 1; i <= 5; i++)
{
digitalWrite(dirPin, HIGH); // Enables the motor to move in a par
for (int x = 0; x < 1600; x++)
{
digitalWrite(stepPin, HIGH);
// by changing the time delay between the steps below
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
// Do not mess with this delay as it will throw things off.
delayMicroseconds(200);
}
}
}
Check my maths. The process will take 2.4 seconds, so triggering it when the button is low will work, but @cyberman42 is advised to get his fat finger off the button or another 2.4 seconds worth of action will ensue.
The full solution would involve initiating the process only once when then button went down, not to be repeated until the button is again seen to be going from not-pressed to pressed.
So it exits/stops the execution of main(), which called loop()?
What happens then, on a microcontroller? It's not like there's an O/S to return to. Does some kind of halt op-code get executed, or does it go into an infinite loop?
Or in my experience at least. Ok, I know that delay(); isn't the recommended thing to use, but I really meant just some kind of waiting period, so the void loop() doesn't start over again immediately after a cycle.
It's like while(1);, or my favorite for(; ; );, except interrupts are disabled. TBH I am only quite sure, I want to see the code before being certain. It def stops the normal regular execution of the loop function.