arnix:
If this will not work i will create account on YT.
It does not work for me - and I don't have high bandwith - YouTube usually works perfectly.
...R
arnix:
If this will not work i will create account on YT.
It does not work for me - and I don't have high bandwith - YouTube usually works perfectly.
...R
EDITED: OK, now i am confused. If i change the code with
RunMode = ! digitalRead(ButtonPin);
i get no compiler error but the program is stuck and the motor is not spinning at all.
If i set it back, then you get what you can see in video.
//----------------------------------------------
The video is showing this:
Stop for few seconds
Fast back - forth motion
repeat
Problems that i have are in the speed of the back/forth spin and button press.
Button should trigger back / forth motion that you see in the video.
Code:
I agree we must define one other approach for solving this problem and code update is necessary.
LED:
To be honest i dont know from where this comes from. There is no defined output to pin 13 in the code that i am using ( the code from forum ). So i have no idea where this comes from.
Robin has mentioned that there is a problem with delay time. Maybe this is the first place to look at...
@Robin2
:-). Vid.me loads and saves files faster then YT so i dont know why you can not see this.
It has definitely nothing to do with bandwidth... If you can not make it work i can make one YT account. But like i wrote, this must work ( Mike is a confirmation
)
Code that am using ( it's the same code as before with ! change ):
int IncrementPin = 9;
int DirectionPin = 8;
int ButtonPin = 2;
int buttonState = 0;
int CurrentPosition;
int Xposition = 0; //may need to swap these if it's backward
int Yposition = 800;
void setup() {
pinMode(IncrementPin, OUTPUT);
pinMode(DirectionPin, OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP);
digitalWrite(IncrementPin, LOW);
digitalWrite(DirectionPin, LOW);
Serial.begin(9600);
}
void loop()
{
static boolean RunMode = false;
if (!RunMode) { //RunMode is false until the button is pressed, then doesn't get checked anymore
//latching RunMode on
RunMode = ! digitalRead(ButtonPin);
Serial.println("Button not pressed");
}
else {
Serial.println("Button pressed");
delay(3000);
MoveMotor(Yposition);
delay(1000);
MoveMotor(Xposition);
delay(3000);
}
}
void MoveMotor(int NewPosition) {
boolean Forward = (NewPosition > CurrentPosition); //sets the direction variable
int AddValue; //How much we add to the current position... it will only be 1 or -1
if (Forward) { // perhaps this is backward and should be NOT forward (!Forward)
digitalWrite(DirectionPin, HIGH);
AddValue = 1;
}
else {
digitalWrite(DirectionPin, LOW);
AddValue = -1;
}
while (CurrentPosition != NewPosition) {
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
delayMicroseconds(400);
CurrentPosition += AddValue; // record this step
}
}
arnix:
EDITED: OK, now i am confused. If i change the code with
RunMode = ! digitalRead(ButtonPin);
i get no compiler error but the program is stuck and the motor is not spinning at all.
If i set it back, then you get what you can see in video.
What messages are you getting from your Serial prints ?
Maybe your code just needs to be
RunMode = digitalRead(ButtonPin);
have you tried that
...R
I think the sense of the RunMode is correct.
The constants at the top define an IncrementPin but that is never used in the code. That will cause problems later if it is not addressed.
The motor driving code sends EXTREMELY SHORT pulses to the driver board with 400us between pulses. Is this within specification for that driver?
MorganS:
The motor driving code sends EXTREMELY SHORT pulses to the driver board with 400us between pulses. Is this within specification for that driver?
My A4988 drivers work fine with the pulse length provided by successive digitalWrite()s. It is a slow instruction. More importantly, can the motor can work that fast - especially without acceleration. But I raised that question in Reply #13 ...
...R
RunMode = ! digitalRead(ButtonPin);
i get no compiler error but the program is stuck and the motor is not spinning at all.
I told you that you were not testing things correctly.
If i set it back, then you get what you can see in video.
So having a stupid line of code just mitigates some other big error some where else. It is no reason to go with stupidity. Remove the stupidity and hunt down what is wrong with serial print statements.
Sorry guys, i could not reply sooner...
I made some test and changed a code a bit:
int IncrementPin = 9;
int DirectionPin = 8;
int CurrentPosition;
//-------------------------
int Xposition = 0;
int Yposition = 800;
//-------------------------
const int ButtonPin = 2; // pushbutton pin
int buttonState = 0; // pushbutton status
void setup() {
pinMode(IncrementPin, OUTPUT);
pinMode(DirectionPin, OUTPUT);
//-------------------------
pinMode(ButtonPin, INPUT_PULLUP);
//--------------------------
digitalWrite(IncrementPin, LOW);
digitalWrite(DirectionPin, LOW);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(ButtonPin);
Serial.println(buttonState);
if (buttonState == HIGH) {
Serial.println("ON");
}
else {
Serial.println("OFF");
/*
delay(3000);
MoveMotor(Yposition);
delay(1000);
MoveMotor(Xposition);
delay(3000);
*/
}
}
void MoveMotor(int NewPosition) {
boolean Forward = (NewPosition > CurrentPosition); //sets the direction variable
int AddValue; //How much we add to the current position... it will only be 1 or -1
if (Forward) { // perhaps this is backward and should be NOT forward (!Forward)
digitalWrite(DirectionPin, HIGH);
AddValue = 1;
}
else {
digitalWrite(DirectionPin, LOW);
AddValue = -1;
}
while (CurrentPosition != NewPosition) {
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
delayMicroseconds(400);
CurrentPosition += AddValue; // record this step
}
}
So. what we now have is this:
If i deactivate the MoveMotor part functions button is replaying correctly.
If i uncomment this part, button reacts randomly ( like if there is a bouncing problem ) and nothing happens when i get confirmation that the button was pressed ( i am planning to add this part ). According to this, button bouncing time in correlation with MoveMotor part is wrong.
Other problem might be what Robin and Mike pointed out.
I dont know...
This is the test when everything is activated.
The test is made bi constant clicking cca each second ( should be ON / OFF all the time ).
0
OFF
1
ON
1
ON
1
ON
1
ON
1
ON
1
ON
1
ON
1
ON
0
OFF
0
OFF
0
OFF
0
OFF
//-----------------------------
I added the back forth motion into the button HIGH state and the code is responding correctly to the button press. But you have to wait 0.5 second after the press, before motor will start to spin back / forth. If i press button few time in a row, only first press will be recognized. Shortly i have to wait cca 4 seconds (after the spin is over ) and after last button press. Hmm ?
Code:
...
void loop() {
buttonState = digitalRead(ButtonPin);
Serial.println(buttonState);
if (buttonState == HIGH) {
Serial.println("ON");
MoveMotor(Yposition);
delay(1000);
MoveMotor(Xposition);
//delay(3000);
}
else {
}
}
//------------------------
Now when we have working back / forth part , now we need to set the speed of this movement.
The best way would be to set one additional switch which will trigger forth spin.
I do have one more question. Would it be possible to make this back/forth movement super fast ? Like 0 sec / 0.5 / 0.5.... Something like pulsating .. something ![]()
//--------------------
Final update for today ![]()
I changed delay function with delayMicroseconds(); and speed can be regulated much better and faster and there's no 4 seconds delay. This calls for beer
!
Next thing is to implement second button.
When you don't include the motor code you have a loop() that repeats very quickly. Try slowing it down with a short delay(500); That will give you time to get your finger off the button.
But, as I said in Reply #13 you should not use the delay() function other than for simple tests.
I just realized I did not include the link to several things at a time in Reply #13 - sorry. It includes code for reading a button.
...R