digitalRead and analogRead on one pin, how?

I have some code where I am using a laser light gate (laser and LDR).
For sensing when something has broken the beam I am reading digital from analog pin 1 (variable resistor to alter it so that it changes from high to low corrently. That all works fine.

Because I am wanting to use it in different lighting conditions etc I am wanting to have a calibration program. It would turn on the laser and request the person to adjust until the value it 650 ish and then turn off the laser to ensure that it falls low enough to work later for an digitalRead. I would much rather still use a digitalRead later on im my code than an analogRead becuase it is called a lot and it is time sensitive in some places.

I tried setting up 'another pin' as sensor1 and then doing:

ar = analogRead(sensor1);
mySerial.print(ar);

but I only get 0, not even 1 if I adjust the pot

I also tried not defining the pin and then doing:

ar = analogRead(0);
mySerial.print(ar);

Do I have to not define the 'digital sensor' in setup and then define it each time when I want to use it? Or is that not the best way to try?

EDIT: Oh sorry, wrong section, please move it someone :smiley:

Thanks,

Mowcius

I am reading digital from analog pin 1

ar = analogRead(0);

Which?

Which?

I am reading digital in most of the code but I want to read analog for calibrating the sensor.

Anyone?

Mowcius

[UNTESTED CODE]

const byte DA_OFFSET = 14; //digital to analog
const byte analogPin = 1;

void setup(){
  int ar = analogRead(analogPin);
  pinMode(analogPin+DA_OFFSET,INPUT); //i do not know if analogRead works after this  test it;)
  boolean state = digitalRead( analogPin+DA_OFFSET );
}

void loop(){}

Is it really that complicated? I would have thought that because I am not defining it as a digital or analog pin I could define something else to the same pin.

Does defining the input as pin 15, automatically define it as a digital pin?

Mowcius

Does defining the input as pin 15, automatically define it as a digital pin?

Pin 15 and analogue pin 1 are the same pin. The pin mode uses the digital nomenclature to address the pin. So if you define pin 15 as an output then you won't be able to do an analogRead(1);
Define Pin 15 as an input and you can read it with a digitalRead(15); and get a logic variable returned. OR you can read it with an analogRead(1); and get a two byte variable. All without having to redefine the pin mode.

Define Pin 15 as an input and you can read it with a digitalRead(15); and get a logic variable returned. OR you can read it with an analogRead(1); and get a two byte variable. All without having to redefine the pin mode.

Nice, thanks, I will try that.

Mowcius

Which?
I am reading digital in most of the code but I want to read analog for calibrating the sensor.

No, you said "I am reading digital from analog pin 1" but the code you posted says you're reading analogue from analogue pin zero.

No, you said "I am reading digital from analog pin 1" but the code you posted says you're reading analogue from analogue pin zero.

Oh sorry, that was just a quick example...

Mowcius