Hello,
I'm currently making an RC submarine for a mechatronics project. I'm using two arduinos where one handles the inputs from the user while the others drives the appropriate motors. Thus, one will be on the sub and the other on the remote. I'm using I2C as the communication protocol between the two devices. My problem is that I'm trying to use the two interrupt pins on the remote arduino to control one of the motors. I've got three motors, and I'm controlling the direction of each one. I was initially using pulling, which worked fine, but I noticed that the motors got weaker as continued to add more motors (resulted in more if statements in the code). Thus, I'm trying to now control one of three motors via the two interrupt pins rather than through polling. The basic idea is that as long as the push button is pressed down (digital pin value of LOW) the motor will be turned on. Thus, I was thinking the mode of the interrupt would be LOW but I have been unsuccessful in getting it work so far. Any help would be greatly appreciated.
// Master code
#include <Wire.h>
// Assigned all appropriate variables for each pin to be used
int buttonMotorAForward = 9;
int buttonMotorAReverse = 8;
int buttonMotorBForward = 7;
int buttonMotorBReverse = 6;
int buttonBallastRise = 10;
int buttonBallastSink = 11;
void setup()
{
// Assign appropriate I/O
pinMode(buttonMotorAForward, INPUT);
pinMode(buttonMotorAReverse, INPUT);
pinMode(buttonMotorBForward, INPUT);
pinMode(buttonMotorBReverse, INPUT);
pinMode(buttonBallastSink, INPUT);
pinMode(buttonBallastRise, INPUT);
// Enable pull resistors for button pins
digitalWrite(buttonMotorAForward, HIGH);
digitalWrite(buttonMotorAReverse, HIGH);
digitalWrite(buttonMotorBForward, HIGH);
digitalWrite(buttonMotorBReverse, HIGH);
digitalWrite(buttonBallastSink, HIGH);
digitalWrite(buttonBallastRise, HIGH);
Wire.begin();
}
void loop()
{
// Forward
if (digitalRead(buttonMotorAForward) == LOW && digitalRead(buttonMotorBForward) == LOW)
{
Wire.beginTransmission(5);
Wire.write('A');
Wire.endTransmission();
}
// Reverse
else if (digitalRead(buttonMotorAReverse) == LOW && digitalRead(buttonMotorBReverse) == LOW)
{
Wire.beginTransmission(5);
Wire.write('B');
Wire.endTransmission();
}
// Spin right
else if (digitalRead(buttonMotorAForward) == LOW && digitalRead(buttonMotorBReverse) == LOW)
{
Wire.beginTransmission(5);
Wire.write('C');
Wire.endTransmission();
}
// Spin left
else if (digitalRead(buttonMotorAReverse) == LOW && digitalRead(buttonMotorBForward) == LOW)
{
Wire.beginTransmission(5);
Wire.write('D');
Wire.endTransmission();
}
else
{ Wire.beginTransmission(5);
Wire.write('K');
Wire.endTransmission();
}
}
// Slave code
#include <Wire.h>
// Assigned all appropriate variables for each pin to be used
int AIN1 = 7;
int AIN2 = 6;
int BIN1 = 5;
int BIN2 = 4;
int PWMA = 11;
int PWMB = 3;
int ballast1 = 12;
int ballast2 = 13;
int PWMballast = 9;
void setup()
{
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(10, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(ballast1, OUTPUT);
pinMode(ballast2, OUTPUT);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
digitalWrite(ballast1, LOW);
digitalWrite(ballast2, LOW);
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
analogWrite(PWMballast, 0);
Wire.begin(5);
Wire.onReceive(receivedEvent);
}
void loop()
{}
void receivedEvent(int howMany)
{
while (Wire.available())
{
char C = Wire.read();
if (C == 'A')
{
digitalWrite(10, HIGH);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
analogWrite(PWMA, 255);
analogWrite(PWMB, 255);
digitalWrite(ballast1, LOW);
digitalWrite(ballast2, LOW);
analogWrite(PWMballast, 0);
}
else if (C == 'B')
{
digitalWrite(10, HIGH);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
analogWrite(PWMA, 255);
analogWrite(PWMB, 255);
digitalWrite(ballast1, LOW);
digitalWrite(ballast2, LOW);
analogWrite(PWMballast, 0);
}
else if (C == 'C')
{
digitalWrite(10, HIGH);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
analogWrite(PWMA, 255);
analogWrite(PWMB, 255);
digitalWrite(ballast1, LOW);
digitalWrite(ballast2, LOW);
analogWrite(PWMballast, 0);
}
else if (C == 'D')
{
digitalWrite(10, HIGH);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
analogWrite(PWMA, 255);
analogWrite(PWMB, 255);
digitalWrite(ballast1, LOW);
digitalWrite(ballast2, LOW);
analogWrite(PWMballast, 0);
}
else
{
digitalWrite(10, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
}
}
}