2

Hi I'm trying to get my Arduino Uno code to work with a in a specific way where if I push a button, the Arduino will read the raw potentiometer value once and if I don't push it nothing happens.

https://imgur.com/M7qGvtC

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

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

}

void loop() {
switchState = digitalRead(2);
if (switchState == High) {
Potval = analogRead(potPin);
Serial.print("potval:");
Serial.Println(Potval);
delay(200);
}

else {
(switchState == LOW);
while (Potval);
}
}
3
  • what is your question about the arduino? ... the question in your title is not really related to the arduino, and it has an answer of yes – jsotola Nov 26 '20 at 2:28
  • 1
    What should that do: (switchState == LOW); while (Potval);? The first line is doing nothing, since this expression is not inside of any flow control command. The second line will loop forever (blocking the code indefinitely), when Potval is not zero. Doesn't seem fitting to me. Why are you even doing this? – chrisl Nov 26 '20 at 7:45
  • 1
    Beware that the Arduino language (C++) is case sensitive: Println is not the same as println, High is not the same as HIGH. – Edgar Bonet Nov 26 '20 at 8:18
1

I had a very similar issue an it is that you aren't setting up your button as an INPUT_PULLUP. When you press the button, you are sending 5V or ground through it depending on your circuit. But then it won't reset on release. That is why either you can put a physical resistor as the schematic shows here: enter image description here

or you can simply change the button initialization to INPUT_PULLUP.

1

There were several issues with your code:

  • It would not compile due to some missing semi-colons (lines 1 & 2);
  • The 'else' clause in your loop() function didn't do anything useful;
  • If your code didn't get trapped in the loop within the 'else' clause (it probably would!), it would print repeatedly as long as you were NOT pressing the button, since you wired the button to read LOW when pressed (which is perfectly fine, buy your code has to take that into account).
  • Buttons appear to bounce (electrically) when you press or release them. They don't switch once and stay that way. They switch multiple times before they settle down, due to the rough contact surfaces sliding over one another. The code has to allow for this as well.
  • An analog pin has to not be set to OUTPUT if you want to read analog with it.

Here is a revised version of your code, compiled and tested. You'll find notations in the comments where I made changes:

int switchState = 0;            // (';' was missing)
int switchPin = 2;          // (';' was missing)
int potPin = A0;
int Potval = 0;             // (this declaration was missing)
bool didPrint = false;      // true if we printed on this button-push
// prevents multi-printing while button stays high

void getSwitch();               // declare the new function


void setup() {
  //  pinMode (potPin, OUTPUT);  // not OUTPUT for analog read
  pinMode (switchPin, INPUT_PULLUP);  // needs pullup or reads may be unreliable
  pinMode (13, OUTPUT); digitalWrite(13, LOW); //DEBUG
  Serial.begin (9600);

}

void loop() {
  getSwitch();
  if (switchState == LOW ) {        // pressed == LOW the way you've wired it

    if (!didPrint) {
      Potval = analogRead(potPin);
      Serial.print("potval:");
      Serial.println(Potval);

      didPrint = true;      // prevent printing again til button has been released
    }
  }
  else if  (switchState == HIGH) {
    didPrint = false;       // printing allowed on next button push
  }
}


void getSwitch() {
  switchState = digitalRead(switchPin);
  delay(30);                // waits for button to settle - may need to tweak this
}
-5

I believe you are getting lots of readings when the button is pushed but nothing when released. This link should solve your problem.

4
  • 1
    i think you forgot the link – Matt Nov 26 '20 at 0:47
  • 2
    We don't like link-only answers (and in your case you forgot the link). The answers here should stand on their own. Links going down is a thing, and indeed quite a common thing. – Nick Gammon Nov 26 '20 at 6:48
  • There is no link. – DragonflyRobotics Nov 26 '20 at 18:50
  • The link was removed. I did the link in place of copying and pasting several pages of text. – Gil Nov 26 '20 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.