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};
}