I am trying to control a robotic arm's DC motor using 2 EMG sensors with an Arduino Uno connected to an Arduino motor shield. We currently use analog input A2 (with 1st set of sensors) to turn motor forward and want to use analog input A3 (2nd set of sensors) to change direction by reversing the polarity. This is the code I have developed, any input/suggestions on how to implement the 2nd input sensor? Or being able to read 2 inputs to control one motor's direction?
int sensorPin = A2; // select the input pin
int ledPin = 12; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int PWM_A = 3; // power for motor A
int DIR_A = 12; //direction for motor A
int BRAKE_A = 9; // Brake for motor A must be LOW to move
int PWM_B = 11; // power for motor B
int DIR_B = 13; // direction for motor B
int BRAKE_B = 8; // Brake for motor B
void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin,INPUT);
pinMode(BRAKE_A, OUTPUT);
pinMode(DIR_A, OUTPUT);
pinMode(BRAKE_B, OUTPUT);
pinMode(DIR_B, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
if (sensorValue> 200) // if sensor valve greater that 200 move motor A
{
digitalWrite(BRAKE_A, LOW);
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
analogWrite(PWM_A, 150);
} else {
digitalWrite(BRAKE_A, HIGH); // setting brake LOW disable motor brake
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
analogWrite(PWM_A, 0);
}
Serial.println(sensorValue);
delay(100);
}