How can i define the analog pin 2 and digital pin

Hello,

How can i define the analog pin 2 and digital pin 2 in the same sketch? I have a serial comunication defined on digital 2 and a sensor reading on analog 2. I can't use A2 because i'm using Antipasto 042 to program an Touch Shield Slide.

#define BatteryPin 0//Arduino analog pin where the Battery monitor is connected
#define Sensor1Pin 1//Arduino analog pin where the Sensor1 is connected
#define Sensor2Pin 2//Arduino analog pin where the Sensor2 is connected
#define Touch_TX_Pin 2//Arduino serial transmit data
#define Touch_RX_Pin 3//Arduino serial receive data

Thank you

Just go ahead and do it - all will be well

Well, the problem is that not everything is ok. As soon as i uncomment the Sensor2Pin there is no serial communication between arduino and touch shield slide, there is my problem

The analog pins are usually defined as D14, 15, 16, 17, 18, 19 for Analog0, 1,2,3,4,5. Are you using the analog pins as analog or digital inputs?

As analog inputs

I will give it a try using

#define BatteryPin 14//Arduino analog pin where the Battery monitor is connected
#define Sensor1Pin 15//Arduino analog pin where the Sensor1 is connected
#define Sensor2Pin 16//Arduino analog pin where the Sensor2 is connected

i’ll let you know if is working
I’m using Duemilenove + Touch Shield Slide + Antipasto 042 IDE

As soon as i uncomment the Sensor2Pin there is no serial communication

Are you saying that this#define Sensor2Pin 2//Arduino analog pin where the Sensor2 is connected is normally commented out, and uncommenting it causes serial comms to fail? What is the default value of Sensor2Pin if this line is commented out?

I think it is time to post your code.

#include<EEPROM.h>
#include<NewSoftSerial.h>
#define BatteryPin 0//Arduino analog pin where the Battery monitor is connected
#define Sensor1Pin 1//Arduino analog pin where the Sensor1 is connected
#define Sensor2Pin 2//Arduino analog pin where the Sensor2 is connected
#define Touch_TX_Pin 2//Arduino serial transmit data
#define Touch_RX_Pin 3//Arduino serial receive data
#define Sensor1PWR 6//Arduino analog pin where the Sensor1 is connected
#define Sensor2PWR 7//Arduino analog pin where the Sensor2 is connected
#define Shutter1 8//Arduino digital pin where the Device1 Shutter is connected
#define Focus1 9//Arduino digital pin where the Device1 Focus is connected
#define Shutter2 10//Arduino digital pin where the Device2 Shutter is connected
#define Focus2 11//Arduino digital pin where the Device2 Focus is connected

//Memory offsets into EEPROM
#define ROM_LCD_Bright 0//LCD Brightness 0-4
#define ROM_LCD_Screen 1//LCD Screen saver 0=Off/1=On
#define ROM_BTN_Delay 2//Button press time 0-99[ms]
#define ROM_Sequence 3//Sequence Off=0/On=1
#define ROM_SDelay 4//Sequence Delay 0-9999[ms] between Shutter1 and Shutter2 if ROM_Sequence=1
#define ROM_Bulb 5//Exposure in Bulb Mode 0-3599[s]
#define ROM_BulbProc 6//Processing time in Bulb Mode 0-7199[s]
#define ROM_TLH 7//Time lapse Hours interval [h]0-23
#define ROM_TLM 8//Timelapse Minutes interval[min]0-59
#define ROM_TLS 9//Timelapse Seconds interval[s]0-59
#define ROM_TLW 10//Timelapse WakeUp interval activate camera focus to get it out from StandBy [s]0-59
#define ROM_TLB 11//Timelapse Use Bulb 0=No/1=Yes(setting from bulb menu)
#define ROM_TLN 12//Timelapse Number of Frames 0-999
#define ROM_S1_Pow 13//Sensor1 power 0=Off/1=On
#define ROM_S1_TrigAt 14//Sensor1 triggerat0=Low/1=HighVAlue
#define ROM_S1_Threshold 15//Sensor1 trigger value0-1023
#define ROM_S2_Pow 16//Sensor2 power 0=Off/1=On
#define ROM_S2_TrigAt 17//Sensor2 trigger at 0=Low/1=HighValue
#define ROM_S2_Threshold 18//Sensor2 trigger value 0-1023
#define ROM_D1_TrigBy 19//Device1 is triggered by 0=None/1=Sensor1/2=Sensor2
#define ROM_D1_Delay 20//Delay between Device1 and his trigger [ms]0-9999
#define ROM_D1_Cycle 21//Device1 Period to wait until next picture[ms]0-9999
#define ROM_D2_Mode 22//Device2 0=None,1=Camera,2=Flash,3=Split
#define ROM_D2_TrigBy 23//Device2 is triggered by 0=None/1=Sensor1/2=Sensor2
#define ROM_D2_Delay 24//Delay between Device2 and his trigger [ms]0-9999
#define ROM_D2_Cycle 25//Device2 Period to wait until next picture[ms]0-9999
#define ROM_D2_SpitTime 26//Device2 Period to wait until next picture[ms]0-999
#define ROM_HDR_Exp 27//Exposure time in HDR mode[ms]0-9999
#define ROM_HDR_Inc 28//0-12 Size of increments array
#define ROM_HDR_Img 29//3-17No of frames in HDR Mode
#define ROM_SIZE 30
typedef struct SettingsVal
{
      unsigned int Value;//crtvalue
      unsigned int MaxVal;//maximum value for the setting
}TextVal;

