Need help with reading potvalue using arduino uno

void setup() {

  int switchState = 0;
  int switchPin = 2;
  int const potPin = A0;
  int Potval;

  //9600 bits per second:

  pinMode (potPin, OUTPUT);
  pinMode (switchPin, INPUT);
  Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int x = 1;
  switchState = digitalRead(2);
  if (switchState == HIGH);
    for (int    y = 0;    y < 5;    y = y + x);    {
    potVal - analogRead(potPin);
    Serial.print("potVal: ");
    Serial.println(potVal);
    delay(2);
  }

  Serial.println("Measurement made");
  delay(200);


}

Hi, I need some help with programing a code to read the potvalue when i press the button. Im trying to have the raw value be read five times when i press the button, and if i turn the knob it has to read the new raw values again when i press the button.

This is what I have code-wise and I'm bewildered. I have a picture of the arduino uno setup with the potentiometer with A0 and digital pin 2

Imgur

if (switchState == HIGH);

Oops

potVal - analogRead(potPin);

'=' not '-' ?

potVal also seems to suffer from scope problems.
Why not post the compilation errors?

for (int y = 0; y < 5; y = y + x); Oops,again

    for (int    y = 0;    y < 5;    y = y + x);    {

Also oops. You terminated the for loop with a semicolon.

It's not going to do anything useful until you've got it to compile.

Don't declare your variables inside setup() or they will only be available in setup(). Shouldn't potPin be INPUT?
And "if (switchState == HIGH);" means if switchState is HIGH do nothing. Lose the ;.

Steve

int switchState = 0;
int switchPin = 2;
int const potPin = A0;
int Potval;

void setup() {



  pinMode (potPin, INPUT);
  pinMode (switchPin, OUTPUT);
  Serial.begin (9600);
}

void loop() {

  int x = 1;
  switchState = digitalRead(2); {
    if (switchState == HIGH)
      for (int i = 0; i < 5; i = i + x)   {
        Potval = analogRead(potPin)
        ;Serial.print("Potval: ")
        ;Serial.println(Potval)
                 ;delay(2)
              
;      }

    Serial.println("Measurement made");
    delay(200);

  }}

Ok I modified the code a little bit, i thought the compiler only did one problem, not all problems. Should be good

nevermind, i think I have it on a loop where I press the button and it will keep stating measurement made

You made the switch pin an output. Slow right down and double check your work. If you pressed the button with that running, you may have destroyed an output on your board.

Subsequently, you use it in only one place but not here:

  switchState = digitalRead(2); {
;Serial.print("Potval: ")

Where did you pick up the unusual semicolon use?

There is no reason to give Potval global scope.
Ditto switchstate

int switchState = 0;
int switchPin = 4;
int potPin = A0;
int Potval;

void setup() {
  // put your setup code here, to run once:

  pinMode (potPin, OUTPUT);
  pinMode (switchPin, INPUT);
  Serial.begin (9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  switchState = digitalRead(4);
  if (switchState == HIGH) {
    Potval = analogRead(potPin);
    Serial.print("Potval:");
    Serial.println(Potval);
    delay(200);
  }

  else {
    (switchState == LOW);
    while (Potval);
  }

}

For some reason I had to switch the location of the semicolon, but believe I had an error somewhere. So at this point I'm not sure what to do. I changed some things just in case

(switchState == LOW);
    while (Potval);
  }

}

What's the intention here, please?

TheMemberFormerlyKnownAsAWOL:

(switchState == LOW);

while (Potval);
 }

}



What's the intention here, please?

I want the program to not do anything until I press the button

const int switchPin = 2;   // Button connected to ditgital pin 2
const int potPin = A0;  //Analog pin for potentiometer pin
int switchState = 0; //sets switchstate and potentiometer value at 0
int potVal = 0;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(potPin, INPUT);   //Have potentiometer AND the switchpin aka button as INPUT:
  pinMode(switchPin, INPUT);
  Serial.begin(9600);  //starting baud count

}

void loop() {
  // put your main code here, to run repeatedly:

switchState = digitalRead(switchPin);  //will read value from specified digital pin
  if (switchState == HIGH) {  //if the button is pressed
    potVal = analogRead(potPin);  //Will read potentiometer pin value
    Serial.println(potVal);  //puts data into ASCII text
    delay(1000);   //delay of 1 sec
  }
}

Fixed, should be good to go