Program reads out wrong ports

I have a Program :

int led_pin = 6; 
int pot_pin = A1;
int output;
int led_value;
int inPin = A2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = 0;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 700;   // the debounce time, increase if the output flickers

void setup() {
  pinMode(led_pin, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5 , OUTPUT);
  Serial.begin(9600);
}
void loop() {
  //Reading from potentiometer
  output = analogRead(pot_pin);
  //Mapping the Values between 0 to 255 because we can give output
  //from 0 -255 using the analogwrite funtion
  led_value = map(output, 0, 1023, 20, 255);
  analogWrite(led_pin, led_value);
  delay(1);
  reading = analogRead(inPin);
  Serial.println(reading);
  if (reading > 1000 && previous < 900 && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }
  if(state == HIGH){
      pinMode(6, OUTPUT);
  pinMode(5 , OUTPUT);
    analogWrite(6, led_value);
    digitalWrite(5,LOW);
  }else{
      pinMode(6, OUTPUT);
  pinMode(5 , OUTPUT);
    digitalWrite(6,LOW);
    analogWrite(5, led_value);
    
  }
  previous = reading;
  
}

i have an poti connected to port A1
and a Button connected to port A2

the problem is, that i don't understand is, that the serial output is from both, the poti and the button?!
what did I do wrong?
I'm using an Arduino UNO, and I have an Motor connected to port 5 and 6
the program is also messing with the speed of the motor

the idea is, that I have an button to switch the motors direction, and the poti to regulate the speed

Please help me!!!

You need to handle the button with digitalRead, not with analogRead

  1. Why are you using analogRead() for a button input?
  2. How is your button wired?
  3. Why do you reconfigure pins 5 and 6 in loop()?
  if (state == HIGH) {
    pinMode(6, OUTPUT);
    pinMode(5 , OUTPUT);
    analogWrite(6, led_value);
    digitalWrite(5, LOW);
  } else {
    pinMode(6, OUTPUT);
    pinMode(5 , OUTPUT);
    digitalWrite(6, LOW);
    analogWrite(5, led_value);

  }

Really need to see your wiring diagram.

Can you post a sample of your serial output?

I need the button to be read in analog cause i have interference, which was messing with the readings(an input was read when there was none)!

serial output:

anonymusdennis:
I need the button to be read in analog cause i have interference, which was messing with the readings(an input was read when there was none)!

What kind of interference? Bouncing?

You can use a digitalRead and software debounce.

You need to connect the switch as on the image below:

Your serial output is only showing the value of pin A2. Is your button wired normally closed?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

A port is a collection of pins, you are talking about pins and calling them ports. The Arduino Uno has
3 ports, B, C and D.