NewSoftSerial TSSerial=NewSoftSerial(Touch_RX_Pin,Touch_TX_Pin);
SettingsVal ROM_VAL[ROM_SIZE];
unsigned int CrtProfile;
/////////////////////////////////////////////////////////
//Description: Runs once at boot time
//Parameters: None
//Returns: None
/////////////////////////////////////////////////////////
void setup()
{
      TSSerial.begin(38400);//Set comunication speed
      //pinMode(Touch_TX_Pin,OUTPUT);
      //pinMode(Touch_RX_Pin,INPUT);
      CrtProfile=RomRead(0);
      pinMode(BatteryPin,INPUT);
      pinMode(Sensor1Pin,INPUT);
      pinMode(Sensor2Pin,INPUT);//if i comment or change the Sensor2Pin the the serial communication will work
      pinMode(Sensor1PWR,OUTPUT);
      pinMode(Sensor2PWR,OUTPUT);
      pinMode(Shutter1,OUTPUT);
      pinMode(Focus1,OUTPUT);
      pinMode(Shutter2,OUTPUT);
      pinMode(Focus2,OUTPUT);
      digitalWrite(Shutter1,LOW);
      digitalWrite(Shutter2,LOW);
      digitalWrite(Focus1,LOW);
      digitalWrite(Focus2,LOW);
      digitalWrite(Sensor1PWR,LOW);
      digitalWrite(Sensor2PWR,LOW);
      /*Reset all EEPROM values*/
      //RomWrite(0,0);
      //ResetEEPROM(1+0*ROM_SIZE);
      //ResetEEPROM(1+1*ROM_SIZE);
      //ResetEEPROM(1+2*ROM_SIZE);
      LoadEEPROM();
      SetMaxVal();
      delay(500);
      TSSerial.print('Q');//Inform Touch Shield that Arduino is ready
}

Normally the sensor2pin should be uncommented because i need to read this pin value. I comment it because i need communication with the slide

pinMode(Sensor2Pin,INPUT);//if i comment or change the Sensor2Pin the the serial communication

I thought you said it was an analogue pin? Why would you be setting the pinMode for an analogue pin (which isn't what's happening here)?

You're setting the pin mode of the digital pin 2. You're doing this for all the analogue pins - don't do it!

/////////////////////////////////////////////////////////
//Description: Reads analog Pin 0/1 of arduino
//Parameters: Pin to read
//Returns: Arithmetic mean of 5 readings
/////////////////////////////////////////////////////////
void ReadSensor(byte Pin)
{
      unsigned int Values=0;
      for(byte i=0;i<5;i++)
      {
            Values+=analogRead(Pin);
      }
      SendInt((Values/5));
}

/////////////////////////////////////////////////////////
//Description: Convert an integer in to two bytes and sends the values to serial
//Parameters: Value to convert
//Returns: None
//////////////////////////////////////////////////////////
void SendInt(unsigned int value)
{
      TSSerial.print((unsigned char)(value>>8));
      TSSerial.print((unsigned char)value);
}

if i understand correctly i don’t have to set anything on analog pins to read them?

Correct - you're using "analogRead".

Thank you

Just to clarify the numbers 0 to 5 for the analog pins only work with analogRead(). All other calls take 0..5 to mean digital pins.

Also the "analog" pins are perfectly normal digital pins too, if you use the numbers 14..19 you can use pinMode(), digitalRead/Write etc. In fact the 14..19 numbers work with analogRead() too ;)

On the Arduino Mega there are more analog and more digital pins so overloading the analog and digital numbers helps with porting to the Mega - the same numbers correspond to the same positions on the headers - the digital pin numbers for the "analog" inputs on the Mega start from 54, not from 14...

It is confusing that analogWrite() isn't called PWMWrite(), for several reasons (and not just that the pin numbering is different for analogRead() and analogWrite()!

Just to clarify the numbers 0 to 5 for the analog pins only work with analogRead().

It depends on which processor you're using.