Go Down

Topic: Joystick 2 axis Arduino help (Read 131 times) previous topic - next topic

sam87

I currently use 2 push buttons to drive an actuator up and down horizontally, and would like to use a 2-axis Joystick (Thumb-stick) with my actuator but I cant make it just go up and down using this, I have my codes below, I only want to use the up and down feature of the joystick but so far I could do up and left for it to work since I can assign x and y but how do I assign just 1 x input to multiple function (Up and down) for my actuator?



Code: [Select]
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 1;     // the number of the pushbutton1 pin (Down)
const int button2Pin = 2;     // the number of the pushbutton2 pin (Up)
const int RELAY1 = 6;      // Actuator Retracting
const int RELAY2 = 7;      // Actuator Extending

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status

const int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor


// initialize the pushbutton pin as an input:

// initialize the relay pin as an output:


const int RELAY3 = 4;      // Bottle Opener reverse
const int RELAY4 = 5;      // Bottle Opener opening

void setup() {

 //start serial connection
 Serial.begin(9600);


 // Initialise the Arduino data pins for OUTPUT
 pinMode(button1Pin, INPUT);
 pinMode(button2Pin, INPUT);
 pinMode(RELAY3, OUTPUT);
 pinMode(RELAY4, OUTPUT);
 pinMode(RELAY1, OUTPUT);
 pinMode(RELAY2, OUTPUT);


 digitalWrite(RELAY1, HIGH);
 delay(3000);
 digitalWrite(RELAY1, LOW);
 delay(1000);
 digitalWrite(RELAY3, LOW);
 delay(1000);
 digitalWrite(RELAY1, LOW);
 delay(1000);
 digitalWrite(RELAY2, HIGH);
 delay(1500);
 digitalWrite(RELAY2, LOW);
 delay(5000);
 digitalWrite(RELAY4, HIGH);
 delay(5500);
 digitalWrite(RELAY4, LOW);
 // Wait 5 seconds to retract + 2 + & - 2 -
 // Turns OFF Relays 2
 delay(5500);
 digitalWrite(RELAY3, HIGH);
 delay(3000);
 digitalWrite(RELAY3, LOW);
 delay(3000);
 
 
 digitalWrite(RELAY1, HIGH);         // Turns Relay On
 delay(2000);                          // Wait 45 seconds to retract + 2 + & - 2 -
 digitalWrite(RELAY2, LOW);
 delay(2000);
 digitalWrite(RELAY3, LOW);
 delay(2000);
 digitalWrite(RELAY4, LOW);          // Turns OFF Relays 2
 delay(2000);
 

}
void loop() {

 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 //print out the value of the pushbutton
 Serial.println(sensorValue);   
 
 // read the state of the pushbutton values:
 button1State = digitalRead(button1Pin);
 button2State = digitalRead(button2Pin);

 // check if the pushbutton1 is pressed.
 // if it is, the buttonState is HIGH:
 // we also ensure tha the other button is not pushed to avoid conflict
 if (button1State == HIGH && button2State == LOW) {     
   // turn relay1 on:   
   digitalWrite(RELAY1, HIGH);     
 }
 // When we let go of the button, turn off the relay
 else if (digitalRead(RELAY1) == HIGH) {
   // turn relay1 off:
   digitalWrite(RELAY1, LOW);
 }
 
 // repeat the same procedure for the second pushbutton
 if (button1State == LOW && button2State == HIGH) {     
   // turn relay2 on:   
   digitalWrite(RELAY2, HIGH);
 }
 // When we let go of the button, turn off the relay
 else if (digitalRead(RELAY2) == HIGH) {
   // turn relay2 off:
   digitalWrite(RELAY2, LOW);
 } 
}

MorganS

So what values did you get from from analogRead()? You should find that the middle position of the joystick returns a value close to 512. Maybe it gives 499. But it bounces around a lot. It might vary from 450 to 550 even without moving the stick. So you need a "dead zone" or "threshold" built into your calculations.

