Arduino uno. a1 a2 value not Zero? am noob!

a1 end a2 have no voltage so should be Zero?

// full code

int X;
int Y;
int PRO;
void setup() {
  Serial.begin(2400);
  pinMode(A2, INPUT);
  pinMode(A1, INPUT);
  pinMode(13, OUTPUT);
  PRO = 0;

  // put your setup code here, to run once:
}

void loop() {
  Y = analogRead(A2);
  X = analogRead(A1);
  if (Y == HIGH) {
    PRO = PRO + 1;
  }

  else if (X == HIGH) {
    PRO = 0;
  }

  else if (PRO >= 1) {
    analogWrite(13, HIGH);
  }

  else if (PRO > 5) {
    PRO == 5;
  }

  else {
    analogWrite(13, LOW);
  }
  Serial.print("A2:");
  Serial.println(A2);
  Serial.print("A1:");
  Serial.println(A1);
  Serial.print("PRO:");
  Serial.println(PRO);
  delay(1);
  // put your main code here, to run repeatedly:
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

That just prints

 8: 812: 12

Did you mean to print the value of X or Y or read the pins again?

Also, what kind of Arduino are you using?

Why are you using analogRead() for signals you are using as digital?

a7

What Arduino board are you using? Are pins 8 and 12 analog inputs on your board?

To what are pins 8 and 12 connected?

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

What is on the receiving end of the serial prints?

arduino uno. I want pin 8 end 12 to Serial.println to ide.

Why are you using analogRead() for signals you are using as digital?

a7 ?

Yes , look at IDE examples for analog and digital inputs . If using analog inputs , you don’t need pinMode and the input has a number value not HIGH or LOW.
If you are using digital inputs , you need pinMode and then it’s digitalRead .

If these pins are not connected to anything you may read random values

Note to self : type faster

Analogread returns a value from 0 through 1023 on most arduino boards. HIGH will be any value other than 0, LOW will be only O, so you will rarely see LOW on an analog pin unless it is connected directly to ground.

< edit >
The UNO has no analog input 8 or 12, only 0 to 5 on the input pins, and an internal 6 & 7 that have no external connection (because the same chip is also used in larger packages that can connect these externally).


arduino uno.

 else if (PRO >= 1) {
   analogWrite(13, HIGH);
 }

 else if (PRO > 5) {
   PRO == 5;
 }

This block of code is very odd, If PRO is greater than 5, the value PRO is compared with 5, and the result of the comparison is then thrown away. To make it even more confusing, this is the ELSE clause of the previous IF that checks to see if PRO is greater than or equal to 1. Since 5 is greater than or equal to 1, the main part of the if will execute, and the entire else clause will always be skipped.

Throughout your code, you are comparing values read from an analog port to the constants HIGH and LOW. HIGH is defined in the header files as 1, and low is defined as 0. The analog value is somewhere between 0 and 4096, the chances of an analog value matching one of these constants is negligible. On the Uno, pins 8,12, and 13 are digital only pins, which suggests to me that you should be using digitalRead() and digitalWrite() for these pins.

Disconnect that 9V battery before you damage the UNO. Fortunately it does not appear to be connected to anything at the moment.

The LEDs have no current limiting resistors, that will eventually damage either the LED or the UNO, possibly both.

Since it looks like we are starting at the very beginning with this stuff for @gru2x2 I point out that i/o pins default to INPUT.

Often the weak pull-up resistor is useful, for that you woukd need to use pinMode() with INPUT_PULLUP mode.

I usually put use pinMode() calls even if just setting INPUT mode - just to be perfectly clear as well as not relying on my memory of what the default is.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.