Using Analog IN as DIGITAL INPUT

Using Analog in as DIGITAL INPUT

I found and understand the string for using analogs as digital OUTPUTs

Trying to use A1 as a digital INPUT… can this be done??? My code attempt follows…

//.................................................................................
void setup()
{
    Serial.begin(9600);

//Setup if ALL DOs are the same
	for (int xpin = D0; xpin <= D7; ++xpin) { pinMode (xpin, OUTPUT);   }
	for (int xpin = D0; xpin <= D7; ++xpin) { digitalWrite (xpin, LOW); }

pinMode (A1, INPUT); // INPUT_PULLDOWN); // INPUT_PULLUP);  // Use  for a digital switch

} // END SETUP 
//.................................................................................

#define ON 1
#define OFF 0

int SWITCH;

//..................................................................................
void loop()
{

	 SWITCH = (digitalRead, A1);
	 Serial.print("A1 SWITCH = "  );
	 Serial.println(SWITCH, DEC);	// retuns “12” no matter what the IN value.

	if (SWITCH == ON) {digitalWrite(D6,ON);}else{digitalWrite(D5,OFF); }
	if (SWITCH == OFF)  {digitalWrite(D5,ON);}else{digitalWrite(D5,OFF); }

        delay(1000); // wait for a time

} // END LOOP
//................................................................................

I think you must have meant to say:

  SWITCH = digitalRead(A1);

That should work just fine.

1 Like

Thank you for the fast answer… off for now… will try tomorrow…

Problem SOLVED !!
Thanks bko. I stared past this simple error for hours; should have asked sooner. The compiler did not flag an error, so i thought “my code” must be correct, right???.

Do you have an opinion what the compiler was doing with the bad statement:

SWITCH = (digitalRead, A1);

I have asked around with a few C++ experts here at work and no one seems to know what that would mean. Compiler bug was the most common answer!

I just threw SWITCH = (digitalRead, A1); in my setup routine on an app I had already and I tried compiling it with an error and it didn’t show up as a warning in the webIDE, but compiling it locally does produce a warning:

I would think it should output 11 all of the time if it was ignoring “digitalRead” though… not 12.

BDub… First a complement well overdue. I have been successfully trained and entertained by some of your code. I have lately been using your HTML/?Ajax?/Java to employ the browser buttons to test my code. Thank you very much.

So, you throw a bone, saying a half answer that you know why the statement will read 11… or 12. I would be educated if you would disclose your golden nugget.

Hint: the port variables (A0, A1, A2 etc) are assigned integer values.

I am going to stick with my answer of “compiler bug” since I don’t think this is correct C/C++.

Thanks @LZHenry, just trying to help out and make learning as fun as possible.

From the spark_wiring.h:

#define D0 0
#define D1 1
#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7

#define LED1 LED_USER

#define A0 10
#define A1 11
#define A2 12
#define A3 13
#define A4 14
#define A5 15
#define A6 16
#define A7 17

I knew A0 = 10, so A1 should equal 11 (and it does). If the compiler ignores the “digitalRead” to the left of the comma, then it’s not adding an extra 1 in there or anything. But you said it was constantly printing 12 to the serial port… so… maybe the online compiler is adding 1 somehow.