Flex sensor and button input combination

I am trying to have one LED turn off while another turns on after increasing the value of a flex sensor and pushing down on a button. Right now, both LED stay on no matter the input and I have no idea what the problem is. Any suggestions would be appreciated.

//LEDs
int ledBlu = 8;
int ledGre = 7;
//Sensors
int flexSensor = A5;
int buttonState = 0;
int value;

void setup() 
{
  pinMode(ledBlu,OUTPUT);
  pinMode(ledGre,OUTPUT);
  pinMode(buttonState,INPUT);
  pinMode(flexSensor,INPUT);
  Serial.begin(9600);
}

void loop()
{
  value = analogRead(flexSensor);
  value = map(value,700,900,0,255);
  buttonState = digitalRead(11);
  digitalWrite(ledBlu,value);
  digitalWrite(ledGre,value);
  Serial.println(value);

  if(buttonState == HIGH && value>408)
  {
    digitalWrite(ledBlu,HIGH);
    digitalWrite(ledGre,LOW);
  }
  else
  {
    digitalWrite(ledBlu,LOW);
    digitalWrite(ledGre,HIGH);
  }
}

Well, first of all "pinMode()" isn't necessary when using analogRead(). Remove that line.
Then, you also need to describe the connection (a diagram is appreciated).
But it isn't a hard guess saying you haven't connected your buttons with a "pull-up" or "pull-down" resistor.

Review the results of an analogRead().

Review the operation of the map() function.

Here, your result will never be more than 255…

Check your wiring.

Lose these first two digitalWrite() operations if you expect the subsequent ones to ever make a diffence.

a7

Welcome to the forum

pinMode(buttonState,INPUT);

I don't think that you meant this, or did you ?

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