error: 'A1' was not declared in this scope

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

error: 'A1' was not declared in this scope

Post by MilanK »

I read the tutorial, and this part is confusing me:
analogRead()
You can read an analog voltage from digital #2 (called A1), digital #3 (called A3) and digital #4 (called A2)

For example, to read an analog voltage on pin #2, you would call analogRead(A1)
So, I am trying to read a piezo knock sensor with:

Code: Select all

  knockSensorVal = analogRead(A1);
Which gives me the following error:
sketch_sep19b.ino: In function 'void loop()':
sketch_sep19b:68: error: 'A1' was not declared in this scope
I've tried just using '2' (also tried '1' after finding an example on google) to no avail. These compile and load to the trinked, but I am not seeing the behavior I expect...

Here is my loop:

Code: Select all

void loop() {
  // Listen for any knock at all.
  knockSensorValue = analogRead(A1);
    
  if (knockSensorVal >=threshold){
    digitalWrite(redLED,HIGH);
    delay(500);
    digitalWrite(redLED,LOW);
    delay(500);
  }
} 
Basically, as a starting point, I just want to blink the redLED when I see a knock. As soon as I load the code, however, the red lihgt just starts blinking, and never stops. As if it is seeing constant knocks... (regardless of what I set threshold to).

Any ideas?

MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

Re: error: 'A1' was not declared in this scope

Post by MilanK »

Just an update. I got the LED behavior I expected by bumping the threshold up a lot more than the values I was used to with my Uno.

Still confused about the 'A1' part. It works if I just use '1'.

So, AnalogRead(1) and DigitalRead(1) will read pins 2 and 1, respectively? Still scratching my head.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: error: 'A1' was not declared in this scope

Post by adafruit_support_bill »

A little more detail would be helpful. What tutorial are you referencing? Which microcontroller are you using? The pin mapping varies depending on the processor.

MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

Re: error: 'A1' was not declared in this scope

Post by MilanK »

Sorry, that was dumb of me to not mention that I'm trying to use a Trinket. (It's in there, but typoed of course...)

I referenced this tutorial: http://learn.adafruit.com/introducing-t ... rduino-ide

-m

MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

Re: error: 'A1' was not declared in this scope

Post by MilanK »

I have things working, but I'm still confused. If I want to use pin 1 to control a red LED and pin 2 as an analog input, here is what works:

set pin 1 as OUTPUT and pin 2 as INPUT
digitalWrite to pin 1 to control the LED state
analogRead pin 1 to monitor the sensor on pin 2

What is confusing me is I have to call pin 2 by two different numbers. I have to call it pin 2 in pinMode but I have to call it pin 1 in analogRead.

And I still get a compiler error if I try to use A1.

MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

Re: error: 'A1' was not declared in this scope

Post by MilanK »

My current understanding:

analogRead(1) and digitalRead(1) don't read the same physical pin. They read pin 2 and pin 1, respectively. The cleanest code I could think of was to do this: (only showing the relevant lines)

Code: Select all

const int knockSensor = 1; 
const int knockSensorPin = 2;

pinMode(knockSensorPin,INPUT);

knockSensorValue = analogRead(knockSensor);
This was confusing at first, since I did actually want to use pin 1 as a digital output...

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: error: 'A1' was not declared in this scope

Post by westfw »

analogRead(1) and digitalRead(1) don't read the same physical pin.
That IS consistent with the Uno and other traditional Arduino boards, although I can see how it would be particularly confusing on a board with so few pins...

MilanK
 
Posts: 12
Joined: Fri Jun 14, 2013 11:59 pm

Re: error: 'A1' was not declared in this scope

Post by MilanK »

Or, put another way, analogRead(1) and digitalRead(2) do read the same physical pin.

My poor pins must be so confused.. On the bright side, I got my project finished: A Trinket-based knock sensor in an Altoids tin that controls a solenoid-based door lock. And I only bricked 1 trinket in the process. (Don't hook +12V to a 5V input..)

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: error: 'A1' was not declared in this scope

Post by wbp »

OK, so the OP got it working, but the original question was: why the error message?

I just got my first Trinket, and followed the install instructions, and am trying to do an analog read following the Adafruit tutorial, and get the same error...

int y = analogRead(A2);

Error: 'A2' was not declared in this scope.

Seems like some declarations are missing?

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: error: 'A1' was not declared in this scope

Post by adafruit »

tutorial is fixed, call analogRead(1) or analogRead(2) - etc.

http://learn.adafruit.com/introducing-t ... analogread

User avatar
AdrianW15060
 
Posts: 3
Joined: Sun Sep 30, 2018 8:31 am

Re: error: 'A1' was not declared in this scope

Post by AdrianW15060 »

I also have that error.

Here are a few lines of my coding:

Code: Select all

int doorPin = 2;
int winPin = 3;
const int tempPin = analogRead(A0);
const int ldrPin = analogRead(A1);
int servo1Pin = 5;
int servo2Pin = 6;
And it gives me this error:

Code: Select all

smart_classroom:19: error: 'A1' was not declared in this scope

 const int ldrPin = analogRead(A1);

                               ^

exit status 1
'A1' was not declared in this scope
If someone would reply me soon, I send my thanks to them.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: error: 'A1' was not declared in this scope

Post by adafruit_support_bill »

@AdrianW15060 - what processor are you using?

User avatar
AdrianW15060
 
Posts: 3
Joined: Sun Sep 30, 2018 8:31 am

Re: error: 'A1' was not declared in this scope

Post by AdrianW15060 »

adafruit_support_bill wrote:@AdrianW15060 - what processor are you using?
Processor: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz 1.80

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: error: 'A1' was not declared in this scope

Post by adafruit_support_bill »

What processor are you compiling for?

User avatar
AdrianW15060
 
Posts: 3
Joined: Sun Sep 30, 2018 8:31 am

Re: error: 'A1' was not declared in this scope

Post by AdrianW15060 »

Actually I solved it by putting 23 for A0 and 24 for A1. But anyway thanks for your time. I hope I didn't waste any of your time.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”