How to code to shut off a linear actuator when it gets to a certain threshold based on fsr

I am using an Arduino uno and trying to force shut off the linear actuator when the fsr threshold hits max pressure. Max pressure on my fsr is around 200. I want the actuator to stop moving when it reaches this point. Below is my current code

byte mspeed=0;
int RPWM1 = 8;
int LPWM1 = 9;
int RPWM2 = 10;
int LPWM2 = 11;
int RPWM3 = 12;
int LPWM3 = 13;
int led1 = 4;
int led2 = 3;
int led3 = 2;
int button =5;
int A=0;
int B=0;
int count = 0;
int pressureAnalogPin=A0;
int pressureReading;
int pressureAnalogPin2=A1;
int pressureReading2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(4,OUTPUT);// output for lights
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
pinMode(5,INPUT);// input for button


pinMode(RPWM1,OUTPUT);
pinMode(LPWM1,OUTPUT);
pinMode(RPWM2,OUTPUT);
pinMode(LPWM2,OUTPUT);
pinMode(RPWM3,OUTPUT);
pinMode(LPWM3,OUTPUT);
pinMode(A0,INPUT);
}








void loop() {
// put your main code here, to run repeatedly:








A= digitalRead(5);
if(A!=B) {
if(A==HIGH){
 count++;}
}








B=A;
if(count==1){
digitalWrite(4,1);
if( digitalRead(7) == LOW & digitalRead(6) == HIGH  ){
 mspeed = 225;
 analogWrite(RPWM1,0);
 analogWrite(LPWM1, mspeed);
}
else if( digitalRead(7) == HIGH & digitalRead(6) == LOW  ){
 mspeed = 225;
 analogWrite(RPWM1,mspeed);
 analogWrite(LPWM1, 0);
}
else{
  analogWrite(RPWM1,0);
 analogWrite(LPWM1, 0);}
}
if(count==2){
digitalWrite(4,0);
digitalWrite(3,1);
if( digitalRead(7) == LOW & digitalRead(6) == HIGH  ){
 mspeed = 225;
  analogWrite(RPWM2,0);
 analogWrite(LPWM2, mspeed);}
 else if( digitalRead(7) == HIGH & digitalRead(6) == LOW  ){
 mspeed = 225;
 analogWrite(RPWM2,mspeed);
 analogWrite(LPWM2, 0);}
 else{
    analogWrite(RPWM2,0);
 analogWrite(LPWM2, 0);}
 }
if(count==3){
 digitalWrite(3,0);
 digitalWrite(2,1);
  if( digitalRead(7) == LOW & digitalRead(6) == HIGH  ){
 mspeed = 225;
   analogWrite(RPWM3,0);
 analogWrite(LPWM3, mspeed);}
 else if( digitalRead(7) == HIGH & digitalRead(6) == LOW  ){
 mspeed = 225;
 analogWrite(RPWM3,mspeed);
 analogWrite(LPWM3, 0);}
 else{
   analogWrite(RPWM3,0);
 analogWrite(LPWM3, 0);}
 }
 if (count==4){
   digitalWrite(4,1);
   digitalWrite(3,1);
   digitalWrite(2,1);
   if( digitalRead(7) == LOW & digitalRead(6) == HIGH  ){
mspeed = 255;
analogWrite(RPWM1,0);
analogWrite(LPWM1, mspeed);
analogWrite(RPWM2,0);
analogWrite(LPWM2, mspeed);
analogWrite(RPWM3,0);
analogWrite(LPWM3, mspeed);
}
else if( digitalRead(7) == HIGH & digitalRead(6) == LOW  ){
mspeed = 255;
analogWrite(RPWM1,mspeed);
analogWrite(LPWM1, 0);
analogWrite(RPWM2,mspeed);
analogWrite(LPWM2, 0);
analogWrite(RPWM3,mspeed);
analogWrite(LPWM3, 0);
}
else{
 analogWrite(RPWM1,0);
analogWrite(LPWM1, 0);
analogWrite(RPWM2,0);
analogWrite(LPWM2, 0);
analogWrite(RPWM3,0);
analogWrite(LPWM3, 0);}
}


   if (count==5){
   count=0;
   digitalWrite(4,0);
   digitalWrite(3,0);
   digitalWrite(2,0);}












  
{pressureReading=analogRead(pressureAnalogPin);
  Serial.print("Pressure Pad Reading = ");
  Serial.print("Variable_1:");
  Serial.print(",");
  Serial.println(pressureReading);


if (pressureReading > 1000){
 Serial.println(" - No pressure");


} else if (pressureReading > 700){
 Serial.println(" - Light Pressure");
 
 } else if (pressureReading > 400) {
 Serial.println(" - Medium Pressure");


}else if (pressureReading > 185) {
 Serial.println(" - Heavy Pressure");


} else if (pressureReading <=175) {
 Serial.println(" - Max pressure");}






{pressureReading2=analogRead(pressureAnalogPin2);
  Serial.print("Pressure Pad Reading = ");
  Serial.print("Variable_2:");
  Serial.println(pressureReading2);
  if (pressureReading2 > 1000){
 Serial.println(" - No pressure");


} else if (pressureReading2 > 700){
 Serial.println(" - Light Pressure");
 
 } else if (pressureReading2 > 400) {
 Serial.println(" - Medium Pressure");


}else if (pressureReading2 > 185) {
 Serial.println(" - Heavy Pressure");


} else if (pressureReading <=175) {
 Serial.println(" - Max pressure");}

}}}




What does the code actually do? I mean, what is the problem with the existing code.

Some comments in the code will help us to understand the intent of the code.

You give the pins names (good) but do not use the names (not so good). It is easier to follow the code if the pins have descriptive names and the names are used.

Please use the IDE autoformat tool (ctrl-t or Tools, Auto format) to format your code and remove the excess white space. Those things will make the code easier to read and follow.

The code is used to control a prosthetic hand. I am using 3 linear actuators to push pistons to give moment to each finger segment. Each finger segment is controlled by its on actuator. We also have a mode selector inside the code. This allows me to press the button once to control one actuator and so forth. After pressed 4 times all 3 linear actuators can be used and pressed 5 times no actuator is turned on. The fsr reading are on the finger tip of the finger. This is used so when picking an item up the fsr can read the applied pressure. I am trying to make it so when that fsr reads too high the linear actuators no longer extend.

if (pressureReading > 200) stop_actuator();

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