Hi All,
I've been trying to write a sketch that controls a motor. One button for up, one for down. I am using the Ardunio Motor Shield to control the motor.
I have everything connected to do with the hardware. If I run the sketch with just one button coded it works as intended, and if I code for just the other that works too, but when without changing any hardware I code for both it does not! The indicating lights come on on the shield but nothing comes from the motor.
The motor is powered by a wall power supply
To add a second button I have re-named, but basically just duplicated the code that makes one button work
#define LED 13
#define BUTTON 5
#define DOWN 6
#define PWA 3
#define DIRA 12
int val = 0;
int old_val = 0;
int va = 0;
int old_va = 0;
int state = 0;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
pinMode(DOWN, INPUT);
pinMode(PWA, OUTPUT);
pinMode(DIRA, OUTPUT);
}
void loop()
{
val = digitalRead(BUTTON);
va = digitalRead(DOWN);
if((val == HIGH) && (old_val))
{
state = 1 - state;
delay(10);
}
if((va == HIGH) && (old_va))
{
state = 2 - state;
delay(10);
}
old_va = va;
old_val = val;
if(state == 1)
{
digitalWrite(LED, HIGH);
digitalWrite(DIRA, LOW);
analogWrite(PWA, 255);
}else{
digitalWrite(LED, LOW);
analogWrite(PWA, 0);
}
if(state == 2)
{
digitalWrite(LED, LOW);
digitalWrite(DIRA, HIGH);
analogWrite(PWA, 255);
}else{
digitalWrite(LED, LOW);
analogWrite(PWA, 0);
}
}