Go Down

Topic: Linear Actuator and Linear Potetiometer  (Read 727 times) previous topic - next topic

MB94

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

Code: [Select]

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

Code: [Select]


#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

Code: [Select]
#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;


  }
 
}



zeus2kx

#1
Mar 11, 2018, 06:17 pm Last Edit: Mar 11, 2018, 06:28 pm by zeus2kx
First thing I saw was
Code: [Select]
int sensorValue = analogRead(A1);
 is missing from you combined code.

MB94

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

TomGeorge

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


Code: [Select]
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.. :)
Everything runs on smoke, let the smoke out, it stops running....

MB94

Would that be in Void Loop or in Void setup?

TomGeorge

#5
Mar 12, 2018, 09:37 pm Last Edit: Mar 12, 2018, 09:37 pm by TomGeorge
Would that be in Void Loop or in Void setup?
In the void loop()...
make them the first two statements.

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

#6
Mar 18, 2018, 10:28 pm Last Edit: Mar 18, 2018, 10:29 pm by TomGeorge
Hi,
Sorry.
Put.


Code: [Select]
int sensorValue = analogRead(A1);
sensorValue = analogRead(A1);



Delete the second "int"

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

MorganS

Way too many of these...
Quote
Code: [Select]
  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."

MorganS

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

SandFarmer

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.

TomGeorge

Hi,
The code you posted in post #11 does not have the pot signal in it?
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.... :)
Everything runs on smoke, let the smoke out, it stops running....

outsider

You have:
Code: [Select]
byte servoPos=0;
Defined as a global AND in loop(), remove "byte" from the one in loop().

Go Up