Paul_KD7HB:
From the tutorial:void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}Paul
Ok, i've amended the code as shown below however the motor spins regardless of which button is being pressed. Also, the motor behaviour seems to depend on whether i define button states at the start as a global variable or within the loop as local (i.e. spins approx 16000rpm if defined as global, but approx 8000rpm if local), which I didn't expect.
// define constants
const int RaiseButton = 13; //The button to raise the actuator is wired to pin 13
const int LowerButton = 11; //The button to lower the actuator is wired to pin 11
const int ClockwisePin = 4; //The input 3 into H-Bridge is wired to pin 4
const int AntiClockPin = 2; //The input 4 into H-Bridge is wired to pin 2
const int enablePin = 7;
int RaiseButtonState = digitalRead(RaiseButton); //Read and store the state of the Raise Button
int LowerButtonState = digitalRead(LowerButton); //Read and store the state of the Lower Button
// setup
void setup() {
pinMode(RaiseButton, INPUT); //Sets the raise button as an input
pinMode(LowerButton, INPUT); //Sets the lower button as an input
pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
pinMode(AntiClockPin, OUTPUT); //Sets the input to the H-Bridge as an output
pinMode(enablePin, OUTPUT);
}
// loop
void loop() {
if (RaiseButtonState == HIGH){ //If the raise button is pressed,
digitalWrite(enablePin, HIGH); //then turn the motor on
digitalWrite(ClockwisePin, HIGH); //clockwise
digitalWrite(AntiClockPin, LOW);
}
if (LowerButtonState == HIGH){ //If the lower button is pressed,
digitalWrite(enablePin, HIGH); //then turn the motor on
digitalWrite(ClockwisePin, LOW); //anti clockwise
digitalWrite(AntiClockPin, HIGH);
}
}
//end of loop