Joystick: problem with analog pins

Hi,

I’m having some problems programming the analog pins of my arduino uno, in order to use a joystick.
This is the joystick I’m using:
http://uk.farnell.com/apem/100113/joystick-microswitch-5a-250v-solder/dp/9895744

datasheet:

My goal is to control several motors, and because of that I need all the digital pins available.
This is the code I made using the digital pins (and it was working):

void setup() {
  
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  Serial.begin(9600);

}

void loop(){

  if((!digitalRead(2)) && (!digitalRead(3))){
    front_right();
  }
  
  else if((!digitalRead(2)) && (!digitalRead(4))){
    front_left();
  }

  else if((!digitalRead(5)) && (!digitalRead(4))){
    back_left();
  }
  
  else if((!digitalRead(5)) && (!digitalRead(3))){
    back_right();
  }
  
  else if(!digitalRead(2)){
    front();
    Serial.println("FRONT");
  }
  else if(!digitalRead(3)) {
    right();
    Serial.println("RIGHT");
  }
  else if(!digitalRead(4)) {
    left();
    Serial.println("LEFT");
  }
  else if(!digitalRead(5)) {
    back();
    Serial.println("BACK");
  }
}

but now that I’m trying to use analog pins, for some reason the arduino serial monitor instead of showing me continually the values I’m printing, it shows 2 or 3 times “FRONT FRONT FRONT”, and then it disappears…

 // Analog pins Code
void setup() {

  Serial.begin(9600);

}

void loop(){

  int actuator1 = A1;
  int actuator2 = A2;
  int actuator3 = A3;
  int actuator4 = A4;
  
  int sensor1 = 0;
  int sensor2 = 0;
  int sensor3 = 0;
  int sensor4 = 0;

  sensor1 = analogRead(actuator1);
  sensor2 = analogRead(actuator2);
  sensor3 = analogRead(actuator3);
  sensor4 = analogRead(actuator4);
  
  Serial.println(actuator1);
  Serial.println(actuator2);
  Serial.println(actuator3);
  Serial.println(actuator4);
  
  delay(500);
  
  if(sensor1>900 && sensor2 <900 && sensor3<900 && sensor4<900)
    Serial.println("FRONT");
  if(sensor1<900 && sensor2>900 && sensor3<900 && sensor4<900)
    Serial.println("RIGHT");
  if(sensor1<900 && sensor2 <900 && sensor3>900 && sensor4<900)
    Serial.println("BACK");
  if(sensor1<900 && sensor2 <900 && sensor3<900 && sensor4>900)
    Serial.println("LEFT"); 

}

Am I missing something?

Thanks in advanced.

You can use the analog pins as digital pins. Use pinMode(A0, INPUT_PULLUP); to set analog input 0 to an input with pullup enabled. Then use digitalRead() for the pins set to input. edit: Link http://arduino.cc/en/Tutorial/AnalogInputPins

Thanks for the quick reply! I read the link you attached.

However, it only enters in the first condition, and it’s showing “FRONT” nonstop…
I think I’ve done what was described on the tutorial

void setup() {

 // initialize the pushbutton pin as an input:
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);

  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  digitalWrite(A4, HIGH);

  Serial.begin(9600);

}

void loop(){

  if(digitalRead(A1)){
    Serial.println("FRONT");}
  else if(digitalRead(A2)){
    Serial.println("RIGHT"); }
  else if(digitalRead(A3)){
    Serial.println("BACK");}
  else if(digitalRead(A4)){
    Serial.println("LEFT");}

}
pinMode(A1, OUTPUT);

Needs to be pinMode(A1,INPUT); or pinMode(INPUT_PULLUP); That tutorial is kind of old. The old way to enable the internal pullups was to set the pin INPUT and then digitalWrite(Ax, HIGH); to enable pullups. The newer IDE enables pullups with just the one statement. How are the switches wired. The switch needs to go from ground to the input, to use internal pullups, and will go LOW when pressed.

Like you said, I changed the code to just one statement.

void setup() {

 // initialize the pushbutton pin as an input:
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);

  Serial.begin(9600);

}

void loop(){

  if(digitalRead(A1)){
    Serial.println("FRONT");}
  else if(digitalRead(A2)){
    Serial.println("RIGHT"); }
  else if(digitalRead(A3)){
    Serial.println("BACK");}
  else if(digitalRead(A4)){
    Serial.println("LEFT");}

}

It shows the right direction, every time I change it. However, when I change to one of the four directions, the serial monitor shows each one repeated more or less 50 times and after that it waits for another command.. For instance, when I press FRONT and then LEFT: (I press FRONT) FRONT FRONT FRONT x47 (I press LEFT) LEFT LEFT LEFT x47

The switch is wired from 5V to the input, because when it's wired from ground it doesn't show anything. It must be a code problem..

You can not "just" wire a button from 5V to the Arduino (or GND for that matter) Look at http://www.pighixxx.com/abc-arduino-basic-connections/ and related subpages.

Logan20: The switch is wired from 5V to the input, because when it's wired from ground it doesn't show anything. It must be a code problem..

Previously you were enabling the internal pull-up resistors with this code:

  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  digitalWrite(A4, HIGH);

You need to either enable the internal pull-up resistors, or add external pull-up or pull-down resistors. Enabling the internal ones seems the most sensible approach to me.

When you have pull-up resistors the input normally reads HIGH, and reads LOW when the switch is closed. In order to detect that, your code would need to test for the input being LOW:

  if(digitalRead(A1) == LOW)
  {
    Serial.println("FRONT");
  }
  else if ...

The "inverse" reading (when you wire the button so it goes to GND when pressed) can be confusing so I do

#define PRESSED LOW
#define RELEASED HIGH

Then in the code I can do "clear" test like

if ( digitalRead(Button1) == PRESSED ) ...

Some servo/pot test code setup so the pot values are easier to see via the serial monitor. You don’t need servos attached to test the pot outputs.

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}

It's working, Thanks for all your help!

You need to either enable the internal pull-up resistors, or add external pull-up or pull-down resistors. Enabling the internal ones seems the most sensible approach to me.

PeterH this tip helped me to understand what was happening ;)

The "inverse" reading (when you wire the button so it goes to GND when pressed) can be confusing so I do Code:

define PRESSED LOW

define RELEASED HIGH

Yes, like that it's much more easier to understand!

Some servo/pot test code setup so the pot values are easier to see via the serial monitor. You don't need servos attached to test the pot outputs.

I didn't know that library 'Servo.h', but since I don't have any simulator that's going to be useful. Thanks!