Arduino, stepper motor, and

Hi!

I'm super new to Arduino, but I'm hopping in on a project and need to work with someone else's code, and learn quickly! Please help:)

So I have two joysticks that connect to stepper motors that control an object.
The problem I'm having, is that the object is moving even when the joysticks aren't being touched.
I'm trying to solve this by testing when the joysticks are actually being pushed, and if they are, then go ahead and do steps, etc.

Current code:

//CONTROLS THE JOYSTICKS, global variables
#define Xpin1 A0      // Pin A0 connected to Joystick 1 x-axis
#define Ypin1 A1      // Pin A1 connected to Joystick 1 y-axis
#define button_1 38     // Pin 38 connected to Joystick Button 1

#define Xpin2 A3      // Pin A2 connected to Joystick 2 x-axis
#define Ypin2 A2      // Pin A3 connected to Joystick 2 y-axis
#define button_2 39     // Pin 39 connected to Joystick Button 2


void setup() {
   Serial.begin(9600);
   pinMode(button_1, INPUT_PULLUP);
   pinMode(button_2, INPUT_PULLUP);

   //This was never implemented but unsure if should be?
   //pinMode(Xpin1, INPUT);
   //pinMode(Xpin2, INPUT);
   //pinMode(Ypin1, INPUT);
   //pinMode(Ypin2, INPUT);
....
...
...
}


void loop() {
....
....
..
  if(switch1) {
    ///THE IF STATEMENT COUNTER==25 used to be the if statement in the code, however that was causing 
        ///the problem, bc the counter increases no matter if the joysticks are touched or not.
    //if (counter == 25)
   //BELOW is my thoughts, except this doesn't work either. 
    if (digitalRead(Xpin1) || digitalRead(Xpin2)
                          || digitalRead(Ypin1) || digitalRead(Ypin2)) { 
      set_bottom(scale(analogRead(X_pin_1) - 512.0), scale(analogRead(Y_pin_1) - 512.0));
      set_top();
    } 
  }

....
......


}

My logic for using the current if statement:
I don't know how to view if the joysticks are being pushed or not. When I do analogRead I get varying numbers. I assume that when the joysticks aren't being touched but the robot is on, then the x and y axis positions would be on low (from digital reading). However this isn't the case, I think one of the Y axis is on high, and only are all of them on low when I move one of the joysticks in a specific direction.

Can someone please enlighten me on whats going on?

Thanks

In the arduino 'IDE' user interface program...... for which you work on your 'sketch file', there is a menu option that allows you to view the 'serial monitor'.

If you include in your code some instructions to print some values, then you will be able monitor the joystick readings.

Before the setup() area, add something like:

int xval1;

Eg ... in your loop of your code .... put something like:

xval1 = analogRead(Xpin1);
Serial.print(xval1);Serial.println();

Hi!
I'm aware of Serial.println().

This is how I discovered that when I don't touch the joysticks, one of the y axis for a joystick is on HIGH when the others are on low.

I'm wondering, what am I expecting to see when I do analogRead on Xpin1 or Ypin1 when the joystick isn't moving?
I figured I would use digitalRead in the if statement (and also print that out) b/c the pins would all be on low unless actually activated/touching the joystick.

figured I would use digitalRead in the if statement (and also print that out) b/c the pins would all be on low unless actually activated/touching the joystick.

No.

You have two sorts of joystick, analog, sometimes called proportional, or digital.

Proportional ones delver a continuous changing voltage as they are moved and most have springs to return them to the centre when not being pushed. This will read arround 512 in this center position.

A digital joystick has two signals per axis and a common connector. It is normal to connect the common to ground and have the two signals connected to two inputs with their internal pull up resistors enabled. With no movement both signals will read high. Then a push in one direction will result in one input reading high and the other low. When pushed in the other direction both signals change state, that is swap over.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

It sounds like you have been thrown in the deep end, and looking at how much code is written, can I suggest that you dump it and write your own.
It may be easier for you to undersatnd and quicker in the long term.

Initially just code that sends your joystick data to the monitor to ensure you have valid and stable readings.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need to know what your hardware is?
Please post links/attachments to your hardware including joysticks, stepper motors and stepper drivers.
Pictures are worth a thousand words.

Thanks.. Tom... :slight_smile:

TomGeorge:
Pictures are worth a thousand words.

