Hello everyone,
I have been struggling to implement an attachInterrupt (on Arduino Uno Digital Pin 2) with a IR Receiver Diode (TSOP38238) and an Linear Actuator (https://www.aliexpress.com/item/100mm-4inch-Stroke-Heavy-duty-1500N-330lbs-Load-DC-12V-Electric-Linear-Actuator-5-7mm-s/2020556667.html?spm=2114.13010608.0.0.1XAV0C). The remote controller I'm using to send IR signal uses NEC protocol and the linear actuator is controlled by a Polulu VNH5019 Motor Driver Carrier (Pololu - VNH5019 Motor Driver Carrier).
SYSTEM DESCRIPTION:
- MacBook Pro macOS Sierra (10.12.4)
- Arduino Uno
- Arduino IDE (1.8.3 Hourly Build 2017/04/26 01:12)
OBJECTIVE:
I want to open and close a mini chamber by controlling the actuator's stroke length. When the chamber closes, I want to take measurements every two seconds and print the result via serial monitor. I'm using the IR remote controller to send a specific signal to close and open the chamber.
PROBLEM:
I can open and close the chamber by sending the specific signal. It also takes measurements, display on serial monitor but during measurement, if I send a chamber open command it does not execute and continues to take measurements (stuck in the loop; interrupt does not work??).
SETUP:
IR Receiver Diode VS --> +5V (Arduino)
IR Receiver Diode GND --> GND (Arduino)
IR Receiver Diode OUT --> 2 (Digital Pin Arduino)
VNH5019 INA --> Arduino Digital Pin 5
VNH5019 PMW --> Arduino Digital Pin 6
VNH5019 INB --> Arduino Digital Pin 4
VNH5019 VDD --> Arduino +5V
VNH5019 GND --> Arduino GND
VNH5019 OUTA --> Linear Actuator (BlackWire)
VNH5019 OUTB --> Linear Actuator (RedWire)
VNH5019 GND --> External Power Supply
VNH5019 VIN --> External Power Supply (12/5A)
WORKING CODE:
#define samplingInterval 2000UL // Sampling delay in ms, can be adjusted.
//Required libraries
#include <IRremote.h>
const byte irPin = 2;
const byte INB = 4;
const byte INA = 5;
const byte PMW = 6;
IRrecv irrecv(irPin);
decode_results results;
const byte sensorPin = A0;
unsigned long currentTime = 0;
unsigned long previousTime = 0;
boolean chambOpen = false;
void setup () {
Serial.begin(19200);
pinMode(irPin, INPUT);
pinMode(INA, OUTPUT);
pinMode(PMW, OUTPUT);
pinMode(INB, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), checkIR, CHANGE);
irrecv.enableIRIn();
}
void loop () {
if (results.value == 16753245) { //Opens chamber
setDir(1);
slide(10000);
chambOpen = true;
}
else if (results.value == 16736925) { //Closes chamber
if (chambOpen = true) {
setDir(2);
slide(10000);
chambOpen = false;
}
while (chambOpen == false) { //Stuck here?
currentTime = millis();
if (currentTime - previousTime > samplingInterval) {
previousTime = currentTime;
Serial.print("Sensor Reading: ");
Serial.println(takeSensorReading());
}
}
}
}
//Takes sensor reading
float takeSensorReading(void) {
float sensorValue = 0.0;
//sensorValue = analogRead(A0);
return sensorValue;
}
//Interrupt ISR
void checkIR(void) {
while (irrecv.decode(&results)) {
irrecv.resume();
}
}
//Sets actuator's stroke direction
void setDir(int d) {
switch (d) {
case 0: // off
digitalWrite(INA, LOW);
digitalWrite(INB, LOW);
break;
case 1: // forward
digitalWrite(INA, HIGH);
digitalWrite(INB, LOW);
break;
case 2: // backward
digitalWrite(INA, LOW);
digitalWrite(INB, HIGH);
break;
case 3: // locked?
digitalWrite(INA, HIGH);
digitalWrite(INB, HIGH);
break;
}
}
//Sets stroke length
void slide(int d) {
for (int v = 0; v < 256; ++v) {
analogWrite(PMW, v);
//delay(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
}
for (int v = 255; v >= 0; --v) {
analogWrite(PMW, v);
//delay(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
delayMicroseconds(d);
}
}
I don't have electronics background. I'd really appreciate if someone can point me what's wrong I'm doing here. Thank you for your time to read the post.