AnalogRead() for my flex sensor only gives 1023

I have been building a robot for a competition, and I have been wondering why my flex sensor only gives the number 1023. It used to give various numbers, but it suddenly just started spamming 1023 in the serial interface.

#include <Servo.h> 

Servo leftdriver;
Servo rightdriver;
Servo backgripper;
Servo flipper;
Servo grab;

int potidle = 0;
int flexidle = 0;
int softpotidle = 0;
int idlelock1 = 0;
int idlelock2 = 0;
int idlelock3 = 0;
int grabidle=0;
int grabtoggle=0;
int buttonState= 0;
int potPin =A5;
int flexpin = A0;
int softpotpin = A2;
int button = 4;

void idle (int potidle1, int flexidle1){
if ( (potidle1 == 0 ) && (flexidle1 == 0) ) {
leftdriver.write(95);
rightdriver.write(93);
}
}


void setup() 
{ 
  leftdriver.attach(6);
  rightdriver.attach(5);
  backgripper.attach(10);
  flipper.attach(9);
  grab.attach(8);
  digitalWrite(flexpin,HIGH);
  digitalWrite(softpotpin, HIGH);
  digitalWrite(potPin,HIGH);
  Serial.begin(9600);
} 

void loop()
{
  int flexreadin = analogRead(flexpin);
  int softpotin = analogRead(softpotpin);
  int potin = analogRead(potPin);
  buttonState = digitalRead(button);
  if ((flexreadin>900) && (flexreadin <925)){   //turns flexreadings into -1,0,1 states
    flexidle = 0;
  }
  else if ((flexreadin>866) && (flexreadin<875)) {
    flexidle = 1;
  }
  else if ((flexreadin>980) && (flexreadin<1023)){
    flexidle = -1;
}

 if (flexidle ==1){
   leftdriver.write(105);
   rightdriver.write(83);
   idlelock1 = 0;
}
else if (flexidle ==-1){
  leftdriver.write(85);
  rightdriver.write(103);
   idlelock1 = 0;
}
else if (flexidle == 0){
  idlelock1 = 1;
}

if ((softpotin > 990) &&  (softpotin <1016)) {
  softpotidle = 0;
}
else if ((softpotin > 660) && (softpotin <820)) {
  softpotidle =1;
}
else if ((softpotin >0) && (softpotin <650)) {
  softpotidle = -1;
}

if (softpotidle == 0) {
  idlelock2 = 1;
}
else if (softpotidle == 1) {
  leftdriver.write (105);
  rightdriver.write (103);
  idlelock2 = 0;
}
else if (softpotidle == -1) {
  leftdriver.write (85);
  rightdriver.write (83);
    idlelock2 = 0;
}
  
if ((idlelock1==1) && (idlelock2==1)){
  leftdriver.write(95);
  rightdriver.write(93);
}

if ((potin> 800) && (potin <930)) {
  flipper.write(10);
}

else if ((potin>990) && (potin<1025)){
  flipper.write(180);
}

if (buttonState==1){
  grabidle=1;
}
   else if (buttonState == 0){
   grabidle=0;
   }
   
if((grabidle==1)&& (grabtoggle==0)){
  grabtoggle=1;
}
 else if ((grabidle==1)&& (grabtoggle==1)){
   grabtoggle=0;
 }
 
 if (grabtoggle==1){
 grab.write(5);
 }
 else if (grabtoggle==0){
   grab.write(180);
 }

int flexreading = analogRead(flexpin);
Serial.println(idlelock1);
Serial.println(idlelock2);
Serial.println(flexreading);
Serial.println(softpotin);
Serial.println(potin);
Serial.println(grabtoggle);
delay(2000);

}

well, now the potentiometer is spitting out 1023 instead of a flex sensor. It is as if my arduino has a virus or something, which I know is impossible.

Hi, have you measured the voltages at the analog input pins on the arduino to check that it is getting the level expected, also check the gnd connections to the pot and force sensor.

Tom.... :slight_smile:

The first thing that I would do is to reduce the program down to reading the analog input, outputting it then waiting for a short time before repeating. This will allow you to easily test the wiring without wondering if the rest of the program is causing a problem.

As a mater of interest how are the servos powered ?

Tell us how you have wired up the flex sensor and what value pull up or pull down resistor you have used.

now the potentiometer is spitting out 1023 instead of a flex sensor.

That sounds to me like a ground has dropped off.

1023 is the largest number that analogRead() produces.

Instead of this,

  digitalWrite(flexpin,HIGH);
  digitalWrite(softpotpin, HIGH);
  digitalWrite(potPin,HIGH);

try this,

  pinMode(flexpin,INPUT);
  pinMode(softpotpin, INPUT);
  pinMode(potPin,INPUT);

Not sure why your are writing a high value to pins that you are using for analog inputs.

Also like some others said is to first check the voltage going to the pin directly with a multimeter to rule out hardware issues.

How do you have your flex sensor wired? I would think it would need to also have a resistor such that flex sensor/resistor combo would simulate a pot/voltage divider.