Code: [Select]

  //set up threshold values for joystick sensor
  const int joyMiddle = 499;
  const int joyThreshold = 51;

 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 //print out the value of the pushbutton
 Serial.println(sensorValue);   
 
 // read the state of the pushbutton values:
 button1State = digitalRead(button1Pin);
 button2State = digitalRead(button2Pin);

 // check if the pushbutton1 is pressed.
 // if it is, the buttonState is HIGH:
 // we also ensure tha the other button is not pushed to avoid conflict
 if ((button1State == HIGH && button2State == LOW) || sensorValue < joyMiddle-JoyThreshold) {     
   // turn relay1 on:   
   digitalWrite(RELAY1, HIGH);     
 }
"The problem is in the code you didn't post."

sam87

Thank you, I get an error message Arduino: 1.8.4 (Mac OS X), Board: "Arduino/Genuino Uno"


Code: [Select]
JoyStick:7: error: 'sensorValue' does not name a type
 sensorValue = analogRead(sensorPin);
 ^
JoyStick:11: error: 'Serial' does not name a type
  Serial.println(sensorValue);   
  ^
/Users/Soorena/Desktop/Arduino Practice/JoyStick/JoyStick.ino: In function 'void setup()':
JoyStick:14: error: 'button1State' was not declared in this scope
  button1State = digitalRead(button1Pin);
  ^
JoyStick:14: error: 'button1Pin' was not declared in this scope
  button1State = digitalRead(button1Pin);
                             ^
JoyStick:15: error: 'button2State' was not declared in this scope
  button2State = digitalRead(button2Pin);
  ^
JoyStick:15: error: 'button2Pin' was not declared in this scope
  button2State = digitalRead(button2Pin);
                             ^
JoyStick:20: error: 'sensorValue' was not declared in this scope
  if ((button1State == HIGH && button2State == LOW) || sensorValue < joyMiddle-JoyThreshold) {     
                                                       ^
JoyStick:20: error: 'JoyThreshold' was not declared in this scope
  if ((button1State == HIGH && button2State == LOW) || sensorValue < joyMiddle-JoyThreshold) {     
                                                                               ^
JoyStick:22: error: 'RELAY1' was not declared in this scope
    digitalWrite(RELAY1, HIGH);     
                 ^
JoyStick:25: error: a function-definition is not allowed here before '{' token
 void loop() {
             ^
JoyStick:28: error: expected '}' at end of input
 }
 ^
exit status 1
'sensorValue' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

sam87

I get 520 for middle point, 0 and 1023 in the monitor.

accuracy is not an issue for me, I only want to be able to control the actuator using an up and down keys on the joystick. how do I wire it/Program it.

outsider

#4
Dec 07, 2017, 01:28 am Last Edit: Dec 07, 2017, 01:32 am by outsider
Try:
Code: [Select]
if(analogRead(A0) > 620)
  button1State = 1;
else
  button1State = 0;

if(analogRead(A0) < 420)
  button2State = 1;
else
  button2State = 0;

Need a link to your Joystick to tell how to wire it.

MorganS

That's a terrible example:

1. Magic numbers. What does 620 mean? Does it get repeated elsewhere in the program? If I change it here do I have to hunt down and change the others or are they actually going to stay the same?

2. You read the input twice. It changes between those two reads. What if it changes enough to be true for both? Read all the inputs only once per calculation cycle.

3. You hardcoded it to A0. What if the layout means it has to change to another pin? Like a magic number, you have to hunt them down and change them all.

4. Usually 1 and 0 get a pass from the magic number rule. Not in this case. What's wrong with true and false? Maybe there should be other constants defined like BUTTON_PRESSED?

5. Multi-line if() statements without braces. Adding a single Serial.print() in the middle will totally bork the logic.

Sam, please show the complete code which generated those error messages. It looks like something simple is missing, like a {, but we can't diagnose it without seeing the code.
"The problem is in the code you didn't post."

Go Up