Linear Actuator and Linear Potetiometer

So this is my problem! I have two codes- one for my linear potentiometer (Code 1) and one for my actuator and force sensor ( Code 2). Individually they work fine on their own! Merging them together they don’t- the issue that I have is on the serial monitor for the linear potentiometer isnt showing me the correct values. I would really appreciate it if someone could tell me what I am doing wrong and how I can fix it!

CODE 1 - Linear Potetiometer code ALONE

int sensorValue=A1;

void setup() {
  Serial.begin(9600);
}
 
void loop() {
  int sensorValue = analogRead(A1);
  Serial.println(sensorValue/12.7875);

  delay(300);
}
//

CODE 2 - Linear Actuator Code with force sensor

#include <Servo.h>

Servo myServo;

byte forcePin = A0;
byte servoPin = 9;
byte ledPin = 13;


byte servoPos = 0;
int countsFor10 = 200;  // find how many ADC counts = 10 force units
boolean forceApplied = false;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  myServo.attach(servoPin);

}

void loop()
{

 

  if (forceApplied == false)  // only run once until force is applied
  {
    while (analogRead(forcePin) <= countsFor10)
  
    {
       Serial.println(analogRead(forcePin));
delay (100);

      myServo.write(servoPos);
    
      delay(100);  // change delay to suit
      servoPos++;
    }
    forceApplied = true;
    digitalWrite(ledPin, HIGH);


    

 Serial.println("endrun");
 Serial.println(analogRead(forcePin));
  byte servoPos=0;


  }
  
}

CODE 3 -My attempt to merge

#include <Servo.h>

Servo myServo;

byte forcePin = A0;
byte sensorValue=A1;
byte servoPin = 9;
byte ledPin = 13;



byte servoPos = 0;
int countsFor10 = 200;  // find how many ADC counts = 10 force units
boolean forceApplied = false;

void setup()
{

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  myServo.attach(servoPin);

}

void loop()
{

  Serial.println(sensorValue/12.7875);

  delay(300);
 
  if (forceApplied == false)  // only run once until force is applied
  {
    while (analogRead(forcePin) <= countsFor10)
  
    {
       Serial.println(analogRead(forcePin));
delay (100);

      myServo.write(servoPos);
    
      delay(100);  // change delay to suit
      servoPos++;
    }
    forceApplied = true;
    digitalWrite(ledPin, HIGH);


    

 Serial.println("endrun");
 Serial.println(analogRead(forcePin));
  byte servoPos=0;


  }
  
}

First thing I saw was

int sensorValue = analogRead(A1);

is missing from you combined code.

Hi, thanks- I tried this and it still doesnt work!

Hi,
Read the sensor value twice, one after the other like this.

int sensorValue = analogRead(A1);
int sensorValue = analogRead(A1);

There is only one AtoD, it is multiplexed between the A0 to A5 inputs.
It needs time to read each input.

If the read is too soon after reading another pin then the AtoD will read some value between the two pins, this is because of a capacitor on the AtoD input still holding some charge from the previous pin read.

Reading the same pin twice allows this to be cleared and the last read should be the accurate AtoD conversion.

Hope it helps.. Tom.. :slight_smile:

Would that be in Void Loop or in Void setup?

MB94:
Would that be in Void Loop or in Void setup?

In the void loop()...
make them the first two statements.

Tom.. :slight_smile:

Hi,
Sorry.
Put.

int sensorValue = analogRead(A1);
sensorValue = analogRead(A1);

Delete the second "int"

Tom.. :slight_smile:

Way too many of these...

  delay(300);

Do you understand what they do in the original programs? Why more is not better?

The problem is in the code you didn't post.

MB94:
Hi thanks for your help! I am still having a little bit of issue. So the shaft of the actuator is moving up and down and the potetiometer is measuring that distance, although, it is measuring the distance at the begning of when the actuator moves, not at the end! How would I programme it so that it mesures the distance at the end?

Thanks!

What exactly are you trying to accomplish with the potentiometer and the actuator? Are you a) moving the actuator to a preset position or are you b) just using timers to move it a bit, then measure where it stopped moving.

If a) - you can program a sketch to constantly check position and adjust actuator as needed
If b) - move the line which reads the potentiometer below the part of sketch where you tell the actuator to move. Then do your Serial.print.

Hi,
The code you posted in post #11 does not have the pot signal in it?

MB94:
Hi, I am trying to program the potentiometer to measure when the actuator has stopped moving.

The pot is an input not an output what do you want its input to do?
Just show the stopping point when the force meter has shown the actuator has applied a force?
What is the application?
Is the amount of force important or can you use a limit switch?
Tom.... :slight_smile:

You have:

byte servoPos=0;

Defined as a global AND in loop(), remove "byte" from the one in loop().