0

I'm trying to use an Arduino Uno to read values from a 2-axis joystick analogue input, and alter these values. Rather than the true joystick values, I arithmetically convert the values at neutral to range between 0 - 5, with 'neutral', i.e. when the joystick isn't being pushed, being at roughly 2.5 for each axis. These joystick values currently output to a live text file, to be graphed in real-time. One function I'm trying to implement is that when the joystick value is greater or less than a defined value (e.g. in the code, greater than 4.55, or less than 0.5) that the Arduino sets that point to be the new 'neutral' position (i.e. 2.5), however my code at the moment isn't working and I'm not sure how to go about attempting this.

Any insight would be most appreciated!

Cheers

const int buttonPin = 2; 
const int successPin = 13; 

int buttonState = 0;

void setup() 
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode (buttonPin, INPUT);
pinMode (successPin, OUTPUT);
while (digitalRead(buttonPin) == LOW) {};
}


void loop () {

int sensorValueY = analogRead(A0);
int sensorValueX = analogRead(A1);


//arithmetic operation that maps output values from 0 - 5 instead of 0 - 1023
float voltageX = sensorValueX * (5.0 / 1023.0);
float voltageY = sensorValueY * (5.0 / 1023.0);
Serial.print(voltageX);
Serial.print(" , ");
Serial.print(voltageY);

if (voltageX >= 4.55) {
  voltageX == 2.5;
  successPin==HIGH;
  delay(1);
  successPin==LOW;
}

if (voltageX <= 0.5) {
  voltageX ==2.5;
  successPin==HIGH;
  delay(1);
  successPin==LOW;
}

if (voltageY >=4.55) {
  voltageY == 2.5;
  successPin==HIGH;
  delay(1);
  successPin==LOW;
}

if (voltageY <= 0.5) {
  voltageY ==2.5;
  successPin==HIGH;
  delay(1);
  successPin==LOW;
}
Serial.println();

}
  • Sorry, I read your post and the comment under @Nick Gammon's post but I couldn't understand what you are trying to achieve. Can you give some examples, for instance "i move the joystick all the way to the left: now it reads 0.1. I set this as new idle...." and so on? Otherwise it becomes very difficult to understand your needs.. – frarugi87 Jan 19 '16 at 17:30
  • Okay, so when the joystick moves all the way to the left, the analogue input value drops to 0, say. At neutral the value is roughly 2.5. What I want is the Arduino to then set the value of 0, when fully left, to 2.5, such that when the joystick physically returns to neutral, the value would read 5. Same would go for all the way up, for example, where the value would normally read 5. At near 5, I want the joystick to set to 2.5 (its starting value), such that when it physically returns to neutral i.e. not pushed, the value would then read 0. (Hope this makes a bit more sense!) – mantistoboggan Jan 19 '16 at 17:36
  • Ok... Now I understood ;) I can't understand the reason to do so though ;) – frarugi87 Jan 19 '16 at 17:38
  • Anyway... When the joystick returns to neutral it'll be 0 again, so it will go back to 2.5 again... – frarugi87 Jan 19 '16 at 17:38
1
if (voltageX >= 4.55) {
  voltageX == 2.5;
  successPin==HIGH;

The last two lines (and all similar ones later) are wrong. == is for comparing, = is for assigning. So change it to:

if (voltageX >= 4.55) {
  voltageX = 2.5;
  successPin = HIGH;

You did that in a lot of places, so fix that up and get back to us.


const int successPin = 13; 
...
  successPin = HIGH;

Assuming you fix the compare to an assign you will get an error message here. You define successPin as 13, and then try to change it to 1 (which is what HIGH is defined as). Do you mean, to write to that pin? Like this?

digitalWrite (successPin, HIGH);  

You will need to change all those as well. And if you are hoping to see an LED on the pin flash, 1 ms is quite a small interval.

  • For lots of the LEDs on Arduino boards, 1 ms is long enough to see an LED flash; for that matter, a few microseconds is long enough. – James Waldby - jwpat7 Jan 19 '16 at 0:14
  • I didn't say he wouldn't see it flash. I warned him the flash would be short. :) – Nick Gammon Jan 19 '16 at 1:57
  • Thanks so much for the input! The issue I now have is that although the Arduino sets >4.55 and <0.5 to be the new 'neutral' point of 2.5, when the joystick physically returns to neutral, i.e. when the values are under the defined thresholds, then rather than going from 2.5 to 0, say, the values go from 2.5, back to 4.54 and then back to 2.5 at physical neutral again. What I'm trying to have is that once set to the new threshold of 2.5, that value stays set, such that when the joystick physically returns to neutral it's idling somewhere around 0, relative to the new threshold. Cheers! – mantistoboggan Jan 19 '16 at 14:22
  • It seems to me that you want the map function (or write something similar yourself). You want, as far as I can see, to unconditionally map the joystick input to a new range. – Nick Gammon Jan 19 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.