The code here works ok for outputting a PWM sweep on a button release, and then stopping the sweep if the button is pushed again.
I need to also drive another pin high when the same button is pushed. When I add code for this, the new pin does go high, but the PWM sweep no longer works.
I'm sure I am doing something foolish/stupid here but can't seem to find ther answer.
Thanks,
Geno
const int buttonPin = 2;
const int ledPin = 9;
const int controlPina = 8;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(controlPina, OUTPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
analogWrite(ledPin, 0 );
//would like to control another pin as below when the buttonState goes high
//digitalWrite(controlPina, HIGH);
//but this breaks the other functions in the loop
}
else {
delay(2000);
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=.5)
{
if (digitalRead(buttonPin) == HIGH)
{
break;
}
analogWrite(ledPin, fadeValue);
delay(30);
}
}
}
lastButtonState = buttonState;
}