Hello,
I am trying to make a simple device that will pan a camera. The device will have 3 modes, which is determined by a digital signal on pins P2 and P3. If both pins are LOW, the device is in "Manual" mode, and the momentary push buttons on pins P4 and P5 control rotation. If P2 is HIGH, the device is in "Continuous" mode, and a momentary push in either direction will cause the motor to spin continuously, until the other direction is pushed, or the mode is changed. If P3 is high, the motor will automatically pan back and forth, for a programmable amount of time in each direction. I am using a 3 position switch, making it impossible for P2 and P3 to be HIGH.
The code at the bottom of the post is working for manual mode and continuous mode. There is currently no code for pan mode because I cannot get P3 to sit at a low state. I discovered this when trying to change continuous mode to P3 instead of P2. I am using 10k resistors to ground on all 4 input pins, and P2, P4, and P5 all seem to working fine.
Any suggestions as to why P3 isn't going low. I am using 4 "AAA" batteries for power, so my VCC voltage is a bit over 6 volt. P3 sits are about 2.8 volt upon loading this code.
Thanks in advance.
// Remote control for camera panning
// Define Pins
#define MOTOR_OUTPUT_A 0 // output to h-bridge to control motor
#define MOTOR_OUTPUT_B 1 // output to h-bridge to control motor
#define MODE_CONT 2 // pins 2 and 3 are wired to a 3 position switch with the middle position being
#define MODE_PAN 3 // off. This is a "mode" switch, and the common has a HIGH signal
#define PUSHBUTTON_CW 4 // push button for clockwise rotation
#define PUSHBUTTON_CCW 5 // push button for counter-clockwise rotation
#define MOTOR_SPEED_CW 35 // my motor spins slightly faster CW than
#define MOTOR_SPEED_CCW 38 // it does CCW, hence 2 different speeds.
void setup() {
// set pin modes
pinMode(MOTOR_OUTPUT_A, OUTPUT);
pinMode(MOTOR_OUTPUT_B, OUTPUT);
pinMode(MODE_PAN, INPUT);
pinMode(MODE_CONT, INPUT);
pinMode(PUSHBUTTON_CW, INPUT);
pinMode(PUSHBUTTON_CCW, INPUT);
// set starting pin states
digitalWrite(MOTOR_OUTPUT_A, LOW);
digitalWrite(MOTOR_OUTPUT_B, LOW);
digitalWrite(MODE_PAN, LOW);
digitalWrite(MODE_CONT, LOW);
digitalWrite(PUSHBUTTON_CW, LOW);
digitalWrite(PUSHBUTTON_CCW, LOW);
}
void loop() {
// check the directional pushbuttons, if one is pushed, start motor in that direction.
if (digitalRead(PUSHBUTTON_CW) == HIGH) {
spin_CW(MOTOR_SPEED_CW);
}
if (digitalRead(PUSHBUTTON_CCW) == HIGH) {
spin_CCW(MOTOR_SPEED_CCW);
}
// check if pushbutton is released, and NOT in continous mode, if so, stop motor.
if (digitalRead(PUSHBUTTON_CW) == LOW && digitalRead(PUSHBUTTON_CCW) == LOW && digitalRead(MODE_CONT) == LOW) {
spin_STOP();
}
}
// spin the motor clockwise
void spin_CW(int speed) {
analogWrite(MOTOR_OUTPUT_B, speed);
digitalWrite(MOTOR_OUTPUT_A, LOW);
}
// spin the motor counter-clockwise
void spin_CCW(int speed) {
analogWrite(MOTOR_OUTPUT_A, speed);
digitalWrite(MOTOR_OUTPUT_B, LOW);
}
// stop the motor
void spin_STOP() {
digitalWrite(MOTOR_OUTPUT_A, LOW);
digitalWrite(MOTOR_OUTPUT_B, LOW);
}