Except for F**ing diagrams, which are worth negative words. Every single one requires additional words like "This is not the motor I actually have." Please don't show pictures of components you don't have and don't intend to buy.

MorganS:
... "F**ing" ...

HAHA!! Love it!

Grumpy_Mike:
No.

You have two sorts of joystick, analog, sometimes called proportional, or digital.

Proportional ones deliver a continuous changing voltage as they are moved and most have springs to return them to the centre when not being pushed. This will read around 512 in this center position.

A digital joystick has two signals per axis and a common connector. It is normal to connect the common to ground and have the two signals connected to two inputs with their internal pull up resistors enabled. With no movement both signals will read high. Then a push in one direction will result in one input reading high and the other low. When pushed in the other direction both signals change state, that is swap over.

Hi. Thank you! So from this, I understand that I'm using an analog joystick. My question now, is what would cause the analogRead(xpin) to not be 512, when the joystick isn't being touched? I'm not at the robot right now, but I remember that it kept reading between 480 and 528 (about that), when the joysticks were not being touched. Or, if the analogRead will return a number around 512, then how am I supposed to test that when it's touched?

MorganS:
Except for F**ing diagrams, which are worth negative words. Every single one requires additional words like "This is not the motor I actually have." Please don't show pictures of components you don't have and don't intend to buy.

ChrisTenone:
HAHA!! Love it!

While it is "cool" it totally covers up what he is talking about. Now the OP thinks that was a curse word and thinks he had better make a really good picture. Oh what is the name of that program that people like to use to make those pretty pictures. Oh yeah Fritzing. And then they use it because you got cute and tried to pretend it was a bad word and they didn't understand you.

So if you just want to be cute and hate on Fritzing then keep at it. If you really want people to stop using it then stop writing it that way or at least write it out so people know what the f**k you are talking about.

Delta_G:
While it is "cool" it totally covers up what he is talking about.

I also had to go back and think about wtf that was about, after all there's nothing wrong with a proper diagram. Just not Fritzing... even the "schematic view" is pretty horrible.

Delta_G:
While it is "cool" it totally covers up what he is talking about. Now the OP thinks that was a curse word and thinks he had better make a really good picture. Oh what is the name of that program that people like to use to make those pretty pictures. Oh yeah Fritzing. And then they use it because you got cute and tried to pretend it was a bad word and they didn't understand you.

So if you just want to be cute and hate on Fritzing then keep at it. If you really want people to stop using it then stop writing it that way or at least write it out so people know what the f**k you are talking about.

I actually don't mind Fritzing. It's got good parts and it's got parts that aren't so good. The arduino.cc site uses it throughout the reference and tutorial section. But look, you've got to throw a bit of humor when you can. This place does have a bit of a humorless rep (at least with new folk who are looking for a bit of gentle help.) The joke was actually on us. Your post helped explain the joke to the uninitiated, so thanks for that.

ssmhoole:
Hi. Thank you! So from this, I understand that I'm using an analog joystick. My question now, is what would cause the analogRead(xpin) to not be 512, when the joystick isn't being touched? I'm not at the robot right now, but I remember that it kept reading between 480 and 528 (about that), when the joysticks were not being touched. Or, if the analogRead will return a number around 512, then how am I supposed to test that when it's touched?

The centre position being a constant 512 is not really possible, the joysticks are a mechanical device and not knowing what yours are like, probably small and built as cheap as possible.
To detect if the joystick has been moved from a central position you use hysteresis.
If the joystick is central, then any reading say less than 20 either side of 512 is regarded as zero, when the value moves outside the limits them the joystick output is then used as a moving value.

We need to see a copy of your circuit to see how you have configured your project and also what you are using as a power supply and stepper controller.
What are you stepper motors?

Have you googled arduino joystick stepper control

Thanks.. Tom... :slight_smile:

ChrisTenone:
Your post helped explain the joke to the uninitiated, so thanks for that.

Right. So the question becomes, do we want to keep the joke funny OR do we want to discourage use of fritzing?

@Chris:
I think fritzing is awesome when used by someone who knows how and has the patience to do it right. But what we get here is some noob trying to put it together as fast as they can with what parts they see right there and only one color of wire and what we see is a mess with parts that aren't really the parts the OP has etc. I get why people here get annoyed with it. But calling it F**ing doesn't help that. It's just good for a laugh from the folks on the inside.

It seems the OP has started again over here:-
https://forum.arduino.cc/index.php?topic=554872.msg3783908#msg3783908