Linear actuator doesn't work by ultrasonic sensor

hi, im programming to move linear actuator with ir remote controller and ultrasonic sensor.
i success to program each other, but since i combine two codes, it works only remote controller.
the ultrasonic sensor is measuring distance well, but linear actuator doesn't move.
can anyone help me?
this is my code

#include <IRremote.hpp>   //must copy IRremote library to arduino libraries
#define plus 0xE718FF00   //move forward button
#define minus 0xAD52FF00  //move backward button
#define automatic 0xE31CFF00   //switch to automatic

#define PWMA0 9 
#define PWMB0 10
#define enable0 13 //pins for motor driver

#define echoPin 7 //echo pin on ultra sonic sensor
#define trigPin 8 //output on ultra sonic sensor

float duration, distance; 

const int RECV = 2;       //IR receiver pin
int enable = 0; //enable pin for motor driver

int count[] = {0}; 
int currentPos = 0; //current position
int threshold = 50; //position tolerance
int destination = 0;

bool forwards = false;
bool backwards = false; // motor states

IRrecv irrecv(RECV);

void setup() {
  pinMode(PWMA0, OUTPUT);
  pinMode(PWMB0, OUTPUT); //set PWM outputs

  pinMode(enable0, OUTPUT); 
  digitalWrite(enable0, LOW); //set enable and turn board OFF

  attachInterrupt(0, speed0, RISING); 

  pinMode(trigPin,OUTPUT);
  pinMode(echoPin, INPUT);
  
  Serial.begin(9600); 
  Serial.println("READY"); 
  irrecv.enableIRIn(); // Start the receiver
} //end setup

void loop() {
 if (irrecv.decode()) 
 {
  Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
  irrecv.resume(); // Receive the next value

  if (irrecv.decodedIRData.decodedRawData == plus)
  {
   forwards = true; backwards = false;
   digitalWrite(enable0, HIGH); // enable board
   analogWrite(PWMA0, 255); 
   analogWrite(PWMB0, 0); // apply speeds
  }

  if (irrecv.decodedIRData.decodedRawData == minus)
  {
   forwards = false; backwards = true;
   digitalWrite(enable0, HIGH); // enable board
   analogWrite(PWMA0, 0);
   analogWrite(PWMB0, 255); // apply speeds
  }
 }

 getDistance(); // measure distance of object from ultra sonic sensor
 currentPos = count[0]; 
   
 if (irrecv.decodedIRData.decodedRawData == automatic)
 {
  if(distance < 1000) // ignore value if greater than stroke length

  {destination = distance*275 ;} //translate measured distance (in mm) to desired stroke position (in pulses)

  if ((destination >= (currentPos - threshold)) && (destination <= (currentPos + threshold))) stopMoving();//stop acuator if it is in the desired position
  else if (destination > currentPos) goForwards();
  else if (destination < currentPos) goBackwards();

  Serial.print("Counts:      "); Serial.println(count[0]);
  Serial.print("currentPos:  "); Serial.println(currentPos);
  Serial.print("Destination: "); Serial.println(destination);
 }
 
}//end loop

void speed0() {
  if (forwards == true) count[0]++; //if moving forwards, add counts
  else if (backwards == true) count[0]--; //if moving back, subtract counts
} //end speed0

void goForwards()
{
  forwards = true; backwards = false;
  digitalWrite(enable0, HIGH); //enable board
  analogWrite(PWMA0, 255); //아날로그값(pwm파)를 핀에 출력
  analogWrite(PWMB0, 0); //apply speeds
} //end goForwards

void goBackwards()
{
  forwards = false; backwards = true;
  digitalWrite(enable0, HIGH); //enable board
  analogWrite(PWMA0, 0);
  analogWrite(PWMB0, 255); //apply speeds
} //end goBackwards

void stopMoving()
{
  forwards = false;
  backwards = false;
  Serial.println("Stopped");
  analogWrite(PWMA0, 0);
  analogWrite(PWMB0, 0); //set speeds to 0
  delay(10);
  
  digitalWrite(enable0, LOW); //disable board
} //end stopMoving

void getDistance()
{
digitalWrite(trigPin, LOW); 
delayMicroseconds(10); 
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = ((float)(340 * duration) / 100000) / 2;
Serial.print("Distance:"); Serial.println(distance); Serial.println("mm");
}

void homeActuator() //fully retract actuator and set count to 0
{
  goBackwards();
  delay(25000); //change this value to the amount of time it takes for the actuator to fully retract
  count[0] = {0};
}

Don't use pins 0 or 1 since they are used for serial communications.
Change duration to an unsigned long
Which arduino are you using?

Hi, @jjwoong
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

This enable is never used. Remove this line.

thanks for your advice. i'm sorry but i can't understand changing duration to an unsigned long. can you give me an example? and i'm using arduino uno r4 wifi

Which driver module are you using?

i'm using L298N driver.

Your conditions (curly braces) seem to be missing and/or corrupting your intended code block, from what i can tell. I realize styles vary but shouldn't there be curly braces around those actuator driving functions and the whole affair wrapped in the condition that you are measuring the distance in the range that concerns you?

Should it look more like this?

    if (distance < 1000) {
      destination = distance * 275 ; //translate measured distance (in mm) to desired stroke position (in pulses)

      if ((destination >= (currentPos - threshold)) && (destination <= (currentPos + threshold))) {
        stopMoving();
      }
      else if (destination > currentPos) {
        goForwards();
      }
      else if (destination < currentPos) {
        goBackwards();
      }
    }

No. An if statement is one condition expression that controls the execution of one statement.

  if (<condition expression>) 
    statement-when-true;

A statement can be simple, or compound. A compound statement is a set of statements within { braces }. Each statement in that set may itself be simple or compound.

If all you are controlling is a function call statement, the braces are superfluous.

 if (abel == cain) tryAgain();

and

 if (abel == cain)
   tryAgain();

and

 if (abel == cain) {
   tryAgain();
 }

all generate the same code.

Now as a matter of style, and for good reasons, the if statement is usually presented with the braces, and many ppl always use braces.

Others like to spare the ink, omitting them when they are not needed, and occasionally pay the price for doing. Ask me how I know.

Then we can talk about parentheses. :expressionless:

@hallowed31's other observation looks correct, and the alternate code also.

a7

1 Like

Good to know, cheers.

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