I am starting with Arduino and I am stuck with a - probably simple - thing.
Situation: I try to control the a stepper motor via a Python Script on the Raspi. The Raspi communicates with a uStepper (Arduino Board) to control the forward and backward spinning of the stepper motor.
Problem: As soon as I connect the stepper motor to the power source, it starts executing the loop and moving.
However, it should only initiate the loop when being triggered by the Python script, via the GPIO. Once I start the python script, the stepper motor stops moving, and the function works well. I guess this has to do with high/low signals, but cant figure it out.
The code which I currently have:
#include <uStepperS.h>
#define MAXACCELERATION 50000 //Max acceleration = 50000 Steps/s^2
int buttonPin1 = 2; // the pin that the pushbutton is attached to
int buttonPin2 = 3; // the pin that the pushbutton is attached to
int buttonPin3 = 4; // the pin that the pushbutton is attached to
int buttonState1 = digitalRead(buttonPin1); // current state of the button
int buttonState3 = digitalRead(buttonPin3); // current state of the button
uStepperS stepper;
void setup()
{
Serial.begin(9600);
stepper.setup();
stepper.setMaxVelocity(5000);
// initialize the button pin as a input:
pinMode(buttonPin1, INPUT_PULLUP);
//pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
void loop()
{
if (buttonState1 = digitalRead(buttonPin1))
{
if (buttonState1 == HIGH){
stepper.moveSteps (-400000);
delay( 4000);
}
else {
stepper.stop();}
}
if (buttonState3 = digitalRead(buttonPin3))
{
if (buttonState3 == HIGH){
stepper.moveSteps (400000);
delay( 4000);
}
else{
stepper.stop();}}
}
Please take a look at the first topic telling how to get the best from this forum.
Use autoformat in the IDE and code tags, </>, when pasting.
A wiring diagram would also be appreciated.
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whetherx is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.
Thanks for your advice. I put the "buttonState1 == LOW" and then the movement stops.
The issue I face then is that when the python script runs, and triggers then communication with the Arduino board, nothing happens. (in the python script I put the GPIO to HIGH to trigger the Arduino)
Agree, and I understand that concept now after playing around with it today and based on your feedback. I am just thinking how to get it to stop moving. My thinking now is to change the wiring.
When I put the "buttonState = 0 " it stops moving, but its also not activated when I run the python script
I just came up with a "hack" as well. I just start the power to the Arduino only when the python script runs. This way the pin gets the right input from the Raspi. This is controlled with a relais via the python script. Maybe not pretty, but definitely "plan B"
@MarkT I didn't know the levelshifter made a difference in the setup, thats why i didn't draw it in the beginning.
Even when I have both ends powered up, the stepper motor is moving. Only when I start the python script on the raspberry, (and change the GPIO to LOW), the motor stops. Based on that I wonder if the two power sources make a difference.